class Mercadoshops::Api

Attributes

access_token[RW]

Public Class Methods

new(args={}) click to toggle source
# File lib/mercadoshops/api.rb, line 5
def initialize(args={})
  @access_token = args[:access_token]

  if args[:endpoint_url].present?
    @endpoint_url = args[:endpoint_url]
  else
    @endpoint_url = 'https://api.mercadoshops.com/v1'
  end
end

Public Instance Methods

get_last_response() click to toggle source
# File lib/mercadoshops/api.rb, line 17
def get_last_response
  @last_response
end
get_last_result() click to toggle source
# File lib/mercadoshops/api.rb, line 21
def get_last_result
  @last_result
end

Private Instance Methods

delete_request(action, params={}, headers={}) click to toggle source
# File lib/mercadoshops/api.rb, line 87
def delete_request(action, params={}, headers={})
  begin
    api_response_kind = headers.delete('api_response_kind')
    api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil?
    api_response_kind = 'object' if api_response_kind.nil?

    parse_response(api_response_kind, RestClient.delete("#{@endpoint_url}#{action}", params))
  rescue => e
    parse_response('object', e.response)
  end
end
get_request(action, params={}, headers={}) click to toggle source
# File lib/mercadoshops/api.rb, line 27
def get_request(action, params={}, headers={})
  begin
    api_response_kind = headers.delete('api_response_kind')
    api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil?
    api_response_kind = 'object' if api_response_kind.nil?

    parse_response(api_response_kind, RestClient.get("#{@endpoint_url}#{action}", {params: params}.merge(headers)))
  rescue => e
    parse_response('object', e.response)
  end
end
head_request(action, params={}, headers={}) click to toggle source
# File lib/mercadoshops/api.rb, line 75
def head_request(action, params={}, headers={})
  begin
    api_response_kind = headers.delete('api_response_kind')
    api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil?
    api_response_kind = 'object' if api_response_kind.nil?

    parse_response(api_response_kind, RestClient.head("#{@endpoint_url}#{action}", params))
  rescue => e
    parse_response('object', e.response)
  end
end
parse_response(api_response_kind, response) click to toggle source
# File lib/mercadoshops/api.rb, line 99
def parse_response(api_response_kind, response)
  @last_response = response

  result = OpenStruct.new
  result.status_code = response.code

  if api_response_kind == 'object'
    result.headers = (JSON.parse(response.headers.to_json, object_class: OpenStruct) rescue response.headers)
    result.body = (JSON.parse(response.body, object_class: OpenStruct) rescue response.body)
  elsif api_response_kind == 'hash'
    result.headers = response.headers
    result.body = (JSON.parse(response.body) rescue response.body)
  else
    result.headers = response.headers
    result.body = response.body
  end

  @last_result = result

  result
end
patch_request(action, params={}, headers={}) click to toggle source
# File lib/mercadoshops/api.rb, line 63
def patch_request(action, params={}, headers={})
  begin
    api_response_kind = headers.delete('api_response_kind')
    api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil?
    api_response_kind = 'object' if api_response_kind.nil?

    parse_response(api_response_kind, RestClient.patch("#{@endpoint_url}#{action}", params, headers))
  rescue => e
    parse_response('object', e.response)
  end
end
post_request(action, params={}, headers={}) click to toggle source
# File lib/mercadoshops/api.rb, line 39
def post_request(action, params={}, headers={})
  begin
    api_response_kind = headers.delete('api_response_kind')
    api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil?
    api_response_kind = 'object' if api_response_kind.nil?

    parse_response(api_response_kind, RestClient.post("#{@endpoint_url}#{action}", params, headers))
  rescue => e
    parse_response('object', e.response)
  end
end
put_request(action, params={}, headers={}) click to toggle source
# File lib/mercadoshops/api.rb, line 51
def put_request(action, params={}, headers={})
  begin
    api_response_kind = headers.delete('api_response_kind')
    api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil?
    api_response_kind = 'object' if api_response_kind.nil?

    parse_response(api_response_kind, RestClient.put("#{@endpoint_url}#{action}", params, headers))
  rescue => e
    parse_response('object', e.response)
  end
end