Class: GMO::Payment::API

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

Direct Known Subclasses

RemittanceAPI, ShopAPI, ShopAndSiteAPI, SiteAPI

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



21
22
23
# File 'lib/gmo.rb', line 21

def initialize(options = {})
  @host = options[:host]
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



24
25
26
# File 'lib/gmo.rb', line 24

def host
  @host
end

Instance Method Details

#api(path, args = {}, verb = "post", options = {}) {|body| ... } ⇒ Object

Yields:

  • (body)


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
# File 'lib/gmo.rb', line 26

def api(path, args = {}, verb = "post", options = {}, &error_checking_block)
  # Setup args for make_request
  path = "/payment/#{path}" unless path =~ /^\//
  options.merge!({ :host => @host })
  # Make request via the provided service
  result = GMO.make_request path, args, verb, options
  # Check for any 500 server errors before parsing the body
  if result.status >= 500
    error_detail = {
      :http_status => result.status.to_i,
      :body        => result.body,
    }
    raise GMO::Payment::ServerError.new(result.body, error_detail)
  end
  # Transform the body to Hash
  # "ACS=1&ACSUrl=url" => { "ACS" => "1", ACSUrl => "url" }
  key_values = result.body.to_s.split('&').map { |str| str.split('=', 2) }.flatten
  response = Hash[*key_values]
  # converting to UTF-8
  body = response = Hash[response.map { |k,v| [k, NKF.nkf('-w',v)] }]
  # Check for errors if provided a error_checking_block
  yield(body) if error_checking_block
  # Return result
  if options[:http_component]
    result.send options[:http_component]
  else
    body
  end
end

#get_request(name, args = {}, options = {}) ⇒ Object Also known as: get!

gmo.get_request(“EntryTran.idPass”, => “bar”) GET /EntryTran.idPass with params foo=bar



58
59
60
# File 'lib/gmo.rb', line 58

def get_request(name, args = {}, options = {})
  api_call(name, args, "get", options)
end

#post_request(name, args = {}, options = {}) ⇒ Object Also known as: post!

gmo.post_request(“EntryTran.idPass”, => “bar”) POST /EntryTran.idPass with params foo=bar



65
66
67
68
# File 'lib/gmo.rb', line 65

def post_request(name, args = {}, options = {})
  args = associate_options_to_gmo_params args
  api_call(name, args, "post", options)
end