Skip to content

Commit

Permalink
Expose brand/update via User resource (#123)
Browse files Browse the repository at this point in the history
* Expose brand/update via User resource

* Fix updating Brand through User.
  • Loading branch information
andychongyz authored Dec 6, 2021
1 parent 25c4da2 commit 1e4c5bd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/easypost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require "easypost/address"
require "easypost/api_key"
require "easypost/batch"
require "easypost/brand"
require "easypost/carrier_account"
require "easypost/carrier_type"
require "easypost/customs_info"
Expand Down
9 changes: 9 additions & 0 deletions lib/easypost/brand.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class EasyPost::Brand < EasyPost::Resource
def url
if user_id.nil? || user_id.empty?
raise EasyPost::Error, "Missing user_id: #{self.class} instance is missing user_id"
end

"#{::EasyPost::User.url}/#{CGI.escape(user_id)}/brand"
end
end
28 changes: 20 additions & 8 deletions lib/easypost/user.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class EasyPost::User < EasyPost::Resource
def self.create(params={}, api_key=nil)
response = EasyPost.make_request(:post, self.url, api_key, {self.class_name.to_sym => params})
return EasyPost::Util.convert_to_easypost_object(response, api_key)
def self.create(params = {}, api_key = nil)
response = EasyPost.make_request(:post, url, api_key, {class_name.to_sym => params})
EasyPost::Util.convert_to_easypost_object(response, api_key)
end

def save
Expand All @@ -14,11 +14,11 @@ def save
response = EasyPost.make_request(:put, url, @api_key, wrapped_params)
refresh_from(response, api_key)
end
return self
self
end

def self.retrieve_me
self.all
all
end

def self.all_api_keys
Expand All @@ -28,11 +28,11 @@ def self.all_api_keys
def api_keys
api_keys = EasyPost::User.all_api_keys

if api_keys.id == self.id
if api_keys.id == id
my_api_keys = api_keys.keys
else
for child in api_keys.children
if child.id == self.id
api_keys.children.each do |child|
if child.id == id
my_api_keys = child.keys
break
end
Expand All @@ -41,4 +41,16 @@ def api_keys

my_api_keys
end

def update_brand(**attrs)
brand = EasyPost::Brand.new
data = {object: "Brand", user_id: id, **attrs}
# Add accessors manually because there's no API to retrieve a brand
brand.add_accessors(data.keys)
# Assigning values with accessors defined above
data.each do |key, val|
brand.send("#{key}=", val)
end
brand.save
end
end

0 comments on commit 1e4c5bd

Please sign in to comment.