asaas-ruby 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ff2b6e280817ed7d7abf7f48c9ac9d01c752eb7c
4
- data.tar.gz: f86480247971d2b7ed06706d16aebd476b5d8507
3
+ metadata.gz: 068d38ad9f1570c2e0dc3b0fd943ffd1f2dfd46c
4
+ data.tar.gz: b070ebb21e0ad32a6a0ad8a83a69e84b4a765081
5
5
  SHA512:
6
- metadata.gz: da1acf421a06fab7ebfe35bccc25dc7490257e89fc7a63d815f39584a07194131555fdeaeb3e6221b7576c3619458242afe80753072072ded2bdff5121478e91
7
- data.tar.gz: da1373477fbb0bbef6f46aa9a26ef0530cc402daec6d82c0f676f02577b05e997c414f200ea1ac667be0d2f90003897ebb5dcef9241f17ebe154d2544f6bac09
6
+ metadata.gz: 9fc41b5bfb8e46307b3e16bb7e2ee9bd3f66782552ea517f1fee77d6bbe12a31f402d06337b09db2012902589a2dc2729b6687cfcb1656c9d2c74ecbc2544edf
7
+ data.tar.gz: 07429a1f31d312df774126675226ebf079f2e9ebeb6b351d716e855bfe0b703c6f79c82d88fec40c4b5b27ebb64891471a4be0fada35be54cc4af3a806454044
Binary file
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "bundler/setup"
4
- require "asaas"
4
+ require "asaas-ruby"
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -54,19 +54,20 @@ module Asaas
54
54
  end
55
55
 
56
56
  def parse_response
57
- ap hash
58
- hash = JSON.parse(@response.body)
59
- @errors = false
60
- @success = true
61
- if hash.fetch("errors", false)
62
- @errors = hash.fetch("errors")
63
- @success = false
64
- elsif hash.fetch("object", false) === "list"
65
- Asaas::Entity::Meta.new(hash)
66
- else
67
- entity = convert_data_to_entity(hash.fetch("object", false))
68
- entity.new(hash) if entity
57
+ ap @response
58
+ res = case @response.response_code
59
+ when 200
60
+ response_success
61
+ when 400
62
+ response_bad_request
63
+ when 401
64
+ response_unauthorized
65
+ when 404
66
+ response_not_found
67
+ when 500
68
+ response_internal_server_error
69
69
  end
70
+ res
70
71
  end
71
72
 
72
73
  def convert_data_to_entity(type)
@@ -74,15 +75,62 @@ module Asaas
74
75
  end
75
76
 
76
77
  def request(method, params = {}, body = nil)
78
+ body = body && body.try(:attributes)
77
79
  @response = Typhoeus::Request.new(
78
80
  parse_url(params.fetch(:id, false)),
79
81
  method: method,
80
- body: body.attributes,
82
+ body: body,
81
83
  params: params,
82
84
  headers: { 'access_token': @token || Asaas::Configuration.token }
83
85
  ).run
84
86
  end
85
87
 
88
+ def response_success
89
+ hash = JSON.parse(@response.body)
90
+ if hash.fetch("object", false) === "list"
91
+ entity = Asaas::Entity::Meta.new(hash)
92
+ else
93
+ entity = convert_data_to_entity(hash.fetch("object", false))
94
+ entity.new(hash) if entity
95
+ end
96
+ entity
97
+ end
98
+
99
+ def response_unauthorized
100
+ error = Asaas::Entity::Error.new
101
+ error.errors << Asaas::Entity::ErrorItem.new(code: 'invalid_token', description: 'The api_key is invalid')
102
+ error
103
+ end
104
+
105
+ def response_internal_server_error
106
+ error = Asaas::Entity::Error.new
107
+ error.errors << Asaas::Entity::ErrorItem.new(code: 'internal_server_error', description: 'Internal Server Error')
108
+ error
109
+ end
110
+
111
+ def response_not_found
112
+ error = Asaas::Entity::Error.new
113
+ error.errors << Asaas::Entity::ErrorItem.new(code: 'not_found', description: 'Object not found')
114
+ error
115
+ end
116
+
117
+ def response_bad_request
118
+ error = Asaas::Entity::Error.new
119
+ begin
120
+ hash = JSON.parse(@response.body)
121
+ errors = hash.fetch("errors", [])
122
+ errors.each do |item|
123
+ error.errors << Asaas::Entity::ErrorItem.new(item)
124
+ end
125
+ error
126
+ rescue
127
+ error = Asaas::Entity::Error.new
128
+ error.errors << Asaas::Entity::ErrorItem.new(code: 'bad_request', description: 'Bad Request')
129
+ error
130
+ end
131
+ error
132
+ end
133
+
86
134
  def get_headers
87
135
  { 'access_token': @token || Asaas::Configuration.token }
88
136
  end
data/lib/asaas/entity.rb CHANGED
@@ -5,6 +5,8 @@ module Asaas
5
5
  autoload :BankAccount, 'asaas/entity/bank_account'
6
6
  autoload :City, 'asaas/entity/city'
7
7
  autoload :Customer, 'asaas/entity/customer'
8
+ autoload :Error, 'asaas/entity/error'
9
+ autoload :ErrorItem, 'asaas/entity/error_item'
8
10
  autoload :Installment, 'asaas/entity/installment'
9
11
  autoload :Meta, 'asaas/entity/meta'
10
12
  autoload :Notification, 'asaas/entity/notification'
@@ -0,0 +1,10 @@
1
+ module Asaas
2
+ module Entity
3
+ class Error
4
+ include Virtus.model
5
+
6
+ attribute :errors, Array[ErrorItem]
7
+
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module Asaas
2
+ module Entity
3
+ class ErrorItem
4
+ include Virtus.model
5
+ attribute :code, String
6
+ attribute :description, String
7
+ end
8
+ end
9
+ end
data/lib/asaas/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Asaas
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4 "
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asaas-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcos Junior
@@ -160,6 +160,7 @@ files:
160
160
  - asaas-ruby-0.1.0.gem
161
161
  - asaas-ruby-0.1.1.gem
162
162
  - asaas-ruby-0.1.2.gem
163
+ - asaas-ruby-0.1.3.gem
163
164
  - asaas-ruby.gemspec
164
165
  - bin/console
165
166
  - bin/setup
@@ -180,6 +181,8 @@ files:
180
181
  - lib/asaas/entity/base.rb
181
182
  - lib/asaas/entity/city.rb
182
183
  - lib/asaas/entity/customer.rb
184
+ - lib/asaas/entity/error.rb
185
+ - lib/asaas/entity/error_item.rb
183
186
  - lib/asaas/entity/installment.rb
184
187
  - lib/asaas/entity/meta.rb
185
188
  - lib/asaas/entity/notification.rb