Class: Transloadit::Response

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

Defined Under Namespace

Modules: Assembly

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body:, headers:, status:) ⇒ Response

Creates a response without exposing the underlying HTTP client.

Parameters:

  • body (String) —

    the raw response body

  • headers (Hash) —

    the response headers

  • status (Integer) —

    the HTTP response status



13
14
15
16
17
# File 'lib/transloadit/response.rb', line 13

def initialize(body:, headers:, status:)
  @raw_body = body
  @headers = normalize_headers(headers)
  @status = status
end

Instance Attribute Details

#headers ⇒ Hash (readonly)

Returns normalized response headers.

Returns:

  • (Hash) —

    normalized response headers



20
21
22
# File 'lib/transloadit/response.rb', line 20

def headers
  @headers
end

#status ⇒ Integer (readonly) Also known as: code

Returns the HTTP response status.

Returns:

  • (Integer) —

    the HTTP response status



23
24
25
# File 'lib/transloadit/response.rb', line 23

def status
  @status
end

Instance Method Details

#[](attribute) ⇒ String

Returns the attribute from the JSON response.

Parameters:

  • attribute (String) —

    the attribute name to look up

Returns:

  • (String) —

    the value for the attribute



34
35
36
# File 'lib/transloadit/response.rb', line 34

def [](attribute)
  body[attribute.to_s]
end

#body ⇒ Hash

Returns the parsed JSON body.

Returns:

  • (Hash) —

    the parsed JSON body hash



43
44
45
# File 'lib/transloadit/response.rb', line 43

def body
  MultiJson.load @raw_body
end

#extend!(mod) ⇒ Transloadit::Response

Chainably extends the response with additional methods. Used to add context-specific functionality to a response.

Parameters:

  • mod (Module) —

    the module to extend with

Returns:



63
64
65
66
67
# File 'lib/transloadit/response.rb', line 63

def extend!(mod)
  extend(mod)

  self
end

#inspect ⇒ String

Inspects the body of the response.

Returns:

  • (String) —

    a human-readable version of the body



52
53
54
# File 'lib/transloadit/response.rb', line 52

def inspect
  body.inspect
end

#replace(other) ⇒ Transloadit::Response

Replaces this response's HTTP data with another response's data.

Parameters:

Returns:



75
76
77
78
79
80
# File 'lib/transloadit/response.rb', line 75

def replace(other)
  @raw_body = other.raw_body
  @headers = other.headers
  @status = other.status
  self
end