Class: Thingiverse::Pagination

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/thingiverse/pagination.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, object) ⇒ Pagination

Returns a new instance of Pagination.



11
12
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/thingiverse/pagination.rb', line 11

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

  # TODO: provide more debug info and raise a custom exception
  raise "#{@response.code}: #{JSON.parse(@response.body)['error']}" unless @response.success?
  
  @objects = @response.parsed_response.collect do |attrs|
    @object.new attrs
  end

  if @response.headers.include?("link")
    @response.headers["link"].split(",").each do |link|
      url, rel = link.split(";").collect{|p| p.gsub(/\<|\>|rel\=|\"/,'').strip}
      instance_variable_set("@#{rel}_url", url)
      url_params = CGI.parse(URI.parse(url).query.to_s)

      case rel
      when "last"
        @total_pages = url_params["page"][0].to_i
      when "next"
        @current_page = url_params["page"][0].to_i - 1
      when "prev"
        @current_page = url_params["page"][0].to_i + 1
      end
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



42
43
44
45
46
47
48
# File 'lib/thingiverse/pagination.rb', line 42

def method_missing(meth, *args, &block)
  if meth.to_s =~ /^(.*)_page$/
    get_url_page($1, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



9
10
11
# File 'lib/thingiverse/pagination.rb', line 9

def current_page
  @current_page
end

#first_urlObject (readonly)

Returns the value of attribute first_url.



9
10
11
# File 'lib/thingiverse/pagination.rb', line 9

def first_url
  @first_url
end

#last_urlObject (readonly)

Returns the value of attribute last_url.



9
10
11
# File 'lib/thingiverse/pagination.rb', line 9

def last_url
  @last_url
end

#next_urlObject (readonly)

Returns the value of attribute next_url.



9
10
11
# File 'lib/thingiverse/pagination.rb', line 9

def next_url
  @next_url
end

#objectObject (readonly)

Returns the value of attribute object.



8
9
10
# File 'lib/thingiverse/pagination.rb', line 8

def object
  @object
end

#prev_urlObject (readonly)

Returns the value of attribute prev_url.



9
10
11
# File 'lib/thingiverse/pagination.rb', line 9

def prev_url
  @prev_url
end

#responseObject (readonly)

Returns the value of attribute response.



8
9
10
# File 'lib/thingiverse/pagination.rb', line 8

def response
  @response
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



9
10
11
# File 'lib/thingiverse/pagination.rb', line 9

def total_pages
  @total_pages
end

Instance Method Details

#each(&block) ⇒ Object



55
56
57
# File 'lib/thingiverse/pagination.rb', line 55

def each(&block)
  @objects.each(&block)
end

#get_url_page(which, *args, &block) ⇒ Object



50
51
52
53
# File 'lib/thingiverse/pagination.rb', line 50

def get_url_page(which, *args, &block)
  url = instance_variable_get("@#{which}_url")
  Thingiverse::Pagination.new(Thingiverse::Connection.get(url), @object) if url
end