conekta 0.3.3 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.gitignore +5 -11
- data/CHANGELOG +5 -0
- data/Gemfile +6 -1
- data/LICENSE.txt +22 -0
- data/README.md +59 -0
- data/Rakefile +1 -14
- data/conekta.gemspec +22 -22
- data/lib/conekta.rb +36 -290
- data/lib/conekta/card.rb +10 -34
- data/lib/conekta/charge.rb +14 -38
- data/lib/conekta/conekta_object.rb +64 -136
- data/lib/conekta/customer.rb +23 -25
- data/lib/conekta/error.rb +65 -0
- data/lib/conekta/event.rb +2 -2
- data/lib/conekta/operations/create.rb +19 -0
- data/lib/conekta/operations/create_member.rb +30 -0
- data/lib/conekta/operations/custom_action.rb +17 -0
- data/lib/conekta/operations/delete.rb +43 -0
- data/lib/conekta/operations/find.rb +28 -0
- data/lib/conekta/operations/update.rb +13 -0
- data/lib/conekta/operations/where.rb +28 -0
- data/lib/conekta/payment_method.rb +4 -0
- data/lib/conekta/plan.rb +8 -5
- data/lib/conekta/requestor.rb +55 -0
- data/lib/conekta/resource.rb +13 -0
- data/lib/conekta/subscription.rb +12 -26
- data/lib/conekta/token.rb +3 -2
- data/lib/conekta/util.rb +19 -91
- data/lib/conekta/version.rb +1 -1
- data/lib/ssl_data/ca_bundle.crt +66 -0
- data/spec/conekta_spec.rb +299 -808
- data/spec/spec_helper.rb +12 -0
- metadata +55 -72
- data/CONTRIBUTORS +0 -9
- data/Gemfile.lock +0 -57
- data/History.txt +0 -4
- data/LICENSE +0 -23
- data/README.rdoc +0 -30
- data/VERSION +0 -1
- data/bin/conekta-console +0 -7
- data/bin/test calls +0 -205
- data/gemfiles/default-with-activesupport.gemfile +0 -3
- data/gemfiles/json.gemfile +0 -4
- data/gemfiles/yajl.gemfile +0 -4
- data/lib/conekta/account.rb +0 -4
- data/lib/conekta/api_operations/create.rb +0 -16
- data/lib/conekta/api_operations/create_member.rb +0 -18
- data/lib/conekta/api_operations/delete.rb +0 -11
- data/lib/conekta/api_operations/list.rb +0 -16
- data/lib/conekta/api_operations/modify_member.rb +0 -12
- data/lib/conekta/api_operations/update.rb +0 -22
- data/lib/conekta/api_resource.rb +0 -33
- data/lib/conekta/errors/api_connection_error.rb +0 -4
- data/lib/conekta/errors/api_error.rb +0 -4
- data/lib/conekta/errors/authentication_error.rb +0 -4
- data/lib/conekta/errors/card_error.rb +0 -11
- data/lib/conekta/errors/conekta_error.rb +0 -20
- data/lib/conekta/errors/malformed_request_error.rb +0 -10
- data/lib/conekta/errors/parameter_validation_error.rb +0 -10
- data/lib/conekta/errors/resource_not_found_error.rb +0 -10
- data/lib/conekta/json.rb +0 -21
- data/lib/conekta/list_object.rb +0 -35
- data/lib/conekta/log.rb +0 -5
- data/lib/conekta/singleton_api_resource.rb +0 -20
- data/lib/data/ca-certificates.crt +0 -3918
- data/spec/conekta_with_active_support_spec.rb +0 -3
- data/spec/test_helper.rb +0 -338
data/lib/conekta/event.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
module Conekta
|
2
|
+
module Operations
|
3
|
+
module Create
|
4
|
+
module ClassMethods
|
5
|
+
def create(params)
|
6
|
+
requestor = Requestor.new
|
7
|
+
url = Util.types[self.class_name.downcase].url
|
8
|
+
response = requestor.request(:post, url, params)
|
9
|
+
instance = self.new
|
10
|
+
instance.load_from(response)
|
11
|
+
instance
|
12
|
+
end
|
13
|
+
end
|
14
|
+
def self.included(base)
|
15
|
+
base.extend(ClassMethods)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Conekta
|
2
|
+
module Operations
|
3
|
+
module CreateMember
|
4
|
+
def create_member(member, params)
|
5
|
+
url = self.url + "/" + member
|
6
|
+
member = member.to_sym
|
7
|
+
requestor = Requestor.new
|
8
|
+
response = requestor.request(:post, url, params)
|
9
|
+
if self.method(member).call and self.method(member).call.class.class_name == "ConektaObject"
|
10
|
+
arr = []
|
11
|
+
self.method(member).call.values.each do |(k,v)|
|
12
|
+
arr << v.to_hash
|
13
|
+
end
|
14
|
+
arr << response
|
15
|
+
self.method(member).call.load_from(arr)
|
16
|
+
self.load_from
|
17
|
+
instances = self.method(member).call
|
18
|
+
instance = instances.last
|
19
|
+
else
|
20
|
+
instance = Util.types[member.to_s].new()
|
21
|
+
instance.load_from(response)
|
22
|
+
self.create_attr(member.to_s, instance)
|
23
|
+
self.set_val(member.to_sym, instance)
|
24
|
+
self.load_from
|
25
|
+
end
|
26
|
+
instance
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Conekta
|
2
|
+
module Operations
|
3
|
+
module CustomAction
|
4
|
+
def custom_action(method, action=nil, params=nil)
|
5
|
+
requestor = Requestor.new
|
6
|
+
if action
|
7
|
+
url = self.url + "/" + action
|
8
|
+
else
|
9
|
+
url = self.url
|
10
|
+
end
|
11
|
+
response = requestor.request(method, url, params)
|
12
|
+
self.load_from(response)
|
13
|
+
self
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Conekta
|
2
|
+
module Operations
|
3
|
+
module Delete
|
4
|
+
def delete
|
5
|
+
self.custom_action(:delete, nil, nil)
|
6
|
+
self
|
7
|
+
end
|
8
|
+
def delete_member(parent, member)
|
9
|
+
self.custom_action(:delete, nil, nil)
|
10
|
+
parent = parent.to_sym
|
11
|
+
member = member.to_sym
|
12
|
+
obj = self.method(parent).call.method(member).call
|
13
|
+
if obj.class.class_name == "ConektaObject"
|
14
|
+
self.method(parent).call.method(member).call.each do |(k, v)|
|
15
|
+
if v.id == self.id
|
16
|
+
self.method(parent).call.method(member).call[k] = nil
|
17
|
+
# Shift hash array
|
18
|
+
shift = false
|
19
|
+
self.method(parent).call.method(member).call.each_with_index do |v,i|
|
20
|
+
if shift
|
21
|
+
self.method(parent).call.method(member).call.set_val(i-1,v[1])
|
22
|
+
self.method(parent).call.method(member).call[i-1] = v[1]
|
23
|
+
end
|
24
|
+
if v[1] == nil
|
25
|
+
shift = true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
n_members = self.method(parent).call.method(member).call.count
|
29
|
+
last_index = n_members - 1
|
30
|
+
# Remove last member because the hash array was shifted
|
31
|
+
self.method(parent).call.method(member).call.unset_key(last_index)
|
32
|
+
self.method(parent).call.method(member).call.delete(last_index)
|
33
|
+
break
|
34
|
+
end
|
35
|
+
end
|
36
|
+
else
|
37
|
+
self.create_attr(member.to_s, nil)
|
38
|
+
end
|
39
|
+
self
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Conekta
|
2
|
+
module Operations
|
3
|
+
module Find
|
4
|
+
module ClassMethods
|
5
|
+
def find(id)
|
6
|
+
instance = self.new(id)
|
7
|
+
url = instance.url
|
8
|
+
requestor = Requestor.new
|
9
|
+
response = requestor.request(:get, url)
|
10
|
+
instance.load_from(response)
|
11
|
+
instance
|
12
|
+
end
|
13
|
+
# <b>DEPRECATED:</b> Please use <tt>find</tt> instead.
|
14
|
+
def retrieve(id)
|
15
|
+
instance = self.new(id)
|
16
|
+
url = instance.url
|
17
|
+
requestor = Requestor.new
|
18
|
+
response = requestor.request(:get, url)
|
19
|
+
instance.load_from(response)
|
20
|
+
instance
|
21
|
+
end
|
22
|
+
end
|
23
|
+
def self.included(base)
|
24
|
+
base.extend(ClassMethods)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Conekta
|
2
|
+
module Operations
|
3
|
+
module Where
|
4
|
+
module ClassMethods
|
5
|
+
def where(params=nil)
|
6
|
+
instance = ConektaObject.new
|
7
|
+
url = Util.types[self.class_name.downcase].url
|
8
|
+
requestor = Requestor.new
|
9
|
+
response = requestor.request(:get, url)
|
10
|
+
instance.load_from(response)
|
11
|
+
instance
|
12
|
+
end
|
13
|
+
# <b>DEPRECATED:</b> Please use <tt>where</tt> instead.
|
14
|
+
def all(params=nil)
|
15
|
+
instance = ConektaObject.new
|
16
|
+
url = Util.types[self.class_name.downcase].url
|
17
|
+
requestor = Requestor.new
|
18
|
+
response = requestor.request(:get, url)
|
19
|
+
instance.load_from(response)
|
20
|
+
instance
|
21
|
+
end
|
22
|
+
end
|
23
|
+
def self.included(base)
|
24
|
+
base.extend(ClassMethods)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/conekta/plan.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
module Conekta
|
2
|
-
class Plan <
|
3
|
-
include Conekta::
|
4
|
-
include Conekta::
|
5
|
-
include Conekta::
|
6
|
-
include Conekta::
|
2
|
+
class Plan < Resource
|
3
|
+
include Conekta::Operations::Find
|
4
|
+
include Conekta::Operations::Where
|
5
|
+
include Conekta::Operations::Create
|
6
|
+
include Conekta::Operations::Delete
|
7
|
+
include Conekta::Operations::Update
|
8
|
+
include Conekta::Operations::CustomAction
|
9
|
+
include Conekta::Operations::CreateMember
|
7
10
|
end
|
8
11
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'base64'
|
3
|
+
module Conekta
|
4
|
+
class Requestor
|
5
|
+
require 'sys/uname'
|
6
|
+
include Sys
|
7
|
+
attr_reader :api_key
|
8
|
+
def initialize()
|
9
|
+
@api_key = Conekta.api_key
|
10
|
+
end
|
11
|
+
def api_url(url='')
|
12
|
+
api_base = Conekta.api_base
|
13
|
+
api_base + url
|
14
|
+
end
|
15
|
+
def request(meth, url, params=nil)
|
16
|
+
url = self.api_url(url)
|
17
|
+
meth = meth.downcase
|
18
|
+
begin
|
19
|
+
conn = Faraday.new(:ssl => {
|
20
|
+
:ca_file => File.dirname(__FILE__) + '/../ssl_data/ca-certificates.crt'
|
21
|
+
}) do |faraday|
|
22
|
+
faraday.adapter Faraday.default_adapter
|
23
|
+
end
|
24
|
+
response = conn.method(meth).call do |req|
|
25
|
+
req.url url
|
26
|
+
|
27
|
+
req.headers['X-Conekta-Client-User-Agent'] = set_headers.to_json
|
28
|
+
req.headers['User-Agent'] = 'Conekta/v1 RubyBindings/' + Conekta::VERSION
|
29
|
+
req.headers['Accept'] = "application/vnd.conekta-v#{Conekta.api_version}+json"
|
30
|
+
req.headers['Authorization'] = "Basic #{ Base64.encode64("#{self.api_key}" + ':')}"
|
31
|
+
if params
|
32
|
+
req.headers['Content-Type'] = 'application/json'
|
33
|
+
req.body = params.to_json
|
34
|
+
end
|
35
|
+
end
|
36
|
+
rescue Exception => e
|
37
|
+
Error.error_handler(e.message, "")
|
38
|
+
end
|
39
|
+
if response.status != 200
|
40
|
+
Error.error_handler(JSON.parse(response.body), response.status)
|
41
|
+
end
|
42
|
+
return JSON.parse(response.body)
|
43
|
+
end
|
44
|
+
private
|
45
|
+
def set_headers
|
46
|
+
headers = {}
|
47
|
+
headers[:bindings_version] = Conekta::VERSION
|
48
|
+
headers[:lang] = 'ruby'
|
49
|
+
headers[:lang_version] = RUBY_VERSION
|
50
|
+
headers[:publisher] = 'conekta'
|
51
|
+
headers[:uname] = Uname.uname
|
52
|
+
return headers
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Conekta
|
2
|
+
class Resource < ConektaObject
|
3
|
+
def self.url()
|
4
|
+
"/#{CGI.escape(self.class_name.downcase)}s"
|
5
|
+
end
|
6
|
+
def url
|
7
|
+
if id == nil || id == ""
|
8
|
+
raise Error.new('Could not get the id of ' + self.class.class_name + ' instance.')
|
9
|
+
end
|
10
|
+
self.class.url + "/" + id
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/conekta/subscription.rb
CHANGED
@@ -1,35 +1,21 @@
|
|
1
1
|
module Conekta
|
2
|
-
class Subscription <
|
3
|
-
include Conekta::
|
4
|
-
include Conekta::
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
11
|
-
|
12
|
-
def customer=(customer)
|
13
|
-
@customer = customer
|
14
|
-
end
|
15
|
-
|
16
|
-
def cancel
|
17
|
-
self.modify_member("subscription", "cancel")
|
2
|
+
class Subscription < Resource
|
3
|
+
include Conekta::Operations::Update
|
4
|
+
include Conekta::Operations::CustomAction
|
5
|
+
def url
|
6
|
+
if id == nil || id == ""
|
7
|
+
raise Error.new('Could not get the id of ' + self.class.class_name + ' instance.')
|
8
|
+
end
|
9
|
+
self.customer.url + "/subscription"
|
18
10
|
end
|
19
|
-
|
20
11
|
def pause
|
21
|
-
|
12
|
+
custom_action(:post, 'pause')
|
22
13
|
end
|
23
|
-
|
24
14
|
def resume
|
25
|
-
|
15
|
+
custom_action(:post, 'resume')
|
26
16
|
end
|
27
|
-
|
28
|
-
|
29
|
-
unless customer = self.customer
|
30
|
-
raise ParameterValidationError.new("Could not determine which URL to request: #{self.class} instance has invalid customer: #{customer.inspect}", 'customer')
|
31
|
-
end
|
32
|
-
"#{Conekta::Customer.url}/#{CGI.escape(customer.id)}/subscription"
|
17
|
+
def cancel
|
18
|
+
custom_action(:post, 'cancel')
|
33
19
|
end
|
34
20
|
end
|
35
21
|
end
|
data/lib/conekta/token.rb
CHANGED
data/lib/conekta/util.rb
CHANGED
@@ -1,103 +1,31 @@
|
|
1
1
|
module Conekta
|
2
2
|
module Util
|
3
|
-
def self.
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
res = {}
|
9
|
-
h.each { |k, v| res[k] = objects_to_ids(v) unless v.nil? }
|
10
|
-
res
|
11
|
-
when Array
|
12
|
-
h.map { |v| objects_to_ids(v) }
|
13
|
-
else
|
14
|
-
h
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.object_classes
|
19
|
-
@object_classes ||= {
|
3
|
+
def self.types
|
4
|
+
@types ||= {
|
5
|
+
'bank_transfer_payment' => PaymentMethod,
|
6
|
+
'card_payment' => PaymentMethod,
|
7
|
+
'cash_payment' => PaymentMethod,
|
20
8
|
'charge' => Charge,
|
21
|
-
'
|
9
|
+
'customer' => Customer,
|
22
10
|
'card' => Card,
|
23
11
|
'subscription' => Subscription,
|
24
|
-
'
|
25
|
-
'
|
12
|
+
'plan' => Plan,
|
13
|
+
'token' => Token,
|
14
|
+
'event' => Event
|
26
15
|
}
|
27
16
|
end
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
when Hash
|
34
|
-
# Try converting to a known object class. If none available, fall back to generic ConektaObject
|
35
|
-
object_classes.fetch(resp[:object], ConektaObject).construct_from(resp, api_key)
|
36
|
-
else
|
37
|
-
resp
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.file_readable(file)
|
42
|
-
# This is nominally equivalent to File.readable?, but that can
|
43
|
-
# report incorrect results on some more oddball filesystems
|
44
|
-
# (such as AFS)
|
45
|
-
begin
|
46
|
-
File.open(file) { |f| }
|
47
|
-
rescue
|
48
|
-
false
|
49
|
-
else
|
50
|
-
true
|
17
|
+
def self.convert_to_conekta_object(resp)
|
18
|
+
if resp.kind_of?(Hash) and resp.has_key?('object') and types[resp['object']]
|
19
|
+
instance = types[resp['object']].new()
|
20
|
+
instance.load_from(resp)
|
21
|
+
return instance
|
51
22
|
end
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
when Hash
|
57
|
-
new = {}
|
58
|
-
object.each do |key, value|
|
59
|
-
key = (key.to_sym rescue key) || key
|
60
|
-
new[key] = symbolize_names(value)
|
61
|
-
end
|
62
|
-
new
|
63
|
-
when Array
|
64
|
-
object.map { |value| symbolize_names(value) }
|
65
|
-
else
|
66
|
-
object
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def self.url_encode(key)
|
71
|
-
URI.escape(key.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
72
|
-
end
|
73
|
-
|
74
|
-
def self.flatten_params(params, parent_key=nil)
|
75
|
-
result = []
|
76
|
-
params.each do |key, value|
|
77
|
-
calculated_key = parent_key ? "#{parent_key}[#{url_encode(key)}]" : url_encode(key)
|
78
|
-
if value.is_a?(Hash)
|
79
|
-
result += flatten_params(value, calculated_key)
|
80
|
-
elsif value.is_a?(Array)
|
81
|
-
result += flatten_params_array(value, calculated_key)
|
82
|
-
else
|
83
|
-
result << [calculated_key, value]
|
84
|
-
end
|
85
|
-
end
|
86
|
-
result
|
87
|
-
end
|
88
|
-
|
89
|
-
def self.flatten_params_array(value, calculated_key)
|
90
|
-
result = []
|
91
|
-
value.each do |elem|
|
92
|
-
if elem.is_a?(Hash)
|
93
|
-
result += flatten_params(elem, calculated_key)
|
94
|
-
elsif elem.is_a?(Array)
|
95
|
-
result += flatten_params_array(elem, calculated_key)
|
96
|
-
else
|
97
|
-
result << ["#{calculated_key}[]", elem]
|
98
|
-
end
|
23
|
+
if resp.respond_to? :each
|
24
|
+
instance = ConektaObject.new
|
25
|
+
instance.load_from(resp)
|
26
|
+
return instance
|
99
27
|
end
|
100
|
-
|
28
|
+
return instance
|
101
29
|
end
|
102
30
|
end
|
103
31
|
end
|