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("https://api2.transloadit.com/")
API_HEADERS =

The default headers to send to the API.

{"Transloadit-Client" => "ruby-sdk:#{Transloadit::VERSION}"}
HMAC_ALGORITHM =

The HMAC algorithm used for calculation request signatures.

OpenSSL::Digest.new("sha384")

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



38
39
40
41
# File 'lib/transloadit/request.rb', line 38

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

Instance Attribute Details

#secret ⇒ String

Returns the authentication secret to sign the request with.

Returns:

  • (String) —

    the authentication secret to sign the request with



27
28
29
# File 'lib/transloadit/request.rb', line 27

def secret
  @secret
end

#url ⇒ String

Returns the API endpoint for the request.

Returns:

  • (String) —

    the API endpoint for the request



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

def url
  @url
end

Class Method Details

._hmac(key, message) ⇒ String

Computes an HMAC digest from the key and message.

Parameters:

  • key (String) —

    the secret key to sign with

  • message (String) —

    the message to sign

Returns:

  • (String) —

    the signature of the message



112
113
114
# File 'lib/transloadit/request.rb', line 112

def self._hmac(key, message)
  OpenSSL::HMAC.hexdigest HMAC_ALGORITHM, key, message
end

Instance Method Details

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

Performs an HTTP DELETE 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:



63
64
65
66
67
68
69
70
# File 'lib/transloadit/request.rb', line 63

def delete(payload = {})
  request! do
    api.delete(url.path) do |request|
      request.headers.update(API_HEADERS)
      request.body = to_payload(payload)
    end
  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:



50
51
52
53
54
# File 'lib/transloadit/request.rb', line 50

def get(params = {})
  request! do
    api.get(url.path + to_query(params), nil, API_HEADERS)
  end
end

#inspect ⇒ String

Returns a human-readable version of the prepared Request.

Returns:

  • (String) —

    a human-readable version of the prepared Request



101
102
103
# File 'lib/transloadit/request.rb', line 101

def inspect
  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:



79
80
81
82
83
# File 'lib/transloadit/request.rb', line 79

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

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

Performs an HTTP PUT 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:



92
93
94
95
96
# File 'lib/transloadit/request.rb', line 92

def put(payload = {})
  request! do
    api.put(url.path, to_payload(payload), API_HEADERS)
  end
end