Class: Chargify::Base
- Inherits:
-
Object
show all
- Includes:
- HTTParty
- Defined in:
- lib/chargify/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new instance of Base.
54
55
56
57
58
59
60
|
# File 'lib/chargify/base.rb', line 54
def initialize(options={})
Chargify::Config.api_key = options[:api_key] if options[:api_key]
Chargify::Config.subdomain = options[:subdomain] if options[:subdomain]
@errors = []
self.attributes = attrs
end
|
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
52
53
54
|
# File 'lib/chargify/base.rb', line 52
def errors
@errors
end
|
Class Method Details
.api_request(type, path, options = {}) ⇒ Object
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
|
# File 'lib/chargify/base.rb', line 11
def api_request(type, path, options={})
self.base_uri "https://#{Chargify::Config.subdomain}.chargify.com"
raise_errors = options.delete(:raise_errors)
options[:body] = options[:body].to_json if options[:body]
options[:basic_auth] = {:username => Chargify::Config.api_key, :password => 'x'}
Chargify::Config.logger.debug("[CHARGIFY] Sending #{self.base_uri}#{path} a payload of #{options[:body]}") if Chargify::Config.debug
begin
response = self.send(type.to_s, path, options)
rescue SocketError
raise(Chargify::Error::ConnectionFailed.new, "Failed to connect to payment gateway.")
end
case response.code.to_i
when 401
raise(Chargify::Error::AccessDenied.new(response), response.body)
when 403
raise(Chargify::Error::Forbidden.new(response), response.body)
when 422
raise(Chargify::Error::BadRequest.new(response), response.body)
when 404
raise(Chargify::Error::NotFound.new(response), response.body)
when 500
raise(Chargify::Error::ServerError.new(response), response.body)
when 504
raise(Chargify::Error::GatewayTimeout.new(response), response.body)
end
Chargify::Config.logger.debug("[CHARGIFY] Response from #{self.base_uri}#{path} was #{response.code}: #{response.body}") if Chargify::Config.debug
response
end
|
Instance Method Details
#api_request(type, path, options = {}) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/chargify/base.rb', line 68
def api_request(type, path, options={})
@errors = []
begin
self.class.api_request(type, path, options)
rescue Chargify::Error::Base => e
if e.response.is_a?(Hash)
if e.response.has_key?("errors")
@errors = [*e.response["errors"]["error"]]
else
@errors = [e.response.body]
end
else
@errors = [e.message]
end
raise
end
end
|
#attributes=(attrs) ⇒ Object
62
63
64
65
66
|
# File 'lib/chargify/base.rb', line 62
def attributes=(attrs)
attrs.each do |k, v|
self.send(:"#{k}=", v) if self.respond_to?(:"#{k}=")
end
end
|