Class: Traitify::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Response

Returns a new instance of Response.



5
6
7
# File 'lib/traitify/response.rb', line 5

def initialize(request)
  @request = request
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



3
4
5
# File 'lib/traitify/response.rb', line 3

def request
  @request
end

Instance Method Details

#dataObject



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

def data
  @data ||= Traitify::Data.new(request.body)
end

#pageObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/traitify/response.rb', line 13

def page
  @page ||= begin
    page = {}

    if request.env.response_headers["link"]
      links = request.env.response_headers["link"].split(",")
      if prev_link = links.find{ |link| link.include?("rel=\"prev\"") }
        prev_link = prev_link.split(/>|</)[1]
        page[:previous] = {
          url: prev_link,
          params: CGI.parse(prev_link.split("?")[1..-1].join("?"))
        }
      end

      if next_link = links.find{ |link| link.include?("rel=\"next\"") }
        next_link = next_link.split(/>|</)[1]
        page[:next] = {
          url: next_link,
          params: CGI.parse(next_link.split("?")[1..-1].join("?"))
        }
      end
    end

    Traitify::Data.new page
  end
end

#totalObject



40
41
42
# File 'lib/traitify/response.rb', line 40

def total
  @total ||= request.env.response_headers["x-total-count"]
end