Class: GarageClient::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/garage_client/response.rb,
lib/garage_client/response/cacheable.rb,
lib/garage_client/response/raise_http_exception.rb

Defined Under Namespace

Classes: Cacheable, RaiseHttpException

Constant Summary collapse

MIME_DICT =
%r{application/vnd\.cookpad\.dictionary\+(json|x-msgpack)}
ACCEPT_BODY_TYPES =
[Array, Hash, NilClass]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, response) ⇒ Response

Returns a new instance of Response.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/garage_client/response.rb', line 10

def initialize(client, response)
  @client = client
  @response = response

  # Faraday's Net::Http adapter returns '' if response is nil.
  # Changes from faraday v0.9.0. faraday/f41ffaabb72d3700338296c79a2084880e6a9843
  #
  # GarageClient::Response#body should be always a String when Faraday
  # became v1.0.0. Because 0.9.0 seems to be not stable.
  response.env[:body] = nil if response.env[:body] == ''

  unless ACCEPT_BODY_TYPES.any? {|type| type === response.body }
    raise GarageClient::InvalidResponseType, "Invalid response type (#{response.body.class}): #{response.body}"
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



107
108
109
# File 'lib/garage_client/response.rb', line 107

def method_missing(name, *args, &block)
  body.send(name, *args, &block)
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



8
9
10
# File 'lib/garage_client/response.rb', line 8

def client
  @client
end

#responseObject

Returns the value of attribute response.



8
9
10
# File 'lib/garage_client/response.rb', line 8

def response
  @response
end

Instance Method Details

#bodyObject



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

def body
  @body ||= case response.body
            when Array
              response.body.map {|res| GarageClient::Resource.new(client, res) }
            when Hash
              if dictionary_response?
                Hash[response.body.map {|id, res| [id, GarageClient::Resource.new(client, res)] }]
              else
                GarageClient::Resource.new(client, response.body)
              end
            when NilClass
              nil
            end
end


93
94
95
# File 'lib/garage_client/response.rb', line 93

def first_page_link
  parsed_link_header.try(:find_link, %w[rel first])
end

#first_page_pathObject



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

def first_page_path
  first_page_link.try(:href)
end

#has_first_page?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/garage_client/response.rb', line 77

def has_first_page?
  !!first_page_link
end

#has_last_page?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/garage_client/response.rb', line 81

def has_last_page?
  !!last_page_link
end

#has_next_page?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/garage_client/response.rb', line 69

def has_next_page?
  !!next_page_link
end

#has_prev_page?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/garage_client/response.rb', line 73

def has_prev_page?
  !!prev_page_link
end


97
98
99
# File 'lib/garage_client/response.rb', line 97

def last_page_link
  parsed_link_header.try(:find_link, %w[rel last])
end

#last_page_pathObject



65
66
67
# File 'lib/garage_client/response.rb', line 65

def last_page_path
  last_page_link.try(:href)
end


26
27
28
# File 'lib/garage_client/response.rb', line 26

def link
  @link ||= response.headers['Link']
end


85
86
87
# File 'lib/garage_client/response.rb', line 85

def next_page_link
  parsed_link_header.try(:find_link, %w[rel next])
end

#next_page_pathObject



53
54
55
# File 'lib/garage_client/response.rb', line 53

def next_page_path
  next_page_link.try(:href)
end


89
90
91
# File 'lib/garage_client/response.rb', line 89

def prev_page_link
  parsed_link_header.try(:find_link, %w[rel prev])
end

#prev_page_pathObject



57
58
59
# File 'lib/garage_client/response.rb', line 57

def prev_page_path
  prev_page_link.try(:href)
end

#respond_to_missing?(name, include_private) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/garage_client/response.rb', line 101

def respond_to_missing?(name, include_private)
  body.respond_to?(name, include_private)
end

#total_countObject



30
31
32
33
34
35
36
# File 'lib/garage_client/response.rb', line 30

def total_count
  unless @total_count
    @total_count = response.headers['X-List-TotalCount']
    @total_count = @total_count.to_i if @total_count
  end
  @total_count
end