Module: Hyperwallet
- Defined in:
- lib/hyperwallet.rb,
lib/hyperwallet/user.rb,
lib/hyperwallet/util.rb,
lib/hyperwallet/payment.rb,
lib/hyperwallet/program.rb,
lib/hyperwallet/version.rb,
lib/hyperwallet/hyperwallet_error.rb,
lib/hyperwallet/hyperwallet_object.rb
Defined Under Namespace
Modules: Util Classes: APIError, AuthenticationError, HyperwalletError, HyperwalletObject, InvalidRequestError, Payment, Program, User
Constant Summary collapse
- VERSION =
"0.1.1"
Class Attribute Summary collapse
-
.api_base ⇒ Object
Returns the value of attribute api_base.
-
.api_password ⇒ Object
Returns the value of attribute api_password.
-
.api_user ⇒ Object
Returns the value of attribute api_user.
-
.debug ⇒ Object
Returns the value of attribute debug.
Class Method Summary collapse
- .api_url(operation = '', api_version = 3) ⇒ Object
- .execute_request(opts) ⇒ Object
- .handle_api_error(rcode, rbody) ⇒ Object
- .parse(response) ⇒ Object
- .request(method, url, params = {}, headers = {}, api_version = 3) ⇒ Object
Class Attribute Details
.api_base ⇒ Object
Returns the value of attribute api_base.
17 18 19 |
# File 'lib/hyperwallet.rb', line 17 def api_base @api_base end |
.api_password ⇒ Object
Returns the value of attribute api_password.
17 18 19 |
# File 'lib/hyperwallet.rb', line 17 def api_password @api_password end |
.api_user ⇒ Object
Returns the value of attribute api_user.
17 18 19 |
# File 'lib/hyperwallet.rb', line 17 def api_user @api_user end |
.debug ⇒ Object
Returns the value of attribute debug.
17 18 19 |
# File 'lib/hyperwallet.rb', line 17 def debug @debug end |
Class Method Details
.api_url(operation = '', api_version = 3) ⇒ Object
20 21 22 |
# File 'lib/hyperwallet.rb', line 20 def self.api_url(operation='', api_version = 3) "#{api_base}/rest/v#{api_version}#{operation}" end |
.execute_request(opts) ⇒ Object
59 60 61 62 |
# File 'lib/hyperwallet.rb', line 59 def self.execute_request(opts) puts "REQUEST: #{opts.to_s}" if debug RestClient::Request.execute(opts) end |
.handle_api_error(rcode, rbody) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/hyperwallet.rb', line 64 def self.handle_api_error(rcode, rbody) case rcode when 400, 404 raise InvalidRequestError.new("Your request is invalid: #{rbody.inspect}", rcode, rbody) when 401 raise AuthenticationError.new("Your API user or password is invalid: #{rbody.inspect}", rcode, rbody) else raise APIError.new("API Error: #{rbody.inspect}", rcode, rbody) end end |
.parse(response) ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/hyperwallet.rb', line 75 def self.parse(response) puts "RESPONSE: #{response.body}" if debug return {} if response.body.empty? begin MultiJson.load(response.body) rescue MultiJson::DecodeError raise APIError.new("Invalid response from the API: #{response.body.inspect}") end end |
.request(method, url, params = {}, headers = {}, api_version = 3) ⇒ Object
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 |
# File 'lib/hyperwallet.rb', line 24 def self.request(method, url, params = {}, headers = {}, api_version = 3) http_method = method.to_s.downcase.to_sym case http_method when :post, :put headers[:content_type] ||= "application/json" payload = params.to_json else # Make params into GET parameters url += "#{URI.parse(url).query ? '&' : '?'}#{uri_encode(params)}" if params && params.any? payload = nil end request_opts = { :headers => headers, :method => method, :verify_ssl => false, :url => api_url(url, api_version), :user => api_user, :password => api_password, :payload => payload } begin response = execute_request(request_opts) handle_api_error(response.code, response.body) unless [200, 201, 204].include?(response.code.to_i) rescue RestClient::ExceptionWithResponse => e if rcode = e.http_code and rbody = e.http_body handle_api_error(rcode, rbody) else raise end end parse(response) end |