Class: Flickr::Photos

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/simple-flickr/photos.rb

Overview

A collection of Photos.

Instance Method Summary collapse

Constructor Details

#initialize(result_xml, client) ⇒ Photos

Create a new Flickr::Photos collection using the provided Hpricot::Elem object, which should contain an appropriate Photo response in XML from Flickr.



36
37
38
39
# File 'lib/simple-flickr/photos.rb', line 36

def initialize( result_xml, client )
  @photos = result_xml.search( 'photo' ).collect { |xml| Flickr::Photo.new( xml, client ) }
  @attributes = result_xml.attributes
end

Instance Method Details

#[](n) ⇒ Object

Allow Photos to be accessed as an Array



58
59
60
# File 'lib/simple-flickr/photos.rb', line 58

def [](n)
  to_a[n]
end

#current_pageObject



10
11
12
# File 'lib/simple-flickr/photos.rb', line 10

def current_page
  @attributes['page'].to_i
end

#eachObject

Iterate through all the Flickr::Photo objects.



42
43
44
# File 'lib/simple-flickr/photos.rb', line 42

def each
  @photos.each { |photo| yield photo }
end

#empty?Boolean

Is this collection of photos empty?

Returns:

  • (Boolean)


47
48
49
# File 'lib/simple-flickr/photos.rb', line 47

def empty?
  to_a.empty?
end

#next_pageObject



26
27
28
# File 'lib/simple-flickr/photos.rb', line 26

def next_page
  total_pages > current_page ? current_page+1 : nil
end

#offsetObject



6
7
8
# File 'lib/simple-flickr/photos.rb', line 6

def offset
  (current_page-1) * per_page
end

#per_pageObject



14
15
16
# File 'lib/simple-flickr/photos.rb', line 14

def per_page
  (@attributes['perpage'] || @attributes['per_page']).to_i
end

#previous_pageObject



30
31
32
# File 'lib/simple-flickr/photos.rb', line 30

def previous_page
  current_page > 1 ? current_page-1 : nil
end

#sizeObject Also known as: length

How big is this collection?



52
53
54
# File 'lib/simple-flickr/photos.rb', line 52

def size
  to_a.size
end

#total_entriesObject



18
19
20
# File 'lib/simple-flickr/photos.rb', line 18

def total_entries
  @attributes['total'].to_i
end

#total_pagesObject



22
23
24
# File 'lib/simple-flickr/photos.rb', line 22

def total_pages
  (total_entries.to_f / per_page).ceil
end