Class: Transloadit::Request

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

Overview

Wraps requests to the Transloadit API. Ensures all API requests return a parsed Transloadit::Response, and abstracts away finding a lightly-used instance on startup.

Constant Summary collapse

API_ENDPOINT =

The default Transloadit API endpoint.

URI.parse('http://api2.transloadit.com/')
API_HEADERS =

The default headers to send to the API.

{ 'User-Agent' => %{Transloadit Ruby SDK #{Transloadit::VERSION}} }
HMAC_ALGORITHM =

The HMAC algorithm used for calculation request signatures.

OpenSSL::Digest::Digest.new('sha1')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, secret = nil) ⇒ Request

Prepares a request against an endpoint URL, optionally with an encryption secret. If only a path is passed, the API will automatically determine the best server to use. If a full URL is given, the host provided will be used.

Parameters:

  • url (String)

    the API endpoint

  • secret (String) (defaults to: nil)

    an optional secret with which to sign the request



44
45
46
47
# File 'lib/transloadit/request.rb', line 44

def initialize(url, secret = nil)
  self.url    = URI.parse(url.to_s)
  self.secret = secret
end

Instance Attribute Details

#secretString

Returns the authentication secret to sign the request with.

Returns:

  • (String)

    the authentication secret to sign the request with



25
26
27
# File 'lib/transloadit/request.rb', line 25

def secret
  @secret
end

#urlString

Returns the API endpoint for the request.

Returns:

  • (String)

    the API endpoint for the request



22
23
24
# File 'lib/transloadit/request.rb', line 22

def url
  @url
end

Class Method Details

.bored!Object

Automatically sets the API endpoint to the server with the most free resources. This is called automatically the first time a request is made.



31
32
33
# File 'lib/transloadit/request.rb', line 31

def self.bored!
  self.api self.bored
end

Instance Method Details

#delete(params = {}) ⇒ Transloadit::Response

Performs an HTTP DELETE to the request’s URL. Takes an optional hash of query params.

Parameters:

  • params (Hash) (defaults to: {})

    additional query parameters

Returns:



69
70
71
72
73
# File 'lib/transloadit/request.rb', line 69

def delete(params = {})
  self.request! do
    self.api[url.path + self.to_query(params)].delete(API_HEADERS)
  end
end

#get(params = {}) ⇒ Transloadit::Response

Performs an HTTP GET to the request’s URL. Takes an optional hash of query params.

Parameters:

  • params (Hash) (defaults to: {})

    additional query parameters

Returns:



56
57
58
59
60
# File 'lib/transloadit/request.rb', line 56

def get(params = {})
  self.request! do
    self.api[url.path + self.to_query(params)].get(API_HEADERS)
  end
end

#inspectString

Returns a human-readable version of the prepared Request.

Returns:

  • (String)

    a human-readable version of the prepared Request



91
92
93
# File 'lib/transloadit/request.rb', line 91

def inspect
  self.url.to_s.inspect
end

#post(payload = {}) ⇒ Transloadit::Response

Performs an HTTP POST to the request’s URL. Takes an optional hash containing the form-encoded payload.

Parameters:

  • payload (Hash) (defaults to: {})

    the payload to form-encode along with the POST

Returns:



82
83
84
85
86
# File 'lib/transloadit/request.rb', line 82

def post(payload = {})
  self.request! do
    self.api[url.path].post(self.to_payload(payload), API_HEADERS)
  end
end