Class: Chargify::Base
Direct Known Subclasses
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Class Method Summary collapse
Instance Method Summary collapse
- #api_request(type, path, options = {}) ⇒ Object
- #attributes=(attrs) ⇒ Object
-
#initialize(options = {}) ⇒ Base
constructor
A new instance of Base.
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(={}) Chargify::Config.api_key = [:api_key] if [:api_key] Chargify::Config.subdomain = [:subdomain] if [:subdomain] @errors = [] self.attributes = attrs end |
Instance Attribute Details
#errors ⇒ Object (readonly)
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, ={}) self.base_uri "https://#{Chargify::Config.subdomain}.chargify.com" # This is to allow bang methods raise_errors = .delete(:raise_errors) # Build options hash for HTTParty [:body] = [:body].to_json if [:body] [: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, ) 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, ={}) @errors = [] begin self.class.api_request(type, path, ) 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.] 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 |