Skip to content

Commit 6dd677f

Browse files
authored
Merge pull request iugu#24 from iugu/add-specs-to-customers
Add specs to customers
2 parents 5480972 + 63f4208 commit 6dd677f

15 files changed

+922
-29
lines changed

lib/iugu/api_create.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module Iugu
22
module APICreate
33
module ClassMethods
44
def create(attributes = {})
5-
Iugu::Factory.create_from_response self.object_type, APIRequest.request("POST", self.url(attributes), attributes)
5+
Iugu::Factory.create_from_response(self.object_type,
6+
APIRequest.request('POST',
7+
self.url(attributes),
8+
attributes))
69
rescue Iugu::RequestWithErrors => ex
710
obj = self.new
811
obj.set_attributes attributes, true

lib/iugu/api_delete.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Iugu
22
module APIDelete
33
def delete
4-
APIRequest.request "DELETE", self.class.url(self.attributes)
4+
APIRequest.request('DELETE', self.class.url(self.attributes))
55
self.errors = nil
66
true
77
rescue Iugu::RequestWithErrors => ex

lib/iugu/api_fetch.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
module Iugu
22
module APIFetch
33
def refresh
4-
copy Iugu::Factory.create_from_response(self.class.object_type, APIRequest.request("GET", self.class.url(self.id)))
4+
copy Iugu::Factory.create_from_response(self.class.object_type,
5+
APIRequest.request('GET',
6+
self.class.url(self.id)))
57
self.errors = nil
68
true
79
rescue Iugu::RequestWithErrors => ex
@@ -11,7 +13,9 @@ def refresh
1113

1214
module ClassMethods
1315
def fetch(options = nil)
14-
Iugu::Factory.create_from_response self.object_type, APIRequest.request("GET", self.url(options))
16+
Iugu::Factory.create_from_response(self.object_type,
17+
APIRequest.request('GET',
18+
self.url(options)))
1519
end
1620
end
1721

lib/iugu/api_request.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
#encoding: UTF-8
2-
31
require 'rest_client'
4-
require "base64"
5-
require "json"
2+
require 'base64'
3+
require 'json'
64

75
module Iugu
86
class APIRequest
97

108
def self.request(method, url, data = {})
119
Iugu::Utils.auth_from_env if Iugu.api_key.nil?
12-
raise Iugu::AuthenticationException, "Chave de API não configurada. Utilize Iugu.api_key = ... para configurar." if Iugu.api_key.nil?
10+
raise Iugu::AuthenticationException, 'Chave de API não configurada. Utilize Iugu.api_key = ... para configurar.' if Iugu.api_key.nil?
1311
handle_response self.send_request method, url, data
1412
end
1513

16-
private
17-
1814
def self.send_request(method, url, data)
1915
RestClient::Request.execute build_request(method, url, data)
2016
rescue RestClient::ResourceNotFound
@@ -26,7 +22,7 @@ def self.send_request(method, url, data)
2622
end
2723

2824
def self.build_request(method, url, data)
29-
{
25+
{
3026
verify_ssl: true,
3127
headers: default_headers,
3228
method: method,
@@ -38,7 +34,7 @@ def self.build_request(method, url, data)
3834

3935
def self.handle_response(response)
4036
response_json = JSON.parse(response.body)
41-
raise ObjectNotFound if response_json.is_a?(Hash) && response_json['errors'] == "Not Found"
37+
raise ObjectNotFound if response_json.is_a?(Hash) && response_json['errors'] == 'Not Found'
4238
raise RequestWithErrors, response_json['errors'] if response_json.is_a?(Hash) && response_json['errors'] && response_json['errors'].length > 0
4339
response_json
4440
rescue JSON::ParserError
@@ -47,7 +43,7 @@ def self.handle_response(response)
4743

4844
def self.default_headers
4945
{
50-
authorization: 'Basic ' + Base64.encode64(Iugu.api_key + ":"),
46+
authorization: 'Basic ' + Base64.encode64(Iugu.api_key + ':'),
5147
accept: 'application/json',
5248
accept_charset: 'utf-8',
5349
user_agent: 'Iugu RubyLibrary',

lib/iugu/api_save.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
module Iugu
22
module APISave
33
def save
4-
copy Iugu::Factory.create_from_response(self.class.object_type, APIRequest.request(is_new? ? "POST" : "PUT", self.class.url(self.attributes), modified_attributes))
4+
method = is_new? ? 'POST' : 'PUT'
5+
copy Iugu::Factory.create_from_response(self.class.object_type,
6+
APIRequest.request(method,
7+
self.class.url(self.attributes),
8+
modified_attributes))
59
self.errors = nil
610
true
711
rescue Iugu::RequestWithErrors => ex

spec/cassettes/Iugu_Customer/_create/should_create_a_customer_with_CPF.yml

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/cassettes/Iugu_Customer/_create/should_create_a_customer_with_CPNJ.yml

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/cassettes/Iugu_Customer/_create/should_create_a_customer_with_full_address.yml

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/cassettes/Iugu_Customer/_create/should_create_a_customer_with_just_the_email_and_name.yml

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)