Class: NRB::Untappd::API::Pagination

Inherits:
Object
  • Object
show all
Defined in:
lib/drink-socially/api/pagination.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pagination) ⇒ Pagination

Returns a new instance of Pagination.



17
18
19
20
21
22
23
# File 'lib/drink-socially/api/pagination.rb', line 17

def initialize(pagination)
  @api_max_id = pagination[:max_id].to_i
  @uris   = { api_next: URI.parse(pagination[:next_url]),
              api_prev: URI.parse(pagination[:since_url]),
              api_since: URI.parse(pagination[:since_url])
            }
end

Class Method Details

.from_response(response) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/drink-socially/api/pagination.rb', line 7

def self.from_response(response)
  # It's duck types (almost) all the way down
  return unless response.respond_to?(:body) &&
                response.body.respond_to?(:response) &&
                response.body.response.respond_to?(:pagination) &&
                !! response.body.response.pagination
  new response.body.response.pagination
end

Instance Method Details

#next_idObject



26
27
28
# File 'lib/drink-socially/api/pagination.rb', line 26

def next_id
  @api_max_id + 1
end

#next_uriObject

Warning: The API will return the most recent results if next_id is

greater than the most recent result


33
34
35
# File 'lib/drink-socially/api/pagination.rb', line 33

def next_uri
  uri_for query: "since=#{next_id}", uri: :next
end

#next_urlObject



38
39
40
# File 'lib/drink-socially/api/pagination.rb', line 38

def next_url
  next_uri.to_s
end

#prev_idObject Also known as: previous_id



43
44
45
# File 'lib/drink-socially/api/pagination.rb', line 43

def prev_id
  @api_max_id - 1
end

#prev_uriObject Also known as: previous_uri



49
50
51
# File 'lib/drink-socially/api/pagination.rb', line 49

def prev_uri
  uri_for query: "max_id=#{prev_id}", uri: :prev
end

#prev_urlObject Also known as: previous_url



55
56
57
# File 'lib/drink-socially/api/pagination.rb', line 55

def prev_url
  prev_uri.to_s
end