Skip to content

Commit 1e4c5bd

Browse files
authored
Expose brand/update via User resource (#123)
* Expose brand/update via User resource * Fix updating Brand through User.
1 parent 25c4da2 commit 1e4c5bd

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

lib/easypost.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
require "easypost/address"
1313
require "easypost/api_key"
1414
require "easypost/batch"
15+
require "easypost/brand"
1516
require "easypost/carrier_account"
1617
require "easypost/carrier_type"
1718
require "easypost/customs_info"

lib/easypost/brand.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class EasyPost::Brand < EasyPost::Resource
2+
def url
3+
if user_id.nil? || user_id.empty?
4+
raise EasyPost::Error, "Missing user_id: #{self.class} instance is missing user_id"
5+
end
6+
7+
"#{::EasyPost::User.url}/#{CGI.escape(user_id)}/brand"
8+
end
9+
end

lib/easypost/user.rb

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class EasyPost::User < EasyPost::Resource
2-
def self.create(params={}, api_key=nil)
3-
response = EasyPost.make_request(:post, self.url, api_key, {self.class_name.to_sym => params})
4-
return EasyPost::Util.convert_to_easypost_object(response, api_key)
2+
def self.create(params = {}, api_key = nil)
3+
response = EasyPost.make_request(:post, url, api_key, {class_name.to_sym => params})
4+
EasyPost::Util.convert_to_easypost_object(response, api_key)
55
end
66

77
def save
@@ -14,11 +14,11 @@ def save
1414
response = EasyPost.make_request(:put, url, @api_key, wrapped_params)
1515
refresh_from(response, api_key)
1616
end
17-
return self
17+
self
1818
end
1919

2020
def self.retrieve_me
21-
self.all
21+
all
2222
end
2323

2424
def self.all_api_keys
@@ -28,11 +28,11 @@ def self.all_api_keys
2828
def api_keys
2929
api_keys = EasyPost::User.all_api_keys
3030

31-
if api_keys.id == self.id
31+
if api_keys.id == id
3232
my_api_keys = api_keys.keys
3333
else
34-
for child in api_keys.children
35-
if child.id == self.id
34+
api_keys.children.each do |child|
35+
if child.id == id
3636
my_api_keys = child.keys
3737
break
3838
end
@@ -41,4 +41,16 @@ def api_keys
4141

4242
my_api_keys
4343
end
44+
45+
def update_brand(**attrs)
46+
brand = EasyPost::Brand.new
47+
data = {object: "Brand", user_id: id, **attrs}
48+
# Add accessors manually because there's no API to retrieve a brand
49+
brand.add_accessors(data.keys)
50+
# Assigning values with accessors defined above
51+
data.each do |key, val|
52+
brand.send("#{key}=", val)
53+
end
54+
brand.save
55+
end
4456
end

0 commit comments

Comments
 (0)