class CardmarketCLI::Account

Attributes

request_count[R]
request_limit[R]

Public Class Methods

new(app_token, app_secret, access_token, access_token_secret, options = {}) click to toggle source
# File lib/cardmarket_cli/account.rb, line 17
def initialize(app_token, app_secret, access_token, access_token_secret, options = {})
  options[:site] ||= options[:test] ? 'https://sandbox.cardmarket.com' : 'https://api.cardmarket.com'
  @oauth_consumer = OAuth::Consumer.new(app_token, app_secret, site: options[:site])
  @access_token = OAuth::AccessToken.new(@oauth_consumer, access_token, access_token_secret)
end

Public Instance Methods

delete(path, body: nil, format: :json, params: {}) click to toggle source
# File lib/cardmarket_cli/account.rb, line 35
def delete(path, body: nil, format: :json, params: {})
  request(path, :delete, body: body, format: format, params: params)
end
get(path, body: nil, format: :json, params: {}) click to toggle source
# File lib/cardmarket_cli/account.rb, line 23
def get(path, body: nil, format: :json, params: {})
  request(path, :get, body: body, format: format, params: params)
end
make_body(body) click to toggle source
# File lib/cardmarket_cli/account.rb, line 58
def make_body(body)
  if body.respond_to? :each
    body = XmlSimple.xml_out(body, RootName: 'request', XmlDeclaration: '<?xml version="1.0" encoding="UTF-8" ?>',
                                   SuppressEmpty: nil, NoAttr: true)
  end
  body
end
make_uri(path, format: :json, params: {}) click to toggle source
# File lib/cardmarket_cli/account.rb, line 50
def make_uri(path, format: :json, params: {})
  raise "Unknown format #{format}" unless %i[json xml].include?(format)

  params = "#{'?' unless params.empty?}#{params.to_a.map { |k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }
                                          .join('&')}"
  "#{@oauth_consumer.site}/ws/v2.0/output.#{format}/#{path}#{params}"
end
post(path, body: nil, format: :json, params: {}) click to toggle source
# File lib/cardmarket_cli/account.rb, line 31
def post(path, body: nil, format: :json, params: {})
  request(path, :post, body: body, format: format, params: params)
end
put(path, body: nil, format: :json, params: {}) click to toggle source
# File lib/cardmarket_cli/account.rb, line 27
def put(path, body: nil, format: :json, params: {})
  request(path, :put, body: body, format: format, params: params)
end
request(path, method, body: nil, format: :json, params: {}) click to toggle source
# File lib/cardmarket_cli/account.rb, line 39
def request(path, method, body: nil, format: :json, params: {})
  uri = make_uri(path, format: format, params: params)
  req = Typhoeus::Request.new(uri, method: method, body: make_body(body))
  oauth_helper = OAuth::Client::Helper.new(req, consumer: @oauth_consumer, token: @access_token, request_uri: uri)
  req.options[:headers].merge!(
    { 'Authorization' => oauth_helper.header + ", realm=#{make_uri(path, format: format).inspect}" }
  )
  LOGGER.info log_request(method, uri, body)
  run_req(req)
end

Private Instance Methods

get_from_header(response, regex) click to toggle source
# File lib/cardmarket_cli/account.rb, line 78
def get_from_header(response, regex)
  match = response.response_headers.match(regex)
  match&.size&.positive? ? match[0].split(':')&.fetch(1) : nil
end
log_request(method, uri, body) click to toggle source
# File lib/cardmarket_cli/account.rb, line 83
def log_request(method, uri, body)
  LOGGER.info("#{method.to_s.capitalize}: #{uri.inspect}")
  LOGGER.debug { body.to_yaml }
end
run_req(req) click to toggle source
# File lib/cardmarket_cli/account.rb, line 68
def run_req(req)
  response = req.run
  @request_count = get_from_header(response, /X-Request-Limit-Count: \d+/).to_i
  @request_limit = get_from_header(response, /X-Request-Limit-Max: \d+/).to_i
  LOGGER.info("#{response.response_code} (#{response.return_code}) (Limit: "\
              "#{request_count || '?'}/#{request_limit || '?'})")
  LOGGER.debug { JSON.parse(response.response_body).to_yaml }
  response
end