Class: Nimbu::Pagination::PageLinks

Inherits:
Object
  • Object
show all
Includes:
Utils::Constants
Defined in:
lib/nimbu-api/pagination/page_links.rb

Constant Summary collapse

",".freeze

Constants included from Utils::Constants

Utils::Constants::ACCEPT, Utils::Constants::ACCEPTED_OAUTH_SCOPES, Utils::Constants::ACCEPT_CHARSET, Utils::Constants::CACHE_CONTROL, Utils::Constants::CONTENT_LENGTH, Utils::Constants::CONTENT_TYPE, Utils::Constants::DATE, Utils::Constants::ETAG, Utils::Constants::HEADER_LAST, Utils::Constants::HEADER_LINK, Utils::Constants::HEADER_NEXT, Utils::Constants::LOCATION, Utils::Constants::META_FIRST, Utils::Constants::META_LAST, Utils::Constants::META_NEXT, Utils::Constants::META_PREV, Utils::Constants::META_REL, Utils::Constants::NIMBU_SITE, Utils::Constants::OAUTH_SCOPES, Utils::Constants::PARAM_PAGE, Utils::Constants::PARAM_PER_PAGE, Utils::Constants::PARAM_START_PAGE, Utils::Constants::RATELIMIT_LIMIT, Utils::Constants::RATELIMIT_REMAINING, Utils::Constants::SERVER, Utils::Constants::USER_AGENT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response_headers) ⇒ PageLinks

Parses links from executed request



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
39
40
# File 'lib/nimbu-api/pagination/page_links.rb', line 14

def initialize(response_headers)
  link_header = response_headers[HEADER_LINK]
  if link_header
    return unless link_header =~ /(next|first|last|prev)/

    link_header.split(DELIM_LINKS).each do |link|
      if link.strip =~ /<([^>]+)>; rel=\"([^\"]+)\"/
        url_part, meta_part = $1, $2
        next if !url_part || !meta_part
        case meta_part
        when META_FIRST
          self.first = url_part
        when META_LAST
          self.last = url_part
        when META_NEXT
          self.next = url_part
        when META_PREV
          self.prev = url_part
        end
      end
    end
  else
    # When on the first page
    self.next = response_headers[HEADER_NEXT]
    self.last = response_headers[HEADER_LAST]
  end
end

Instance Attribute Details

#firstObject

Hold the extracted values for URI from the Link header for the first, last, next and previous page.



10
11
12
# File 'lib/nimbu-api/pagination/page_links.rb', line 10

def first
  @first
end

#lastObject

Hold the extracted values for URI from the Link header for the first, last, next and previous page.



10
11
12
# File 'lib/nimbu-api/pagination/page_links.rb', line 10

def last
  @last
end

#nextObject

Hold the extracted values for URI from the Link header for the first, last, next and previous page.



10
11
12
# File 'lib/nimbu-api/pagination/page_links.rb', line 10

def next
  @next
end

#prevObject

Hold the extracted values for URI from the Link header for the first, last, next and previous page.



10
11
12
# File 'lib/nimbu-api/pagination/page_links.rb', line 10

def prev
  @prev
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/nimbu-api/pagination/page_links.rb', line 42

def any?
  self.first || self.last || self.next || self.prev
end