Class: Newegg::Client

Inherits:
Object
  • Object
show all
Includes:
API
Defined in:
lib/newegg/client.rb

Overview

API Client

Constant Summary collapse

DEFAULT_ENDPOINT =
'https://api.newegg.cn'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Orders

#cancel_order, #fetch_orders, #order_details, #ship_order

Constructor Details

#initialize(options) ⇒ Client

Initializes a new Client object

Parameters:

  • options (Hash)

Options Hash (options):

  • :app_key (String)

    Required.

  • :access_token (String)

    Required.

  • :endpoint (String)

    Optional.



20
21
22
23
24
# File 'lib/newegg/client.rb', line 20

def initialize(options)
  @app_key = options.fetch(:app_key)
  @access_token = options.fetch(:access_token)
  @endpoint = options[:endpoint]
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



13
14
15
# File 'lib/newegg/client.rb', line 13

def access_token
  @access_token
end

#app_keyObject (readonly)

Returns the value of attribute app_key.



13
14
15
# File 'lib/newegg/client.rb', line 13

def app_key
  @app_key
end

Instance Method Details

#connection_optionsHash

Connection options for faraday

Returns:

  • (Hash)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/newegg/client.rb', line 39

def connection_options
  {
    builder: middleware,
    headers: {
      accept: 'application/json',
      user_agent: user_agent,
      Authorization: "#{app_key}&#{access_token}"
    },
    request: {
      open_timeout: 10,
      timeout: 30
    }
  }
end

#delete(path, params = {}) ⇒ Object

Perform an HTTP DELETE request



84
85
86
# File 'lib/newegg/client.rb', line 84

def delete(path, params = {})
  request(:delete, path, params)
end

#endpointString

Returns:

  • (String)


27
28
29
# File 'lib/newegg/client.rb', line 27

def endpoint
  @endpoint ||= DEFAULT_ENDPOINT
end

#get(path, params = {}) ⇒ Object

Perform an HTTP GET request



69
70
71
# File 'lib/newegg/client.rb', line 69

def get(path, params = {})
  request(:get, path, params)
end

#middlewareFaraday::RackBuilder

Returns:

  • (Faraday::RackBuilder)


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/newegg/client.rb', line 55

def middleware
  Faraday::RackBuilder.new do |faraday|
    # Checks for files in the payload, otherwise leaves everything untouched
    faraday.request :multipart
    # Encodes as "application/x-www-form-urlencoded" if not already encoded
    faraday.request :url_encoded
    # Parse JSON response bodies
    faraday.response :parse_json
    # Set default HTTP adapter
    faraday.adapter :net_http
  end
end

#post(path, params = {}) ⇒ Object

Perform an HTTP POST request



74
75
76
# File 'lib/newegg/client.rb', line 74

def post(path, params = {})
  request(:post, path, params)
end

#put(path, params = {}) ⇒ Object

Perform an HTTP PUT request



79
80
81
# File 'lib/newegg/client.rb', line 79

def put(path, params = {})
  request(:put, path, params)
end

#user_agentString

Returns:

  • (String)


32
33
34
# File 'lib/newegg/client.rb', line 32

def user_agent
  "Newegg Ruby Gem #{Newegg::VERSION}"
end