class Optidash::Client

Main Client class, responsible for entire interaction with the API

Public Class Methods

new(api_key) click to toggle source
# File lib/optidash/client.rb, line 6
def initialize(api_key)
    @api_key = api_key
end

Public Instance Methods

fetch(url) click to toggle source

Prepares Fetch request @param url [String] web location of a file we wan't to transform via api

# File lib/optidash/client.rb, line 14
def fetch(url)
    validate_request :fetch

    # initiating the request
    @request = Optidash::Request::Fetch.new(@api_key, @proxy)

    # and preparing data for the request
    @request.data[:url] = url
    self
end
proxy(url = nil) click to toggle source

Sets request proxy

# File lib/optidash/client.rb, line 72
def proxy(url = nil)
    return self if url.nil?
    validate_url(url)
    # adding proxy if request instance already exists
    @request ? @request.proxy = url : @proxy = url
    self
end
to_buffer() click to toggle source

Returns response containing JSON metadata and binary file buffer

# File lib/optidash/client.rb, line 64
def to_buffer
    validate_binary_operations
    @request.perform(binary: true)
end
to_file(path) click to toggle source

Returns response containing JSON metadata and saves received binary file @param path [String] file destination

# File lib/optidash/client.rb, line 51
def to_file(path)
    if path.nil?
        raise Optidash::Errors::NoPathProvided, "No save path provided"
    end

    validate_binary_operations
    @request.perform(binary: true, save_path: path)
end
to_json() click to toggle source

Returns response containing only JSON metadata

# File lib/optidash/client.rb, line 42
def to_json
    @request.perform
end
upload(path) click to toggle source

Prepares Upload request @param path [String] local file path

# File lib/optidash/client.rb, line 30
def upload(path)
    validate_request :upload

    # initiating the request
    @request = Optidash::Request::Upload.new(@api_key, path, @proxy)
    self
end

Private Instance Methods

validate_binary_operations() click to toggle source

Validates if particular operations are valid for binary responses.

# File lib/optidash/client.rb, line 113
def validate_binary_operations
    if @request.data[:webhook]
        raise Optidash::Errors::OperationNotSupported, "Binary responses are not supported when using Webhooks"
    end

    if @request.data[:store]
        raise Optidash::Errors::OperationNotSupported, "Binary responses are not supported when using External Storage"
    end
end
validate_request(type) click to toggle source

Validates if we can perform request operation, i.e. if fetch was called upload should throw an exception and the other way around. So we don't chain two types of requests

@param type [Symbol] request type which we want to perform

# File lib/optidash/client.rb, line 104
def validate_request(type)
    return if @request.nil? || @request.type == type
    raise Optidash::Errors::RequestTypeConflict.new(@request.type, type)
end
validate_url(url) click to toggle source

Validates proxy url

# File lib/optidash/client.rb, line 127
def validate_url(url)
    return if url =~ /\A#{URI::regexp(['http', 'https'])}\z/
    raise Optidash::Errors::InvalidProxyUrl, "Proxy url is not valid"
end