Class: CardmarketCLI::Account

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_token, app_secret, access_token, access_token_secret, options = {}) ⇒ Account

Returns a new instance of Account.



17
18
19
20
21
# 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

Instance Attribute Details

#request_countObject (readonly)

Returns the value of attribute request_count.



15
16
17
# File 'lib/cardmarket_cli/account.rb', line 15

def request_count
  @request_count
end

#request_limitObject (readonly)

Returns the value of attribute request_limit.



15
16
17
# File 'lib/cardmarket_cli/account.rb', line 15

def request_limit
  @request_limit
end

Instance Method Details

#delete(path, body: nil, format: :json, params: {}) ⇒ Object



35
36
37
# 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: {}) ⇒ Object



23
24
25
# 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) ⇒ Object



58
59
60
61
62
63
64
# 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: {}) ⇒ Object



50
51
52
53
54
55
56
# 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: {}) ⇒ Object



31
32
33
# 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: {}) ⇒ Object



27
28
29
# 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: {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# 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