Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api key as option to manage multiple accounts #2

Merged
merged 5 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion iugu.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ Gem::Specification.new do |spec|

spec.add_dependency 'rest-client'

spec.add_development_dependency 'bundler', '~> 1.5'
spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec', '~> 3'
spec.add_development_dependency 'byebug'
spec.add_development_dependency 'vcr'
spec.add_development_dependency 'webmock'
spec.add_development_dependency 'pry'
spec.add_development_dependency 'pry-nav'
end
18 changes: 8 additions & 10 deletions lib/iugu/api_child_resource.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
module Iugu
class APIChildResource
@parent_keys = {}
@fabricator = nil

def initialize(parent_keys = {}, fabricator)
def initialize(parent_keys = {}, fabricator = nil, options = {})
@parent_keys = parent_keys
@fabricator = fabricator
@options = options
end

def create(attributes = {})
@fabricator.send "create", merge_params(attributes)
def create(params = {})
@fabricator.send "create", merge_params(params), options
end

def search(options = {})
@fabricator.send "search", merge_params(options)
def search(params = {})
@fabricator.send "search", merge_params(params), options
end

def fetch(options = nil)
@fabricator.send "fetch", merge_params({ id: options })
def fetch(params = nil)
@fabricator.send "fetch", merge_params({ id: params }), options
end

private
Expand Down
7 changes: 2 additions & 5 deletions lib/iugu/api_create.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
module Iugu
module APICreate
module ClassMethods
def create(attributes = {})
Iugu::Factory.create_from_response(self.object_type,
APIRequest.request('POST',
self.url(attributes),
attributes))
def create(attributes = {}, options = {})
Iugu::Factory.create_from_response(self.object_type, APIRequest.request("POST", self.url(attributes), attributes, options), nil, options)
rescue Iugu::RequestWithErrors => ex
obj = self.new
obj.set_attributes attributes, true
Expand Down
5 changes: 3 additions & 2 deletions lib/iugu/api_delete.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Iugu
module APIDelete
def delete
APIRequest.request('DELETE', self.class.url(self.attributes))
def delete(options = {})
options = self.options.merge(options)
APIRequest.request("DELETE", self.class.url(self.attributes), {}, options)
self.errors = nil
true
rescue Iugu::RequestWithErrors => ex
Expand Down
13 changes: 5 additions & 8 deletions lib/iugu/api_fetch.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module Iugu
module APIFetch
def refresh
copy Iugu::Factory.create_from_response(self.class.object_type,
APIRequest.request('GET',
self.class.url(self.id)))
def refresh(options = {})
options = self.options.merge(options)
copy Iugu::Factory.create_from_response(self.class.object_type, APIRequest.request("GET", self.class.url(self.id), {}, options))
self.errors = nil
true
rescue Iugu::RequestWithErrors => ex
Expand All @@ -12,10 +11,8 @@ def refresh
end

module ClassMethods
def fetch(options = nil)
Iugu::Factory.create_from_response(self.object_type,
APIRequest.request('GET',
self.url(options)))
def fetch(params = nil, options = {})
Iugu::Factory.create_from_response(self.object_type, APIRequest.request("GET", self.url(params), {}, options), nil, options)
end
end

Expand Down
25 changes: 12 additions & 13 deletions lib/iugu/api_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

module Iugu
class APIRequest

def self.request(method, url, data = {})
Iugu::Utils.auth_from_env if Iugu.api_key.nil?
raise Iugu::AuthenticationException, 'Chave de API não configurada. Utilize Iugu.api_key = ... para configurar.' if Iugu.api_key.nil?
handle_response self.send_request method, url, data
def self.request(method, url, data = {}, options = {})
api_key = options[:api_key] || Iugu.api_key || Iugu::Utils.auth_from_env
raise Iugu::AuthenticationException, "Chave de API não configurada. Utilize Iugu.api_key = ... para configurar." if api_key.nil?
handle_response self.send_request api_key, method, url, data
end

def self.send_request(method, url, data)
RestClient::Request.execute build_request(method, url, data)
private

def self.send_request(api_key, method, url, data)
RestClient::Request.execute build_request(api_key, method, url, data)
rescue RestClient::ResourceNotFound
raise ObjectNotFound
rescue RestClient::UnprocessableEntity => ex
Expand All @@ -21,10 +22,10 @@ def self.send_request(method, url, data)
raise RequestWithErrors.new JSON.parse(ex.response)['errors']
end

def self.build_request(method, url, data)
def self.build_request(api_key, method, url, data)
{
verify_ssl: true,
headers: default_headers,
headers: default_headers(api_key),
method: method,
payload: data.to_json,
url: url,
Expand All @@ -41,17 +42,15 @@ def self.handle_response(response)
raise RequestFailed
end

def self.default_headers
def self.default_headers(api_key)
{
authorization: 'Basic ' + Base64.encode64(Iugu.api_key + ':'),
authorization: 'Basic ' + Base64.encode64(api_key + ":"),
accept: 'application/json',
accept_charset: 'utf-8',
user_agent: 'Iugu RubyLibrary',
accept_language: 'pt-br;q=0.9,pt-BR',
#content_type: 'application/x-www-form-urlencoded; charset=utf-8'
content_type: 'application/json; charset=utf-8'
}
end

end
end
2 changes: 1 addition & 1 deletion lib/iugu/api_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def self.relative_url(options = "")
end

def self.object_base_uri
pluralized_models = ["customer", "payment_method", "invoice", "subscription", "plan"]
pluralized_models = ["customer", "payment_method", "invoice", "subscription", "plan", "account"]
if pluralized_models.include? self.object_type
object_type = self.object_type + "s"
else
Expand Down
8 changes: 2 additions & 6 deletions lib/iugu/api_save.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
module Iugu
module APISave
def save
method = is_new? ? 'POST' : 'PUT'
copy Iugu::Factory.create_from_response(self.class.object_type,
APIRequest.request(method,
self.class.url(self.attributes),
modified_attributes))
def save(options = self.options)
copy Iugu::Factory.create_from_response(self.class.object_type, APIRequest.request(is_new? ? "POST" : "PUT", self.class.url(self.attributes), modified_attributes, options))
self.errors = nil
true
rescue Iugu::RequestWithErrors => ex
Expand Down
5 changes: 3 additions & 2 deletions lib/iugu/api_search.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module Iugu
module APICreate
module ClassMethods
def search(options = {})
Iugu::Factory.create_from_response self.object_type, APIRequest.request("GET", self.url(options), options)
def search(params = {}, options = {})
options = self.options.merge(options)
Iugu::Factory.create_from_response self.object_type, APIRequest.request("GET", self.url(params), params, options)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/iugu/charge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def success

def invoice
return false unless @attributes['invoice_id']
Invoice.fetch @attributes['invoice_id']
Invoice.fetch(@attributes['invoice_id'], options)
end
end
end
6 changes: 3 additions & 3 deletions lib/iugu/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ class Customer < APIResource
include Iugu::APIDelete

def payment_methods
APIChildResource.new({ customer_id: self.id }, Iugu::PaymentMethod)
APIChildResource.new({ customer_id: self.id }, Iugu::PaymentMethod, options)
end

def invoices
APIChildResource.new({ customer_id: self.id }, Iugu::Invoice)
APIChildResource.new({ customer_id: self.id }, Iugu::Invoice, options)
end

def default_payment_method
return false unless @attributes['default_payment_method_id']
PaymentMethod.fetch({ id: @attributes['default_payment_method_id'], customer_id: self.id })
PaymentMethod.fetch({ id: @attributes['default_payment_method_id'], customer_id: self.id }, options)
end
end
end
18 changes: 8 additions & 10 deletions lib/iugu/factory.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
module Iugu
class Factory
def self.create_from_response(object_type, response, errors = nil)
def self.create_from_response(object_type, response, errors = nil, options = {})
if response.nil?
obj = Iugu.const_get(Iugu::Utils.camelize(object_type)).new
obj.errors = errors if errors
obj.options = options

obj
elsif response.is_a?(Array)
results = []
response.each do |i|
results.push Iugu.const_get(Iugu::Utils.camelize(object_type)).new i
end
Iugu::SearchResult.new results, results.count
response.each { |i| results.push(Iugu.const_get(Iugu::Utils.camelize(object_type)).new(i, options)) }
Iugu::SearchResult.new(results, results.count)
elsif response['items'] && response['totalItems']
results = []
response['items'].each do |v|
results.push self.create_from_response(object_type, v)
end
Iugu::SearchResult.new results, response['totalItems']
response['items'].each { |v| results.push(self.create_from_response(object_type, v, errors, options)) }
Iugu::SearchResult.new(results, response['totalItems'])
else
Iugu.const_get(Iugu::Utils.camelize(object_type)).new response
Iugu.const_get(Iugu::Utils.camelize(object_type)).new(response, options)
end
end
end
Expand Down
19 changes: 7 additions & 12 deletions lib/iugu/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ class Invoice < APIResource

def customer
return false unless @attributes['customer_id']
Customer.fetch @attributes['customer_id']
Customer.fetch(@attributes['customer_id'], options)
end

def cancel
copy Iugu::Factory.create_from_response(self.class.object_type,
APIRequest.request('PUT',
"#{self.class.url(self.id)}/cancel"))
copy Iugu::Factory.create_from_response(self.class.object_type, APIRequest.request("PUT", "#{self.class.url(self.id)}/cancel", {}, options), nil, options)
self.errors = nil
true
rescue Iugu::RequestWithErrors => ex
Expand All @@ -22,21 +20,18 @@ def cancel
end

def refund
copy Iugu::Factory.create_from_response(self.class.object_type,
APIRequest.request('POST',
"#{self.class.url(self.id)}/refund"))
copy Iugu::Factory.create_from_response(self.class.object_type, APIRequest.request("POST", "#{self.class.url(self.id)}/refund", {}, options), nil, options)
self.errors = nil
true
rescue Iugu::RequestWithErrors => ex
self.errors = ex.errors
false
end

def duplicate(attributes = {})
copy Iugu::Factory.create_from_response(self.class.object_type,
APIRequest.request('POST',
"#{self.class.url(self.id)}/duplicate",
attributes ))
def duplicate(params = {})
params.merge!(id: self.id)

copy Iugu::Factory.create_from_response(self.class.object_type, APIRequest.request("POST", "#{self.class.url(self.id)}/duplicate", params, options), nil, options)
self.errors = nil
true
rescue Iugu::RequestWithErrors => ex
Expand Down
6 changes: 3 additions & 3 deletions lib/iugu/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
module Iugu
class Object

attr_accessor :errors
attr_accessor :errors, :options

undef :id if method_defined?(:id)

def initialize(attributes = {})
def initialize(attributes = {}, options = {})
@unsaved_attributes = Set.new
@options = options
set_attributes attributes
end

Expand Down Expand Up @@ -60,6 +61,5 @@ def set_attributes(attributes, unsaved = false)
def metaclass
class << self; self; end
end

end
end
6 changes: 2 additions & 4 deletions lib/iugu/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ class Plan < APIResource
include Iugu::APISave
include Iugu::APIDelete

def self.fetch_by_identifier(identifier)
Iugu::Factory.create_from_response(object_type,
APIRequest.request('GET',
"#{url}/identifier/#{identifier}"))
def self.fetch_by_identifier(identifier, options = {})
Iugu::Factory.create_from_response(object_type, APIRequest.request("GET", "#{url}/identifier/#{identifier}", {}, options), nil, options)
end
end
end
Loading