Class: MyMoip::Request

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/mymoip/request.rb

Direct Known Subclasses

PaymentRequest, TransparentRequest

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Request

Returns a new instance of Request.



7
8
9
# File 'lib/mymoip/request.rb', line 7

def initialize(id)
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/mymoip/request.rb', line 5

def id
  @id
end

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/mymoip/request.rb', line 5

def response
  @response
end

Instance Method Details

#api_call(params, opts = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mymoip/request.rb', line 11

def api_call(params, opts = {})
  opts[:logger]   ||= MyMoip.logger
  opts[:username] ||= MyMoip.token
  opts[:password] ||= MyMoip.key

  unless opts[:username].present? && opts[:password].present?
    MyMoip.ensure_key_and_token_set!
  end

  opts[:logger].info  "New #{self.class} being sent to MoIP."
  opts[:logger].debug "#{self.class} of ##{@id} with #{params.inspect}"

  url = MyMoip.api_url + params.delete(:path)
  params[:basic_auth] = { username: opts[:username], password: opts[:password] }

  @response = HTTParty.send params.delete(:http_method), url, params

  opts[:logger].debug "#{self.class} of ##{@id} to #{url} had response #{@response.inspect}"
end