Class: Gateway::Client
- Inherits:
-
Object
- Object
- Gateway::Client
- Defined in:
- lib/gateway/client.rb
Overview
Client
Constant Summary collapse
- METHOD_GET =
http method GET
'get'
- METHOD_POST =
http method POST
'post'
Instance Attribute Summary collapse
-
#config_data ⇒ Object
readonly
Returns the value of attribute config_data.
-
#request ⇒ Object
Returns the value of attribute request.
-
#test ⇒ Object
readonly
Returns the value of attribute test.
Instance Method Summary collapse
-
#initialize(request, token, test = false) ⇒ Client
constructor
Initialize.
-
#response ⇒ Hash
Send request to gateway and take response from it.
-
#url(query_params) ⇒ String
Generate url.
Constructor Details
#initialize(request, token, test = false) ⇒ Client
Initialize
29 30 31 32 33 34 35 36 37 |
# File 'lib/gateway/client.rb', line 29 def initialize(request, token, test = false) Gateway.config = JSON.parse(YAML.load_file("#{Gateway.root}/gateway/config.yml").to_json, object_class: OpenStruct) @request = request @token = token @test = test request_name = request.class.name.split('::').last @config_data = Gateway.config.api_methods[request_name] raise Gateway::StandardError.new("fill data in config.yml for method: #{request_name}") unless @config_data end |
Instance Attribute Details
#config_data ⇒ Object (readonly)
Returns the value of attribute config_data.
20 21 22 |
# File 'lib/gateway/client.rb', line 20 def config_data @config_data end |
#request ⇒ Object
Returns the value of attribute request.
19 20 21 |
# File 'lib/gateway/client.rb', line 19 def request @request end |
#test ⇒ Object (readonly)
Returns the value of attribute test.
20 21 22 |
# File 'lib/gateway/client.rb', line 20 def test @test end |
Instance Method Details
#response ⇒ Hash
Send request to gateway and take response from it
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/gateway/client.rb', line 44 def response # Rails.logger.debug('gateway request: ' + YAML::dump(request)) query_params = {r: config_data.route} case config_data.http_method when METHOD_GET query_params.merge! request.attributes response = RestClient.get(url(query_params)) when METHOD_POST response = RestClient.post(url(query_params), request.attributes, Authorization: @token) else raise Gateway::ArgumentError.new("http method #{config_data.http_method} is not supported") end return JSON.parse(response) rescue RestClient::Exception => e response = JSON.parse e.response raise Gateway::StandardError.new(response['message']) end |