Class: Pixaven::Request::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pixaven/request.rb

Overview

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

Direct Known Subclasses

Fetch, Upload

Constant Summary collapse

API_URL =
"https://api.pixaven.com/1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, proxy = nil) ⇒ Base

Constructor. Passing api key for authentication



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pixaven/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

Instance Attribute Details

#dataObject

Returns the value of attribute data.



14
15
16
# File 'lib/pixaven/request.rb', line 14

def data
  @data
end

#proxyObject

Returns the value of attribute proxy.



15
16
17
# File 'lib/pixaven/request.rb', line 15

def proxy
  @proxy
end

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/pixaven/request.rb', line 13

def type
  @type
end

Instance Method Details

#perform(binary: false, save_path: nil) ⇒ Object

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pixaven/request.rb', line 50

def perform(binary: false, save_path: nil)
    @data.merge!(response: { mode: "binary" }) if binary

    Pixaven::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
    Pixaven::Response.new(e.response).data
end