class Connectors::ApiConnector
Heading¶ ↑
This class has all the common logic from api connectors.
Attributes
api_client_id[R]
api_domain[R]
api_domain_format[R]
api_headers_token[R]
body[R]
code[R]
connection_protocol[R]
headers[R]
logger[R]
request[R]
Public Instance Methods
call(method, endpoint, args={})
click to toggle source
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
# 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={})
click to toggle source
# 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={})
click to toggle source
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' }
# 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)
click to toggle source
# 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={})
click to toggle source
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: [{ … },{ …}…]}
# 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
Protected Instance Methods
common_headers()
click to toggle source
# File lib/api_connector/api_connector.rb, line 116 def common_headers { 'X-Client-Id' => @api_client_id }.merge( {"Authorization" => "Basic #{@api_headers_token}"} ) if @api_headers_token end
format(string)
click to toggle source
# File lib/api_connector/api_connector.rb, line 136 def format string match_data = string.match(/\w+.+\w+/) match_data ? match_data[0] : '' end
header(headers={})
click to toggle source
# File lib/api_connector/api_connector.rb, line 94 def header headers={} (common_headers || {}).merge({ 'X-Client-Id' => @x_person_id, 'Content-Type' => @content_type || 'application/json', 'Charset' => @charset || 'utf-8' }).merge(headers || {}) end
initialize(options = {})
click to toggle source
# 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
opts_to_vars(opts)
click to toggle source
# File lib/api_connector/api_connector.rb, line 86 def opts_to_vars(opts) instance_eval do opts.each do |k, v| instance_variable_set("@#{k}".to_sym, v) end end end
parametrize(hash)
click to toggle source
# File lib/api_connector/api_connector.rb, line 128 def parametrize hash hash.map do |key,values| [values].flatten.map do |value| "#{key}=#{value}" end end.join('&') end
print(status, request, response)
click to toggle source
# File lib/api_connector/api_connector.rb, line 107 def print(status, request, response) status = :debug @logger.send(status, "#{DateTime.now} "\ "- Request: #{request.inspect} "\ "- Response: #{response.force_encoding('utf-8')}" ) end
url(endpoint, args={})
click to toggle source
# File lib/api_connector/api_connector.rb, line 102 def url endpoint, args={} url_constructor endpoint, args end
url_constructor(endpoint, hash)
click to toggle source
# File lib/api_connector/api_connector.rb, line 122 def url_constructor endpoint, hash url = "#{@connection_protocol}://#{format(@api_domain)}" << "/#{format(endpoint)}" url << ("?#{parametrize(hash)}") unless hash.empty? Addressable::URI.parse(url).normalize.to_str end