mp_api 0.1.8 → 0.1.9

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
  SHA256:
3
- metadata.gz: 25bbc0e3b8f592463032d3bb549647a6a6d8f49489b11d303bc9027e8422b4af
4
- data.tar.gz: b4f29ee32c428589423a1c199c41b969743089bf15a0f7d97e02a7ffb913a933
3
+ metadata.gz: 7958b0b7c457103803f4add0d017a49d36e0eb420552ed406aebe9a9bb31a210
4
+ data.tar.gz: 9421b7d5e0a50e7e4552f07f6319708b7abbb92b9e34925a1e2993f790155ba1
5
5
  SHA512:
6
- metadata.gz: 8033add8df41e4c1b8200657926ec9c080801c29861fe039d4e83c886298a2e1fbd70c3e9740cb0374f52a613aa42a52be880d2fb53c0f42d73c637ba37e26c1
7
- data.tar.gz: a472148d5368e384d2025e80e4498841f3175a33e1bc5486ebfec309d2789717bf27f636f1e2592f62454e10c2789456e54be201ccce232307d9d0d4e725ba7e
6
+ metadata.gz: 90b2e88528da605933fc22078a737a6b16d48df84a57c1034850d3627a4331a0c26882a3ca786410d2a78ff1f844e0913ddeda90c3b6a05bb93fd5038decced7
7
+ data.tar.gz: bda6cb3e8f9dabd483569564f7f542d0f00490722d783ead92ef10ec634afc07e06ac494f79448c48e21b4c016036e1a5809f4b530056b07c7fbe5270f8ab8a3
data/lib/mp_api/client.rb CHANGED
@@ -1,55 +1,60 @@
1
- module MpApi
2
- class Client < Ac::Base
3
- BASE_URL = "https://api.mercadopago.com/v1/"
4
- MAX_RETRIES = 2
5
- attr_reader :access_token
6
- def initialize(access_token=MpApi.configuration.access_token)
7
- @headers = {
8
- "Content-Type": "application/json",
9
- 'x-idempotency-key' => SecureRandom.uuid
10
- }
11
- super access_token
12
- end
13
-
14
- def create_payment(body)
15
- response = post("/payments", body: body, headers: @headers) do |response|
16
- p response.json unless response.success?
17
- ![500, 429].include?(response.code) || response['id']
18
- end
19
- response.json
20
- end
21
-
22
- def get_payment(payment_id)
23
- response = get("/payments/#{payment_id}") do |response|
24
- p response.json unless response.success?
25
- ![500, 429].include?(response.code) || response['id']
26
- end
27
- response.json
28
- end
29
-
30
- def create_token(card_token_data)
31
- response = post("/card_tokens", body: card_token_data, headers: @headers) do |response|
32
- p response.json unless response.success?
33
- ![500, 429].include?(response.code) || response['id']
34
- end
35
- response.json
36
- end
37
-
38
- def search_payment_methods(query)
39
- response = get("/payment_methods/search", params: query) do |response|
40
- p response.json unless response.success?
41
- ![500, 429].include?(response.code) || response.json['results'][0]
42
- end
43
- response.json
44
- end
45
-
46
- def get_payment_methods
47
- response = get("/payment_methods") do |response|
48
- p response.json unless response.success?
49
- ![500, 429].include?(response.code) || response['id']
50
- end
51
- response.json
52
- end
53
-
54
- end
1
+ module MpApi
2
+ class Client < Ac::Base
3
+ BASE_URL = "https://api.mercadopago.com/v1/"
4
+ MAX_RETRIES = 2
5
+ attr_reader :access_token
6
+ def initialize(access_token=MpApi.configuration.access_token)
7
+ @headers = {
8
+ "Content-Type": "application/json",
9
+ 'x-idempotency-key' => SecureRandom.uuid
10
+ }
11
+ super access_token
12
+ end
13
+
14
+ def create_payment(body)
15
+ response = post("/payments", body: body, headers: @headers) do |response|
16
+ p response.json unless response.success?
17
+ ![500, 429].include?(response.code) || response['id']
18
+ end
19
+ raise RequestError.new(response.json['message']) if ![200,201].include?(response.code)
20
+ response.json
21
+ end
22
+
23
+ def get_payment(payment_id)
24
+ response = get("/payments/#{payment_id}") do |response|
25
+ p response.json unless response.success?
26
+ ![500, 429].include?(response.code) || response['id']
27
+ end
28
+ raise RequestError.new(response.json['message']) if ![200,201].include?(response.code)
29
+ response.json
30
+ end
31
+
32
+ def create_token(card_token_data)
33
+ response = post("/card_tokens", body: card_token_data, headers: @headers) do |response|
34
+ p response.json unless response.success?
35
+ ![500, 429].include?(response.code) || response['id']
36
+ end
37
+ raise RequestError.new(response.json['message']) if ![200,201].include?(response.code)
38
+ response.json
39
+ end
40
+
41
+ def search_payment_methods(query)
42
+ response = get("/payment_methods/search", params: query) do |response|
43
+ p response.json unless response.success?
44
+ ![500, 429].include?(response.code) || response.json['results'][0]
45
+ end
46
+ raise RequestError.new(response.json['message']) if ![200,201].include?(response.code)
47
+ response.json
48
+ end
49
+
50
+ def get_payment_methods
51
+ response = get("/payment_methods") do |response|
52
+ p response.json unless response.success?
53
+ ![500, 429].include?(response.code) || response['id']
54
+ end
55
+ raise RequestError.new(response.json['message']) if ![200,201].include?(response.code)
56
+ response.json
57
+ end
58
+
59
+ end
55
60
  end
@@ -3,7 +3,11 @@ module MpApi
3
3
  class PaymentMethod
4
4
 
5
5
  def self.find_by_first_six_digits(first_six_digits, credit: true)
6
- response = Client.new.search_payment_methods(build_query(first_six_digits))
6
+ begin
7
+ response = Client.new.search_payment_methods(build_query(first_six_digits))
8
+ rescue RuntimeError => e
9
+ e.message == 'Too many retries' ? raise(TooManyRequestsError) : raise(e)
10
+ end
7
11
  new(**build_hash(response, credit))
8
12
  end
9
13
 
data/lib/mp_api/token.rb CHANGED
@@ -25,8 +25,12 @@ module MpApi
25
25
  end
26
26
 
27
27
  def create
28
- response = Client.new.create_token(JSON.dump(build_json))
29
- self.class.new(**self.class.build_hash(response))
28
+ begin
29
+ response = Client.new.create_token(JSON.dump(build_json))
30
+ self.class.new(**self.class.build_hash(response))
31
+ rescue RuntimeError => e
32
+ e.message == 'Too many retries' ? raise(TooManyRequestsError) : raise(e)
33
+ end
30
34
  end
31
35
 
32
36
  def self.build_hash(response)
@@ -1,5 +1,5 @@
1
- # frozen_string_literal: true
2
-
3
- module MpApi
4
- VERSION = "0.1.8"
5
- end
1
+ # frozen_string_literal: true
2
+
3
+ module MpApi
4
+ VERSION = "0.1.9"
5
+ end
data/lib/mp_api.rb CHANGED
@@ -15,6 +15,12 @@ module MpApi
15
15
  end
16
16
  end
17
17
 
18
+ class RequestError < StandardError
19
+ def initialize(message="Request error")
20
+ super(message)
21
+ end
22
+ end
23
+
18
24
  class Configuration
19
25
  attr_accessor :access_token, :public_key
20
26
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mp_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - caio
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-07 00:00:00.000000000 Z
11
+ date: 2023-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ac
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  - !ruby/object:Gem::Version
65
65
  version: '0'
66
66
  requirements: []
67
- rubygems_version: 3.4.19
67
+ rubygems_version: 3.4.6
68
68
  signing_key:
69
69
  specification_version: 4
70
70
  summary: ''