Class: Crest::Response

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

Overview

A normalized interface to represent CREST API responses

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns CREST API response.

Parameters:

  • status (String)
  • body (String)
  • headers (Hash{String => String})

38
39
40
41
42
43
44
45
46
47
48
# File 'lib/crest/response.rb', line 38

def initialize(status:, body:, headers:)
  @body = body
  @headers = headers
  @status = status

  @data = begin
    Hashie::Mash.new JSON.parse(body)
  rescue JSON::ParserError
    nil
  end
end

Instance Attribute Details

#bodyString (readonly)

HTTP response body

Returns:

  • (String)

9
10
11
# File 'lib/crest/response.rb', line 9

def body
  @body
end

#dataHashie::Mash? (readonly)

Parsed CREST API response data

Returns:

  • (Hashie::Mash, nil)

21
22
23
# File 'lib/crest/response.rb', line 21

def data
  @data
end

#headersHash{String => String} (readonly)

HTTP response headers

Returns:

  • (Hash{String => String})

13
14
15
# File 'lib/crest/response.rb', line 13

def headers
  @headers
end

#statusString (readonly)

HTTP reponse status code

Returns:

  • (String)

17
18
19
# File 'lib/crest/response.rb', line 17

def status
  @status
end

Class Method Details

.from_excon(excon_response) ⇒ Crest::Response

Convert an Excon response object into a Crest::Response

Parameters:

  • excon_response (Excon::Response)

Returns:


26
27
28
29
30
31
32
# File 'lib/crest/response.rb', line 26

def self.from_excon(excon_response)
  new(
    status: excon_response.status,
    headers: excon_response.headers.to_hash,
    body: excon_response.body
  )
end

Instance Method Details

#inspectObject


51
52
53
# File 'lib/crest/response.rb', line 51

def inspect
  format('<%s status="%s">', self.class.name, @status)
end