class Optidash::Request::Base

Base class. Should be inherited by Fetch and Upload classes.

Constants

API_URL

Attributes

data[RW]
proxy[RW]
type[R]

Public Class Methods

new(key, proxy = nil) click to toggle source

Constructor. Passing api key for authentication

# File lib/optidash/request.rb, line 23
def initialize(key, proxy = nil)

    ##
    # Setting initial options for the request

    @options = {
        user: key,
        password: "",
        method: :post,
        url: "#{API_URL}/#{@type}",
        ssl_ca_file: File.dirname(__FILE__) + '/../data/cacert.pem'
    }

    @proxy = proxy

    ##
    # Request data, which will be sent do API server

    @data = {}
end

Public Instance Methods

perform(binary: false, save_path: nil) click to toggle source

Performs actual request, sending entire data set options:

  • binary [Boolean] indicates whether request should return binary response or not

  • save_path [String|NilClass] save location for binary file, if needed

# File lib/optidash/request.rb, line 50
def perform(binary: false, save_path: nil)
    @data.merge!(response: { mode: "binary" }) if binary

    Optidash::Response.new(
        RestClient::Request.execute(@options.merge(
            payload: request_data,
            headers: headers(binary: binary),
            proxy: proxy
        )),
        save_path: save_path
    ).data
rescue RestClient::ExceptionWithResponse => e
    Optidash::Response.new(e.response).data
end

Private Instance Methods

headers(binary: false) click to toggle source

Prepares request headers, appending x-optidash-binary header if necessary.

# File lib/optidash/request.rb, line 71
def headers(binary: false)
    @headers ||= {}
    @headers["x-optidash-binary"] = 1 if binary
    @headers
end