pagarme 0.15 → 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 44500eb2b6f419659836569ebed302b4ad53fdc2
4
+ data.tar.gz: 9fd134b080deae4a281459288df9b4775f584bc5
5
+ SHA512:
6
+ metadata.gz: d9b3164829fee223d759a326148a7384408dc9755cfe652f0564c7c3947a8b90b3e0bb722490216d019440c5494429d9325fb501383582a1e39f7c49414fcd87
7
+ data.tar.gz: b64dccc95beff0f54b9f0c708ad76fb74a78fca7df5b397b74faed2d3045735ec45178fbe9c1bdb6b79ff408fb3cafebaafcd21ea0117fa89966c9c3f00159c8
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pagarme (1.0)
5
+ multi_json
6
+ rest-client
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (4.0.0)
12
+ i18n (~> 0.6, >= 0.6.4)
13
+ minitest (~> 4.2)
14
+ multi_json (~> 1.3)
15
+ thread_safe (~> 0.1)
16
+ tzinfo (~> 0.3.37)
17
+ atomic (1.1.14)
18
+ bourne (1.5.0)
19
+ mocha (>= 0.13.2, < 0.15)
20
+ i18n (0.6.5)
21
+ metaclass (0.0.1)
22
+ mime-types (2.0)
23
+ minitest (4.7.5)
24
+ mocha (0.14.0)
25
+ metaclass (~> 0.0.1)
26
+ multi_json (1.8.2)
27
+ rake (10.1.0)
28
+ rest-client (1.6.7)
29
+ mime-types (>= 1.16)
30
+ shoulda (3.4.0)
31
+ shoulda-context (~> 1.0, >= 1.0.1)
32
+ shoulda-matchers (~> 1.0, >= 1.4.1)
33
+ shoulda-context (1.1.5)
34
+ shoulda-matchers (1.5.6)
35
+ activesupport (>= 3.0.0)
36
+ bourne (~> 1.3)
37
+ test-unit (2.5.5)
38
+ thread_safe (0.1.3)
39
+ atomic
40
+ tzinfo (0.3.38)
41
+
42
+ PLATFORMS
43
+ ruby
44
+
45
+ DEPENDENCIES
46
+ bundler (~> 1.3)
47
+ pagarme!
48
+ rake
49
+ shoulda (~> 3.4.0)
50
+ test-unit
data/Rakefile CHANGED
@@ -1 +1,17 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ task :default => [:all]
4
+
5
+ task :test do
6
+ ret = true
7
+ Dir["test/**/*.rb"].each do |f|
8
+ ret = ret && ruby(f, '')
9
+ end
10
+ end
11
+
12
+ task :all do
13
+ Rake::Task["test"].invoke
14
+ require 'active_support/all'
15
+ Rake::Task["test"].reenable
16
+ Rake::Task["test"].invoke
17
+ end
@@ -1,48 +1,44 @@
1
- require File.join(File.dirname(__FILE__), '.', 'pagarme/transaction')
2
- require File.join(File.dirname(__FILE__), '.', 'pagarme/request')
3
- require File.join(File.dirname(__FILE__), '.', 'pagarme/errors')
1
+ require 'pagarme/pagarme_object'
2
+ require 'pagarme/util'
3
+ require 'pagarme/model'
4
+ require 'pagarme/transaction_common'
5
+ require 'pagarme/customer'
6
+ require 'pagarme/phone'
7
+ require 'pagarme/address'
8
+ require 'pagarme/subscription'
9
+ require 'pagarme/transaction'
10
+ require 'pagarme/plan'
11
+ require 'pagarme/request'
12
+ require 'pagarme/errors'
4
13
 
5
14
  module PagarMe
6
-
7
15
  @@api_key = nil
8
- @@api_endpoint = 'https://0.0.0.0:3001'
16
+ @@api_endpoint = 'https://api.pagar.me'
9
17
  @@api_version = '1'
10
18
  @@live = true
11
19
  @@api_card_encryption_public_key = File.join(File.dirname(__FILE__), '..', 'certs/public_key.pem')
12
20
 
13
21
  def self.api_key=(api_key)
14
- @@api_key = api_key
22
+ @@api_key = api_key
15
23
  end
16
24
 
17
25
  def self.api_key
18
- @@api_key
26
+ @@api_key
19
27
  end
20
28
 
21
29
  def self.api_card_encryption_public_key
22
- @@api_card_encryption_public_key
30
+ @@api_card_encryption_public_key
23
31
  end
24
32
 
25
33
  def self.full_api_url(relative_path)
26
- "#{@@api_endpoint}/#{@@api_version}#{relative_path}"
34
+ "#{@@api_endpoint}/#{@@api_version}#{relative_path}"
27
35
  end
28
36
 
29
37
  def self.live
30
- @@live
38
+ @@live
31
39
  end
32
40
 
33
41
  def self.live=(live)
34
- @@live = live
42
+ @@live = live
35
43
  end
36
-
37
- # def self.request(method, url, parameters)
38
- # request_options = {
39
- # :method => method,
40
- # :url => self.full_api_url(url)
41
- # :headers => nil,
42
- # :open_timeout => 30,
43
- # :payload => payload,
44
- # :timeout => 80,
45
- # }
46
- # end
47
-
48
44
  end
@@ -0,0 +1,4 @@
1
+ module PagarMe
2
+ class Address < Model
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module PagarMe
2
+ class Customer < Model
3
+ end
4
+ end
@@ -1,11 +1,25 @@
1
1
  module PagarMe
2
2
  class PagarMeError < StandardError
3
- attr_reader :message
4
- attr_reader :original_error
3
+ attr_accessor :message
4
+ attr_accessor :parameter_name
5
+ attr_accessor :type
6
+ attr_accessor :url
7
+ attr_accessor :errors
5
8
 
6
- def initialize(message=nil, original_error=nil)
7
- @message = message
8
- @original_error = original_error
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
+
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
9
23
  end
10
24
 
11
25
  def to_s
@@ -13,7 +27,7 @@ module PagarMe
13
27
  end
14
28
  end
15
29
 
16
- class TransactionError < PagarMeError
30
+ class RequestError < PagarMeError
17
31
  end
18
32
 
19
33
  class ConnectionError < PagarMeError
@@ -0,0 +1,53 @@
1
+ module PagarMe
2
+ class Model < PagarMeObject
3
+
4
+ def self.class_name
5
+ self.name.split('::')[-1]
6
+ end
7
+
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
+
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
+
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
+
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
+
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
+
40
+ def self.all(page = 1, count = 10)
41
+ raise RequestError.new("Invalid page count") if page < 1 or count < 1
42
+
43
+ request = PagarMe::Request.new(url, 'GET')
44
+ request.parameters = {
45
+ :page => page,
46
+ :count => count
47
+ }
48
+
49
+ response = request.run
50
+ response.map { |obj_response| self.new(obj_response) }
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,141 @@
1
+ # encoding: utf-8
2
+ require 'set'
3
+
4
+ module PagarMe
5
+ class PagarMeObject
6
+
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
+ return @attributes[name] if @attributes.has_key?(name)
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,4 @@
1
+ module PagarMe
2
+ class Phone < Model
3
+ end
4
+ end
@@ -0,0 +1,56 @@
1
+ # encoding: utf-8
2
+
3
+ module PagarMe
4
+ class Plan < Model
5
+ def initialize(response = {})
6
+ super
7
+ before_set_filter :amount, :format_amount
8
+ end
9
+
10
+ def create
11
+ validate
12
+ super
13
+ end
14
+
15
+ def save
16
+ raise PagarMeError.new('O plano precisa estar criado para poder ser atualizado!', 'internal_error') unless self.id
17
+ super
18
+ end
19
+
20
+ def format_amount(amount)
21
+ if amount.kind_of?(String)
22
+ value = amount.gsub(/\./, "")
23
+ value = value.strip
24
+ value = value.match(/\d+/)[0]
25
+ amount = value
26
+ end
27
+ amount
28
+ end
29
+
30
+ private
31
+
32
+ def validate
33
+ error = PagarMeError.new
34
+ if !self[:amount] || self.amount.to_i <= 0
35
+ error.errors << PagarMeError.new('Valor invalido', 'amount')
36
+ end
37
+ if !self[:days] || self.days.to_i <= 0
38
+ error.errors << PagarMeError.new('Numero de dias invalido', 'days')
39
+ end
40
+ if !self[:name] || self.name.length <= 0
41
+ error.errors << PagarMeError.new('Nome invalido', 'name')
42
+ end
43
+ if self[:trial_days] && self.trial_days.to_i < 0
44
+ error.errors << PagarMeError.new('Dias de teste invalido', 'trial_days')
45
+ end
46
+
47
+ if(error.errors.any?)
48
+ error.message = error.errors.map {|e| e.message}
49
+ error.message = error.message.join(',')
50
+ raise error
51
+ else
52
+ nil
53
+ end
54
+ end
55
+ end
56
+ end