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.
-
#url(query_params) ⇒ String
readonly
Generate url.
Instance Method Summary collapse
-
#initialize(request, token, url = Gateway.config.urls.production) ⇒ Client
constructor
Initialize.
-
#response ⇒ Hash
Send request to gateway and take response from it.
Constructor Details
#initialize(request, token, url = Gateway.config.urls.production) ⇒ Client
Initialize
29 30 31 32 33 34 35 36 37 |
# File 'lib/gateway/client.rb', line 29 def initialize(request, token, url = Gateway.config.urls.production) Gateway.config = JSON.parse(YAML.load_file("#{Gateway.root}/gateway/config.yml").to_json, object_class: OpenStruct) @request = request @token = token @url = url 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 |
#url(query_params) ⇒ String (readonly)
Generate url
74 75 76 |
# File 'lib/gateway/client.rb', line 74 def url @url 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 61 62 63 64 |
# 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 if config_data.content_type == 'json' response = RestClient.post(url(query_params), request.attributes.to_json, content_type: :json, Authorization: @token) else response = RestClient.post(url(query_params), request.attributes, Authorization: @token) end 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 |