Class: HomeAway::API::Paginator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/homeaway/api/paginator.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, hashie, auto_pagination = false) ⇒ Paginator

Returns a new instance of Paginator.



22
23
24
25
26
# File 'lib/homeaway/api/paginator.rb', line 22

def initialize(client, hashie, auto_pagination=false)
  @hashie = hashie
  @client = client
  @auto_pagination = auto_pagination
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



116
117
118
119
120
121
122
# File 'lib/homeaway/api/paginator.rb', line 116

def method_missing(name, *args, &block)
  if @hashie.respond_to? name
    @hashie.send name, *args, &block
  else
    super
  end
end

Instance Method Details

#each(&block) ⇒ Object

supply a block that expects a single parameter to iterate through this paginator



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/homeaway/api/paginator.rb', line 29

def each(&block)
  if @auto_pagination
    first_hashie = @hashie.clone
    while true
      cur_hits = entries.clone
      cur_hits.each do |entity|
        if block_given?
          block.call entity
        else
          yield entity
        end
      end
      if @hashie.nextPage && @hashie.page < 300
        begin
          next_page!
        rescue HomeAway::API::Errors::RequestedRangeNotSatisfiableError
          # we have a max page limit
          # reset me to the first so that I can be iterated over again
          @hashie = first_hashie
          break
        end
      else
        # reset me to the first so that I can be iterated over again
        @hashie = first_hashie
        break
      end
    end
  else
    entries.each do |entity|
      if block_given?
        block.call entity
      else
        yield entity
      end
    end
  end
end

#entriesArray Also known as: hits

Returns the current page of results as an array of entities.

Returns:

  • (Array)

    the current page of results as an array of entities



125
126
127
128
# File 'lib/homeaway/api/paginator.rb', line 125

def entries
  entries = @hashie['entries'] ||= nil
  entries.map { |entry| HomeAway::API::Response.new(entry) }
end

#next_pageHomeAway::API::Paginator

Returns a paginator object that has the next page of results.

Returns:



76
77
78
79
80
# File 'lib/homeaway/api/paginator.rb', line 76

def next_page
  return nil unless next_page?
  next_hashie = @client.get(*parse_url(nextPage))
  self.class.new(@client, next_hashie, @auto_pagination)
end

#next_page!Boolean

updates this paginator to have the next page of results in place

Returns:

  • (Boolean)

    true if successful



84
85
86
87
88
# File 'lib/homeaway/api/paginator.rb', line 84

def next_page!
  return false unless next_page?
  @hashie = @client.get(*parse_url(nextPage))
  true
end

#next_page?Boolean

Returns does this paginator have another page?.

Returns:

  • (Boolean)

    does this paginator have another page?



91
92
93
# File 'lib/homeaway/api/paginator.rb', line 91

def next_page?
  @hashie.has_key? 'nextPage'
end

#previous_pageHomeAway::API::Paginator

Returns a paginator object that has the previous page of results.

Returns:



96
97
98
99
100
# File 'lib/homeaway/api/paginator.rb', line 96

def previous_page
  return nil unless previous_page?
  prev_hashie = @client.get(*parse_url(prevPage))
  self.class.new(@client, prev_hashie, @auto_pagination)
end

#previous_page!Boolean

updates this paginator to have the previous page of results in place

Returns:

  • (Boolean)

    true if successful



104
105
106
107
108
# File 'lib/homeaway/api/paginator.rb', line 104

def previous_page!
  return false unless previous_page?
  @hashie = @client.get(*parse_url(prevPage))
  true
end

#previous_page?Boolean

Returns does this paginator have previous page?.

Returns:

  • (Boolean)

    does this paginator have previous page?



111
112
113
# File 'lib/homeaway/api/paginator.rb', line 111

def previous_page?
  @hashie.has_key? 'prevPage'
end

#sizeInteger Also known as: length

Returns the size of this paginator.

Returns:

  • (Integer)

    the size of this paginator



68
69
70
71
# File 'lib/homeaway/api/paginator.rb', line 68

def size
  return @hashie['size'] if @hashie.has_key? 'size'
  0
end