class Newegg::Client
Constants
- DEFAULT_ENDPOINT
Attributes
access_token[R]
app_key[R]
Public Class Methods
new(options)
click to toggle source
Initializes a new Client
object @param options [Hash] @option options [String] :app_key Required. @option options [String] :access_token Required. @option options [String] :endpoint Optional.
# 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
Public Instance Methods
connection_options()
click to toggle source
Connection options for faraday
@return [Hash]
# 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 = {})
click to toggle source
Perform an HTTP DELETE request
# File lib/newegg/client.rb, line 84 def delete(path, params = {}) request(:delete, path, params) end
endpoint()
click to toggle source
@return [String]
# File lib/newegg/client.rb, line 27 def endpoint @endpoint ||= DEFAULT_ENDPOINT end
get(path, params = {})
click to toggle source
Perform an HTTP GET request
# File lib/newegg/client.rb, line 69 def get(path, params = {}) request(:get, path, params) end
middleware()
click to toggle source
@return [Faraday::RackBuilder]
# 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 = {})
click to toggle source
Perform an HTTP POST request
# File lib/newegg/client.rb, line 74 def post(path, params = {}) request(:post, path, params) end
put(path, params = {})
click to toggle source
Perform an HTTP PUT request
# File lib/newegg/client.rb, line 79 def put(path, params = {}) request(:put, path, params) end
user_agent()
click to toggle source
@return [String]
# File lib/newegg/client.rb, line 32 def user_agent "Newegg Ruby Gem #{Newegg::VERSION}" end
Private Instance Methods
connection()
click to toggle source
# File lib/newegg/client.rb, line 90 def connection @connection ||= Faraday.new(endpoint, connection_options) end
request(method, path, params = {}, headers = {})
click to toggle source
# File lib/newegg/client.rb, line 94 def request(method, path, params = {}, headers = {}) connection.send(method.to_sym, path, params) { |request| request.headers.update(headers) }.env end