class GmoPayment::Client::Response
Attributes
@!attribute [r] called_method
Public Class Methods
@param [Symbol] called_method
@param [Net::HTTPResponse] response
# File lib/gmo_payment/client/response.rb, line 8 def initialize(called_method, response) @called_method = called_method @response = response end
Public Instance Methods
Convert {#prebody} keys to Symbol that is {GmoPayment::GLOSSARY} key.
@return [Hash]
# File lib/gmo_payment/client/response.rb, line 119 def body self.prebody.each_with_object({}) do |(key, value), hash| hash[GmoPayment::GLOSSARY.key(key) || key] = value end end
Whether {#prebody} which of Hash values should return array or not. If called {GmoPayment::Api#search_card}, it will return true.
# File lib/gmo_payment/client/response.rb, line 18 def called_return_array? @called_method == :search_card end
When generating {#prebody}, whether to use {#split_encoded_raw_body} or not. If called {GmoPayment::Api#search_trade_btc}, it will return true.
# File lib/gmo_payment/client/response.rb, line 24 def called_split_normal? @called_method != :search_trade_btc end
Encode {#raw_body} Shift_JIS to UTF-8.
@return [String]
# File lib/gmo_payment/client/response.rb, line 63 def encoded_raw_body self.raw_body.encode('UTF-8', 'Shift_JIS', :invalid => :replace, :undef => :replace) end
Whether response body has “ErrCode” or not.
# File lib/gmo_payment/client/response.rb, line 131 def error? self.body.keys.include?(:err_code) end
HTTP status code.
@return [Integer]
# File lib/gmo_payment/client/response.rb, line 42 def http_code @response.code.to_i end
Whether HTTP response is not 2xx.
# File lib/gmo_payment/client/response.rb, line 126 def http_error? !(200..299).include?(http_code) end
HTTP response header.
@return [Hash]
# File lib/gmo_payment/client/response.rb, line 31 def http_header hash = {} @response.each do |key, value| hash[key] = value end hash end
HTTP status message.
@return [String]
# File lib/gmo_payment/client/response.rb, line 49 def http_message @response.message.to_s end
Convert response body to an easy-to-use format.
@return [Hash]
Hash<String, Array> if {#encoded_raw_body} has "ErrCode" or {#called_return_array?} is true. The other cases, it will return Hash<String, String>.
# File lib/gmo_payment/client/response.rb, line 105 def prebody array = called_split_normal? ? self.split_encoded_raw_body : self.split_encoded_raw_body! array.each_with_object({}) do |(key, value), hash| if called_return_array? || ['ErrCode', 'ErrInfo'].include?(key) hash[key] = value else hash[key] = value.join('|') end end end
Reponse body as it is.
@return [String]
# File lib/gmo_payment/client/response.rb, line 56 def raw_body @response.body.to_s end
Split {#encoded_raw_body} by {GmoPayment::GLOSSARY} keys. It works like URI.decode_www_form.
@example
"AccessID=xxx&AccessPass=zzz-1|zzz-2&PayTimes=" # => [["AccessID", ["xxx"]], ["AccessPass", ["zzz-1", "zzz-2"]]] # PayTimes is unavailable.
@return [Array]
# File lib/gmo_payment/client/response.rb, line 75 def split_encoded_raw_body return_array = [] array = self.encoded_raw_body.split(/(&?#{GmoPayment::GLOSSARY.values.join('=)|(&?')}=)/).values_at(1..-1) array.each_slice(2) do |(key, value)| next if value.nil? || value.empty? return_array << [key.gsub(/[&=]/, ''), value.split('|')] end return_array end
Split {#encoded_raw_body} by “&” and “=”. It works like URI.decode_www_form.
@example
"A=a&B=&C=c-1|c-2" # => [["A", ["a"]], ["C", ["c-1", "c-2"]]] # B is unavailable.
@return [Array]
# File lib/gmo_payment/client/response.rb, line 93 def split_encoded_raw_body! self.encoded_raw_body.split('&').map do |elem| key, value = elem.split('=', 2) [key, value.split('|')] unless value.nil? || value.empty? end.compact end