class GMO::Payment::API

Attributes

host[R]

Public Class Methods

new(options = {}) click to toggle source
   # File lib/gmo.rb
21 def initialize(options = {})
22   @host = options[:host]
23 end

Public Instance Methods

api(path, args = {}, verb = "post", options = {}) { |body| ... } click to toggle source
   # File lib/gmo.rb
26 def api(path, args = {}, verb = "post", options = {}, &error_checking_block)
27   # Setup args for make_request
28   path = "/payment/#{path}" unless path =~ /^\//
29   options.merge!({ :host => @host })
30   # Make request via the provided service
31   result = GMO.make_request path, args, verb, options
32   # Check for any 500 server errors before parsing the body
33   if result.status >= 500
34     error_detail = {
35       :http_status => result.status.to_i,
36       :body        => result.body,
37     }
38     raise GMO::Payment::ServerError.new(result.body, error_detail)
39   end
40   # Transform the body to Hash
41   if /\.json\Z/ =~ path
42     # Parse the body as JSON
43     parsed_result = ::JSON.parse(result.body)
44     response = parsed_result.is_a?(Array) ? parsed_result[0] : parsed_result
45   else
46     # Parse the body as Query string
47     # "ACS=1&ACSUrl=url" => { "ACS" => "1", ACSUrl => "url" }
48     key_values = result.body.to_s.split('&').map { |str| str.split('=', 2) }.flatten
49     response = Hash[*key_values]
50   end
51   # converting to UTF-8
52   body = response = Hash[response.map { |k,v| [k, NKF.nkf('-S -w',v)] }]
53   # Check for errors if provided a error_checking_block
54   yield(body) if error_checking_block
55   # Return result
56   if options[:http_component]
57     result.send options[:http_component]
58   else
59     body
60   end
61 end
get!(name, args = {}, options = {})
Alias for: get_request
get_request(name, args = {}, options = {}) click to toggle source

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

   # File lib/gmo.rb
65 def get_request(name, args = {}, options = {})
66   api_call(name, args, "get", options)
67 end
Also aliased as: get!
post!(name, args = {}, options = {})
Alias for: post_request
post_request(name, args = {}, options = {}) click to toggle source

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

   # File lib/gmo.rb
72 def post_request(name, args = {}, options = {})
73   args = associate_options_to_gmo_params args
74   api_call(name, args, "post", options)
75 end
Also aliased as: post!

Private Instance Methods

api_call(*args) click to toggle source
   # File lib/gmo.rb
89 def api_call(*args)
90   raise "Called abstract method: api_call"
91 end
assert_required_options(required, options) click to toggle source
   # File lib/gmo.rb
80 def assert_required_options(required, options)
81   missing = required.select { |param| options[param].nil? }
82   raise ArgumentError, "Required #{missing.join(', ')} were not provided." unless missing.empty?
83 end
associate_options_to_gmo_params(options) click to toggle source
   # File lib/gmo.rb
85 def associate_options_to_gmo_params(options)
86   Hash[options.map { |k, v| [GMO::Const::INPUT_PARAMS[k], v] }]
87 end