Module: Hyperwallet

Defined in:
lib/hyperwallet.rb,
lib/hyperwallet/user.rb,
lib/hyperwallet/util.rb,
lib/hyperwallet/payment.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, User

Constant Summary collapse

VERSION =
"0.2.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_baseObject

Returns the value of attribute api_base.



16
17
18
# File 'lib/hyperwallet.rb', line 16

def api_base
  @api_base
end

.api_passwordObject

Returns the value of attribute api_password.



16
17
18
# File 'lib/hyperwallet.rb', line 16

def api_password
  @api_password
end

.api_userObject

Returns the value of attribute api_user.



16
17
18
# File 'lib/hyperwallet.rb', line 16

def api_user
  @api_user
end

Class Method Details

.api_url(operation = '', api_version = 3) ⇒ Object



19
20
21
# File 'lib/hyperwallet.rb', line 19

def self.api_url(operation='', api_version = 3)
  "#{api_base}/rest/v#{api_version}#{operation}"
end

.execute_request(opts) ⇒ Object



61
62
63
# File 'lib/hyperwallet.rb', line 61

def self.execute_request(opts)
  RestClient::Request.execute(opts)
end

.handle_api_error(rcode, rbody) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/hyperwallet.rb', line 65

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



76
77
78
79
80
81
82
# File 'lib/hyperwallet.rb', line 76

def self.parse(response)
  begin
    response = 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



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
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/hyperwallet.rb', line 23

def self.request(method, url, params = {}, headers = {}, api_version = 3)
  http_method = method.to_s.downcase.to_sym
  case http_method
  when :get, :head, :delete
    # Make params into GET parameters
    url += "#{URI.parse(url).query ? '&' : '?'}#{uri_encode(params)}" if params && params.any?
    payload = nil
  else
    payload = params.is_a?(String) ? params : uri_encode(params)
    if http_method == :post
      payload = params.to_json
      headers[:content_type] ||= "application/json"
    end
  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].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