class RubyEtsy::Client

Constants

REFRESH_TOKEN_URL
V3_URL

Attributes

access_token[RW]
api_key[RW]
api_secret[RW]
refresh_token[RW]
shop_id[RW]

Public Class Methods

new(access_token:, refresh_token:, api_key:, api_secret:, shop_id:) click to toggle source
# File lib/ruby-etsy/client.rb, line 15
def initialize(access_token:, refresh_token:, api_key:, api_secret:, shop_id:) 
  RubyEtsy.configure

  @access_token  = access_token  || RubyEtsy.config.access_token
  @refresh_token = refresh_token || RubyEtsy.config.refresh_token
  @api_key       = api_key       || RubyEtsy.config.api_key
  @api_secret    = api_secret    || RubyEtsy.config.api_secret
  @shop_id       = shop_id       || RubyEtsy.config.shop_id
end

Public Instance Methods

action(url, payload: {}, query_params: {}, http_method: :post) click to toggle source
# File lib/ruby-etsy/client.rb, line 25
def action(url, payload: {}, query_params: {}, http_method: :post)
  headers = {
    'User-Agent': "RubyEtsy client v#{RubyEtsy::VERSION})",
    'x-api-key': api_key,
    'Authorization': "Bearer #{access_token}" 
  }
  
  response = ::RestClient::Request.execute(
    method:     http_method, 
    url:        construct_url(url, query_params),
    payload:    payload.to_json, 
    headers:    headers,
    timeout:    5, 
    verify_ssl: ::OpenSSL::SSL::VERIFY_NONE
  )
  
  ::HttpParser.parse(response)
rescue RestClient::Unauthorized => e
  puts "RESCUED UNATHORIZED"
  
  refresh_token
  action(url, http_method: :get)
end

Private Instance Methods

construct_url(url, query_params={}) click to toggle source
# File lib/ruby-etsy/client.rb, line 76
def construct_url(url, query_params={})
  query = query_params.present? ? URI.encode_www_form(query_params).prepend('?') : ""
  "#{V3_URL}#{url}#{query}"
end