Class: AcceptOn::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/accepton/request.rb

Constant Summary collapse

URLS =
{
  development: 'http://checkout.accepton.dev',
  staging: 'https://staging-checkout.accepton.com',
  production: 'https://checkout.accepton.com'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, request_method, path, options = {}) ⇒ Accepton::Request

Parameters:

  • client (AcceptOn::Client)
  • request_method (String, Symbol)
  • path (String)
  • options (Hash) (defaults to: {})


21
22
23
24
25
26
27
28
29
30
# File 'lib/accepton/request.rb', line 21

def initialize(client, request_method, path, options = {})
  options = default_options.merge(options)
  url = URLS[options.delete(:environment).to_sym]
  @client = client
  @request_method = request_method
  @uri = Addressable::URI.parse(path.start_with?('http') ? path : url + path)
  @options = options
  @path = @uri.path
  @headers = AcceptOn::Headers.new(@client).request_headers
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



14
15
16
# File 'lib/accepton/request.rb', line 14

def client
  @client
end

#headersObject

Returns the value of attribute headers.



14
15
16
# File 'lib/accepton/request.rb', line 14

def headers
  @headers
end

#optionsObject

Returns the value of attribute options.



14
15
16
# File 'lib/accepton/request.rb', line 14

def options
  @options
end

#pathObject

Returns the value of attribute path.



14
15
16
# File 'lib/accepton/request.rb', line 14

def path
  @path
end

Instance Method Details

#performHashie::Mash

Returns if the request reutrns a success code.

Returns:

  • (Hashie::Mash)

    if the request reutrns a success code

Raises:



34
35
36
37
38
39
# File 'lib/accepton/request.rb', line 34

def perform
  options_key = @request_method == :get ? :params : :form
  response = HTTP.with(@headers).public_send(@request_method, @uri.to_s, options_key => @options)
  response_body = Hashie::Mash.new(response.parse)
  fail_or_return_response_body(response_body, response.code)
end