Class: Conekta::Requestor

Inherits:
Object
  • Object
show all
Includes:
Sys
Defined in:
lib/conekta/requestor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil) ⇒ Requestor

Returns a new instance of Requestor.



11
12
13
14
15
16
17
# File 'lib/conekta/requestor.rb', line 11

def initialize(api_key=nil)
  if (api_key.nil?)
    @api_key = Conekta.api_key
  else
    @api_key = api_key
  end
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



9
10
11
# File 'lib/conekta/requestor.rb', line 9

def api_key
  @api_key
end

Instance Method Details

#api_url(_url = '') ⇒ Object



19
20
21
22
# File 'lib/conekta/requestor.rb', line 19

def api_url(_url='')
  api_base = Conekta.api_base
  api_base + _url
end

#request(meth, _url, params = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/conekta/requestor.rb', line 24

def request(meth, _url, params = nil)
  _url = self.api_url(_url)
  meth = meth.downcase

  begin
    connection = build_connection(_url, params)
    response = connection.method(meth).call do |req|
      (if meth == :get then req.params = params else req.body = params.to_json end) if params
    end
  rescue Exception => e
    raise Error.error_handler({}, nil)
  end

  json_response = JSON.parse(response.body)
  raise Error.error_handler(json_response, response.status) if response.status != 200
  json_response
end