Class: WhosGotDirt::Response

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

Overview

Accepts a response and returns the results in a consistent format.

Examples:

Create a new class for transforming responses to results.

class MyAPIResponse < WhosGotDirt::Response
  def to_a
    JSON.load(body).map do |result|
      {
        request_url: env.url.to_s,
        name: result['Name'],
      }
    end
  end
end

Use the class to parse a response.

url = MyAPIRequest.new(name: 'John Smith').to_s
response = Faraday.get(url)
MyAPIResponse.new(response).to_a
# [{
#   :request_url=>"https://api.example.com/endpoint?name=John+Smith",
#   :name=>"John Smith"
# }, {
#   :request_url=>"https://api.example.com/endpoint?name=John+Smith",
#   :name=>"John Aaron Smith"
# }]

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Object

Sets the response's response.

Parameters:

  • response (Faraday::Response)

    a response



# File 'lib/whos_got_dirt/response.rb', line 72

Class Attribute Details

.templateObject (readonly)

Returns the value of attribute template.



31
32
33
# File 'lib/whos_got_dirt/response.rb', line 31

def template
  @template
end

Instance Method Details

#bodyString

Returns the response body.

Returns:

  • (String)

    the response body



# File 'lib/whos_got_dirt/response.rb', line 76

#env#url, #request_headers

Returns the response's internals.

Returns:

  • (#url, #request_headers)

    the response's internals



# File 'lib/whos_got_dirt/response.rb', line 80

#error_messageString

Returns the error message in the response body.

Returns:

  • (String)

    the error message in the response body



68
69
70
# File 'lib/whos_got_dirt/response.rb', line 68

def error_message
  body
end

#headers#[]

Returns the response headers.

Returns:

  • (#[])

    the response headers



# File 'lib/whos_got_dirt/response.rb', line 84

#item_url(result) ⇒ Object

This method is abstract.

Subclass and override #item_url to return an item's URL when given a result

Raises:

  • (NotImplementedError)


61
62
63
# File 'lib/whos_got_dirt/response.rb', line 61

def item_url(result)
  raise NotImplementedError
end

#parse_bodyObject

This method is abstract.

Subclass and override #parse_body to parse the response body

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/whos_got_dirt/response.rb', line 49

def parse_body
  raise NotImplementedError
end

#parsed_bodyObject

Returns the parsed response body.

Returns:

  • the parsed response body



44
45
46
# File 'lib/whos_got_dirt/response.rb', line 44

def parsed_body
  @parsed_body ||= parse_body
end

#rendererObject

Returns the result renderer.

Returns:

  • the result renderer



37
38
39
# File 'lib/whos_got_dirt/response.rb', line 37

def renderer
  @renderer ||= Renderer.new(self.class.template)
end

#statusFixnum

Returns the HTTP status code.

Returns:

  • (Fixnum)

    the HTTP status code



# File 'lib/whos_got_dirt/response.rb', line 88

#success?Boolean

Returns whether the request succeeded.

Returns:

  • (Boolean)

    whether the request succeeded



# File 'lib/whos_got_dirt/response.rb', line 92

#to_aObject

This method is abstract.

Subclass and override #to_a to transform the parsed response body into results

Raises:

  • (NotImplementedError)


55
56
57
# File 'lib/whos_got_dirt/response.rb', line 55

def to_a
  raise NotImplementedError
end