pagarme 1.9.2 → 1.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/Gemfile.lock +1 -1
- data/lib/pagarme.rb +11 -4
- data/lib/pagarme/errors.rb +23 -23
- data/lib/pagarme/model.rb +50 -50
- data/lib/pagarme/object.rb +135 -135
- data/lib/pagarme/plan.rb +4 -4
- data/lib/pagarme/request.rb +58 -58
- data/lib/pagarme/subscription.rb +34 -34
- data/lib/pagarme/transaction.rb +15 -8
- data/lib/pagarme/transaction_common.rb +81 -81
- data/lib/pagarme/util.rb +12 -9
- data/pagarme.gemspec +1 -1
- data/test/pagarme/object.rb +17 -17
- data/test/pagarme/plan.rb +9 -9
- data/test/pagarme/subscription.rb +61 -61
- data/test/pagarme/transaction.rb +158 -148
- data/test/test_helper.rb +76 -76
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8641743ec32b982257e8fdd31df4acedcaf111a5
|
4
|
+
data.tar.gz: dad121632d7f8073b6ba047788154cbf965aa775
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e5dce369112fdd3d218700340bd65f912c9b2c9a27fc5763f544e95aebf8f22910a78a5445854e2f49007c2d33df29c82dd4f4a293df8773ade39c5ea938199
|
7
|
+
data.tar.gz: 2446887bd86e88057529c3ef18169bdbb36f47c21de44d17f1aa6aa4331a7e84431440d51daa92adbdbe8f6c9e0457d15a2990a866afeaef522158d7d3ed3b94
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
data/lib/pagarme.rb
CHANGED
@@ -14,11 +14,18 @@ require 'pagarme/errors'
|
|
14
14
|
|
15
15
|
module PagarMe
|
16
16
|
@@api_key = nil
|
17
|
-
@@api_endpoint = 'https://api.pagar.me'
|
18
|
-
@@api_version = '1'
|
17
|
+
@@api_endpoint = 'https://api.pagar.me/1'
|
19
18
|
@@live = true
|
20
19
|
@@api_card_encryption_public_key = File.join(File.dirname(__FILE__), '..', 'certs/public_key.pem')
|
21
20
|
|
21
|
+
def self.api_endpoint=(api_endpoint)
|
22
|
+
@@api_endpoint = api_endpoint
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.api_endpoint
|
26
|
+
@@api_endpoint
|
27
|
+
end
|
28
|
+
|
22
29
|
def self.api_key=(api_key)
|
23
30
|
@@api_key = api_key
|
24
31
|
end
|
@@ -32,10 +39,10 @@ module PagarMe
|
|
32
39
|
end
|
33
40
|
|
34
41
|
def self.full_api_url(relative_path)
|
35
|
-
|
42
|
+
"#{@@api_endpoint}#{relative_path}"
|
36
43
|
end
|
37
44
|
|
38
45
|
def self.validate_fingerprint(id, fingerprint)
|
39
|
-
|
46
|
+
Digest::SHA1.hexdigest(id.to_s + "#" + @@api_key) == fingerprint
|
40
47
|
end
|
41
48
|
end
|
data/lib/pagarme/errors.rb
CHANGED
@@ -1,30 +1,30 @@
|
|
1
1
|
module PagarMe
|
2
2
|
class PagarMeError < StandardError
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
attr_accessor :message
|
4
|
+
attr_accessor :parameter_name
|
5
|
+
attr_accessor :type
|
6
|
+
attr_accessor :url
|
7
|
+
attr_accessor :errors
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
9
|
+
def initialize(message = "", parameter_name = "", type = "", url = "")
|
10
|
+
self.message = message
|
11
|
+
self.type = type
|
12
|
+
self.parameter_name = parameter_name
|
13
|
+
self.errors = []
|
14
|
+
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
16
|
+
def self.initFromServerResponse(response = {})
|
17
|
+
object = self.new
|
18
|
+
response['errors'].map do |error|
|
19
|
+
object.message += error['message'] + ', '
|
20
|
+
object.errors << PagarMeError.new(error['message'], error['parameter_name'], error['type'], response['url'])
|
21
|
+
end
|
22
|
+
object
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_s
|
26
|
+
"#{self.class.to_s} - #{message}"
|
27
|
+
end
|
28
28
|
end
|
29
29
|
|
30
30
|
class RequestError < PagarMeError
|
data/lib/pagarme/model.rb
CHANGED
@@ -1,64 +1,64 @@
|
|
1
1
|
module PagarMe
|
2
2
|
class Model < PagarMeObject
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
def self.class_name
|
5
|
+
self.name.split('::')[-1]
|
6
|
+
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
def self.url()
|
9
|
+
if self == Model
|
10
|
+
raise PagarMeError.new('Cant request for model')
|
11
|
+
end
|
12
|
+
"/#{CGI.escape(class_name.downcase)}s"
|
13
|
+
end
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
def url
|
16
|
+
raise PagarMeError.new("ID invalido", 'id') if !self.id
|
17
|
+
"#{self.class.url}/#{CGI.escape(id.to_s)}"
|
18
|
+
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
20
|
+
def create
|
21
|
+
request = PagarMe::Request.new(self.class.url, 'POST')
|
22
|
+
request.parameters = self.to_hash
|
23
|
+
response = request.run
|
24
|
+
update(response)
|
25
|
+
end
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
27
|
+
def save
|
28
|
+
request = PagarMe::Request.new(self.url, 'PUT')
|
29
|
+
request.parameters = self.unsaved_values
|
30
|
+
response = request.run
|
31
|
+
update(response)
|
32
|
+
end
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
34
|
+
def self.find_by_id(id)
|
35
|
+
request = PagarMe::Request.new(self.url + "/#{id}", 'GET')
|
36
|
+
response = request.run
|
37
|
+
PagarMe::Util.convert_to_pagarme_object(response)
|
38
|
+
end
|
39
39
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
request = PagarMe::Request.new(self.url, 'GET')
|
44
|
-
|
45
|
-
request.parameters = hash
|
46
|
-
request.parameters.merge!({:page => page, :count => count})
|
47
|
-
response = request.run
|
48
|
-
PagarMe::Util.convert_to_pagarme_object(response)
|
49
|
-
end
|
40
|
+
def self.find_by(hash, page = 1, count = 10)
|
41
|
+
raise RequestError.new("Invalid page count") if page < 1 or count < 1
|
50
42
|
|
51
|
-
|
52
|
-
raise RequestError.new("Invalid page count") if page < 1 or count < 1
|
43
|
+
request = PagarMe::Request.new(self.url, 'GET')
|
53
44
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
45
|
+
request.parameters = hash
|
46
|
+
request.parameters.merge!({:page => page, :count => count})
|
47
|
+
response = request.run
|
48
|
+
PagarMe::Util.convert_to_pagarme_object(response)
|
49
|
+
end
|
59
50
|
|
60
|
-
|
61
|
-
|
62
|
-
|
51
|
+
def self.all(page = 1, count = 10)
|
52
|
+
raise RequestError.new("Invalid page count") if page < 1 or count < 1
|
53
|
+
|
54
|
+
request = PagarMe::Request.new(url, 'GET')
|
55
|
+
request.parameters = {
|
56
|
+
:page => page,
|
57
|
+
:count => count
|
58
|
+
}
|
59
|
+
|
60
|
+
response = request.run
|
61
|
+
response.map { |obj_response| self.new(obj_response) }
|
62
|
+
end
|
63
63
|
end
|
64
64
|
end
|
data/lib/pagarme/object.rb
CHANGED
@@ -4,140 +4,140 @@ require 'set'
|
|
4
4
|
module PagarMe
|
5
5
|
class PagarMeObject
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
7
|
+
def initialize(response = {})
|
8
|
+
#init @attributes - which are the attributes in the object
|
9
|
+
@attributes = {}
|
10
|
+
|
11
|
+
# filters - which are the filters that will be called when appropriate , such as before_set
|
12
|
+
@filters = {}
|
13
|
+
|
14
|
+
# Values that were changed in the object but weren't saved
|
15
|
+
@unsaved_values = Set.new
|
16
|
+
|
17
|
+
#Update object
|
18
|
+
update(response)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.build(attributes)
|
22
|
+
object = self.new(attributes)
|
23
|
+
return object
|
24
|
+
end
|
25
|
+
|
26
|
+
def update(attributes)
|
27
|
+
|
28
|
+
removed = Set.new(@attributes.keys - attributes.keys)
|
29
|
+
added = Set.new(attributes.keys - @attributes.keys)
|
30
|
+
|
31
|
+
instance_eval do
|
32
|
+
remove_attribute(removed)
|
33
|
+
add_attribute(added)
|
34
|
+
end
|
35
|
+
|
36
|
+
removed.each do |key|
|
37
|
+
@attributes.delete(key)
|
38
|
+
@unsaved_values.delete(key)
|
39
|
+
end
|
40
|
+
|
41
|
+
attributes.each do |key, value|
|
42
|
+
@attributes[key] = Util.convert_to_pagarme_object(value)
|
43
|
+
@unsaved_values.delete(key)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def each(&block)
|
48
|
+
@attributes.each(&block)
|
49
|
+
end
|
50
|
+
|
51
|
+
def []=(key,value)
|
52
|
+
@attributes[key] = value
|
53
|
+
end
|
54
|
+
|
55
|
+
def [](key)
|
56
|
+
@attributes[key.to_sym]
|
57
|
+
end
|
58
|
+
|
59
|
+
def unsaved_values
|
60
|
+
values = {}
|
61
|
+
@unsaved_values.each do |k|
|
62
|
+
if(@attributes[k].kind_of?(PagarMeObject))
|
63
|
+
values[k] = @attributes[k].unsaved_values
|
64
|
+
else
|
65
|
+
values[k] = @attributes[k]
|
66
|
+
end
|
67
|
+
end
|
68
|
+
values
|
69
|
+
end
|
70
|
+
|
71
|
+
def to_hash
|
72
|
+
ret_attributes = {}
|
73
|
+
@attributes.each do |k,v|
|
74
|
+
if @attributes[k].kind_of?(PagarMeObject)
|
75
|
+
ret_attributes[k] = @attributes[k].to_hash if @attributes[k].kind_of?(PagarMeObject)
|
76
|
+
else
|
77
|
+
ret_attributes[k] = @attributes[k]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
return ret_attributes
|
81
|
+
end
|
82
|
+
|
83
|
+
protected
|
84
|
+
|
85
|
+
def metaclass
|
86
|
+
class << self; self; end
|
87
|
+
end
|
88
|
+
|
89
|
+
def remove_attribute(keys)
|
90
|
+
metaclass.instance_eval do
|
91
|
+
keys.each do |key|
|
92
|
+
key_sym = :"#{key}="
|
93
|
+
remove_method(key) if method_defined?(key)
|
94
|
+
remove_method(key_sym) if method_defined?(key_sym)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def add_attribute(keys)
|
100
|
+
metaclass.instance_eval do
|
101
|
+
keys.each do |key|
|
102
|
+
key_set = "#{key}="
|
103
|
+
define_method(key) { @attributes[key] }
|
104
|
+
define_method(key_set) do |value|
|
105
|
+
if @filters[key]
|
106
|
+
@filters[key].each do |meth|
|
107
|
+
if methods.include?(meth)
|
108
|
+
@attributes[key] = method(meth).call(value)
|
109
|
+
@unsaved_values.add(key)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
else
|
113
|
+
@attributes[key] = value
|
114
|
+
@unsaved_values.add(key)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def before_set_filter(attribute, method)
|
122
|
+
@filters[attribute.to_sym] = Array.new unless @filters[attribute.to_sym]
|
123
|
+
@filters[attribute.to_sym] << method.to_sym
|
124
|
+
end
|
125
|
+
|
126
|
+
def method_missing(name, *args)
|
127
|
+
if name.to_s.end_with?('=')
|
128
|
+
attr = name.to_s[0...-1].to_sym
|
129
|
+
add_attribute([attr])
|
130
|
+
begin
|
131
|
+
mth = method(name)
|
132
|
+
rescue NameError
|
133
|
+
raise NoMethodError.new("O atributo #{name} nao e permitido.")
|
134
|
+
end
|
135
|
+
return mth.call(args[0])
|
136
|
+
else
|
137
|
+
if @attributes.has_key?(name)
|
138
|
+
return @attributes[name]
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
142
|
end
|
143
143
|
end
|