Class: Connectors::ApiConnector

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

Overview

Heading

This class has all the common logic from api connectors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ApiConnector

:notnew:



11
12
13
14
15
16
17
18
# File 'lib/api_connector/api_connector.rb', line 11

def initialize(options = {}) #:notnew:
  options.symbolize_keys!

  #@logger = Logger.new "#{Rails.root}/log/#{self.class.to_s.demodulize.underscore}.log"
  #@logger.level = Rails.logger.level

  opts_to_vars(options)
end

Instance Attribute Details

#api_client_idObject (readonly)

Returns the value of attribute api_client_id.



7
8
9
# File 'lib/api_connector/api_connector.rb', line 7

def api_client_id
  @api_client_id
end

#api_domainObject (readonly)

Returns the value of attribute api_domain.



7
8
9
# File 'lib/api_connector/api_connector.rb', line 7

def api_domain
  @api_domain
end

#api_domain_formatObject (readonly)

Returns the value of attribute api_domain_format.



7
8
9
# File 'lib/api_connector/api_connector.rb', line 7

def api_domain_format
  @api_domain_format
end

#api_headers_tokenObject (readonly)

Returns the value of attribute api_headers_token.



7
8
9
# File 'lib/api_connector/api_connector.rb', line 7

def api_headers_token
  @api_headers_token
end

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/api_connector/api_connector.rb', line 7

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



7
8
9
# File 'lib/api_connector/api_connector.rb', line 7

def code
  @code
end

#connection_protocolObject (readonly)

Returns the value of attribute connection_protocol.



7
8
9
# File 'lib/api_connector/api_connector.rb', line 7

def connection_protocol
  @connection_protocol
end

Returns the value of attribute cookie_jar.



7
8
9
# File 'lib/api_connector/api_connector.rb', line 7

def cookie_jar
  @cookie_jar
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



7
8
9
# File 'lib/api_connector/api_connector.rb', line 7

def cookies
  @cookies
end

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/api_connector/api_connector.rb', line 7

def headers
  @headers
end

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'lib/api_connector/api_connector.rb', line 7

def logger
  @logger
end

#requestObject (readonly)

Returns the value of attribute request.



7
8
9
# File 'lib/api_connector/api_connector.rb', line 7

def request
  @request
end

Instance Method Details

#call(method, endpoint, args = {}, params) ⇒ Object

low level api for request (needed por PUT, PATCH & DELETE methods)

Attributes

  • endpoint - Url endpoint ex. /merchant/get

  • args - Request arguments, (add headers key for extra headers options) ex. { method: :get, headers: { ‘content-type’ => ‘xml’ } } (method key is needed, otherwise :get will be setted)

  • params - Request parameters / payload data



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/api_connector/api_connector.rb', line 57

def call method, endpoint, args={}, params
  raise "Endpoint can't be blank" unless endpoint
  raise "Method is missing" unless method

  url = (method == :get || method == :delete) ? url(endpoint,params) : url(endpoint)

  RestClient::Request.execute(method: method,
                          url: url,
                          headers: header(args[:headers]),
                          payload: params || {}
                         ) do |response, request, result|
                           #status = response.code == 200 ? :debug : :error
                           #print(status, request, response.body)
                           parse(response, endpoint)
                         end

end

#delete(hash = {}) ⇒ Object



32
33
34
35
# File 'lib/api_connector/api_connector.rb', line 32

def delete(hash={})
  hash.symbolize_keys!
  call(:delete, hash[:endpoint], (hash[:args]||{}), hash[:params]||{})
end

#get(hash = {}) ⇒ Object

makes a GET request

Attributes

* +hash+ - Hash of Parameters
** +endpoint+ - Url endpoint ex. /product/get (no need to specify the version)
** +args+ - Request arguments, (add headers key for extra headers options) ex. hash[:headers] = { 'content-type' => 'xml' }
** +params+ - Request parameters. ex. hash[:params] = { 'merchantId' => 'XXXXXX' }


27
28
29
30
# File 'lib/api_connector/api_connector.rb', line 27

def get(hash={})
  hash.symbolize_keys!
  call(:get, hash[:endpoint], (hash[:args]||{}), hash[:params]||{})
end

#parse(response, endpoint = nil) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/api_connector/api_connector.rb', line 75

def parse(response, endpoint = nil)
  @headers, @code, @cookies, @cookie_jar, @request, @body = response.headers, response.code, response.cookies, response.cookie_jar, response.request, response.body
  begin
    JSON.parse(response)
  rescue JSON::ParserError
    { status: '400', message: "RestClient failed to parse JSON: #{response}" }
  end
end

#post(hash = {}, payload) ⇒ Object

makes a POST request

Attributes

  • hash - Hash of parameters

** endpoint - Url endpoint ex. /product/createOrUpdate ** args - Request arguments, (add headers key for extra headers options) ex. hash = { ‘content-type’ => ‘xml’ }

  • payload - Data for the request ex. { merchantId: ‘asdasdsadas’, products: [{ … },{ …}…]}



44
45
46
47
48
49
# File 'lib/api_connector/api_connector.rb', line 44

def post hash={}, payload
  raise 'Payload cannot be blank' if payload.nil? || payload.empty?

  hash.symbolize_keys!
  call(:post, hash[:endpoint], (hash[:args]||{}).merge({:method => :post}), payload)
end