class NeonRAW::Clients::Base

The underlying base for the client

Public Instance Methods

api_connection(path, meth, params, opts = {}, json = true) click to toggle source

Connects to Reddit for oAuth2 requests. @!method api_connection(path, meth, params, opts = {}, json = true) @param path [String] The API path. @param meth [Symbol] The request method. @param params [Hash] The parameters. @param opts [Hash] Optional parameters for the request body. @param json [Boolean] Whether or not the expected response will be JSON. @return [Typhoeus::Response] Returns the response.

# File lib/NeonRAW/clients/base.rb, line 35
def api_connection(path, meth, params, opts = {}, json = true)
  sleep(@ratelimit_reset) if @requests_remaining <= 0
  response = Typhoeus::Request.new(
    'https://oauth.reddit.com' + path,
    method: meth,
    body: opts,
    headers: api_headers,
    params: params
  ).run
  error = assign_errors(response, json)
  raise error unless error.nil?
  update_ratelimit_info(response.headers)
  response
end
api_headers() click to toggle source

Creates headers for oAuth2 requests. @!method api_headers @return [Hash] Returns oAuth2 headers.

# File lib/NeonRAW/clients/base.rb, line 20
def api_headers
  {
    'User-Agent' => @user_agent,
    'Authorization' => "bearer #{@access.access_token}"
  }
end
auth_connection(path, meth, params) click to toggle source

Makes the connection used to authorize the client. @!method auth_connection(path, meth, params) @param path [String] The API path. @param meth [Symbol] The request method. @param params [Hash] The parameters. @return [Typhoeus::Response] Returns the response.

# File lib/NeonRAW/clients/base.rb, line 56
def auth_connection(path, meth, params)
  response = Typhoeus::Request.new(
    'https://www.reddit.com' + path,
    method: meth,
    userpwd: "#{@client_id}:#{@secret}",
    headers: { 'User-Agent' => @user_agent },
    params: params
  ).run
  error = assign_errors(response, true)
  raise error unless error.nil?
  response
end
refresh_access!() click to toggle source

Refreshes the access token. @!method refresh_access!

# File lib/NeonRAW/clients/base.rb, line 71
def refresh_access!
  response = auth_connection(
    '/api/v1/access_token', :post,
    grant_type: 'refresh_token',
    refresh_token: @access.refresh_token
  )
  data = JSON.parse(response.body, symbolize_names: true)
  @access.refresh! data
end
request_data(path, meth, params = {}, opts = {}) click to toggle source

Requests data from Reddit. @!method request_data(path, meth, params = {}, opts = {}) @param path [String] The API path to connect to. @param meth [Symbol] The request method to use. @param params [Hash] Parameters for the request. @param opts [Hash] Optional parameters for methods that send stuff

via the request body.

@return [Hash] Returns the parsed JSON containing the response data.

# File lib/NeonRAW/clients/base.rb, line 89
def request_data(path, meth, params = {}, opts = {})
  refresh_access! if @access.expired?
  response = api_connection(path, meth, params, opts)
  data = JSON.parse(response.body, symbolize_names: true)
  error = parse_errors(data)
  raise error unless error.nil?
  data
end
request_nonjson(path, meth, params = {}, opts = {}) click to toggle source

Requests non-JSON data from Reddit. @!method request_nonjson(path, meth, params = {}, opts = {}) @param path [String] The API path to connect to. @param meth [Symbol] The request method to use. @param params [Hash] Parameters for the request. @param opts [Hash] Optional parameters for methods that send stuff

via the request body.
# File lib/NeonRAW/clients/base.rb, line 105
def request_nonjson(path, meth, params = {}, opts = {})
  refresh_access! if @access.expired?
  api_connection(path, meth, params, opts, false).body
end