Class: Flickry::Photo

Inherits:
Base show all
Defined in:
lib/flickry/photo.rb

Instance Method Summary collapse

Methods inherited from Base

#attributes

Methods inherited from SuperStruct

#[], #[]=, #each, #each_pair, #members

Constructor Details

#initialize(flickr_id) ⇒ Photo

Returns a new instance of Photo.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/flickry/photo.rb', line 3

def initialize(flickr_id)
  super(nil)
  foto = flickr.photos.getInfo(:photo_id => flickr_id)
  self.raw_photo = foto
  self.photo_id = flickr_id

  extract_attrs!(foto, [:dateuploaded, :description, :farm, :id, :isfavorite,
    :media, :notes, :originalformat, :originalsecret, :rotation, :secret, :server, :tags,
    :title])
  extract_attrs_into_substructs!(foto, {
    :dates        => [:lastupdate, :posted, :taken, :takengranularity],
    :editability  => [:canaddmeta, :cancomment],
    :geoperms     => [:iscontact, :isfamily, :isfriend, :ispublic],
    :usage        => [:canblog, :candownload, :canprint],
    :visibility   => [:isfamily, :isfriend, :ispublic]
  })
  self.location = Flickry::Location.new(foto.respond_to?(:location) ? foto.location : nil)
  self.owner    = Flickry::Person.find(foto.respond_to?(:owner) ? foto.owner.nsid : nil)
  self.license  = Flickry::License.new(foto.respond_to?(:license) ? foto.license : 99)
  _tags = self.tags.dup
  self.tags = _tags.collect do |t|
    Flickry::Tag.new(t)
  end
  self.urls = foto.urls.collect { |u| u.to_s }
end

Instance Method Details

#commentsObject

Lazily fetches the photo’s comments when called, memoizes so later calls are faster…



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/flickry/photo.rb', line 39

def comments
  return @comments if @comments
  
  if raw_photo.comments.to_i == 0
    @comments = []
  else
    @comments = []
    flickr_comments = flickr.photos.comments.getList(:photo_id => self.photo_id)
    flickr_comments.each do |comment|
      @comments << Flickry::Comment.new(comment)
    end
  end
  return @comments
end

#favorite?Boolean

W Booleans

Returns:

  • (Boolean)


82
83
84
# File 'lib/flickry/photo.rb', line 82

def favorite?
  self.isfavorite == 1
end

#last_updated_atObject

Time/Dates



55
56
57
# File 'lib/flickry/photo.rb', line 55

def last_updated_at
  dates_to_time(:lastupdate)
end

#posted_atObject



59
60
61
# File 'lib/flickry/photo.rb', line 59

def posted_at
  dates_to_time(:posted)
end

#sizesObject

Lazily fetches the photo’s sizes when called, memoizes so later calls are faster…



32
33
34
# File 'lib/flickry/photo.rb', line 32

def sizes
  @sizes ||= Flickry::Sizes.new(flickr.photos.getSizes(:photo_id => self.photo_id))
end

#tag_list(separator = ', ') ⇒ Object

Join tags into a single string



76
77
78
# File 'lib/flickry/photo.rb', line 76

def tag_list(separator = ', ')
  self.tags.join(separator)
end

#taken_atObject



63
64
65
# File 'lib/flickry/photo.rb', line 63

def taken_at
  Time.parse(self.dates.taken)
end

#uploaded_atObject



67
68
69
70
71
# File 'lib/flickry/photo.rb', line 67

def uploaded_at
  uploaded = self.dateuploaded.to_i
  return nil if uploaded == 0
  Time.at(uploaded)    
end

#visible_to_family?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/flickry/photo.rb', line 86

def visible_to_family?
  self.visibility.isfamily == 1
end

#visible_to_friends?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/flickry/photo.rb', line 90

def visible_to_friends?
  self.visibility.isfriend == 1
end

#visible_to_public?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/flickry/photo.rb', line 94

def visible_to_public?
  self.visibility.ispublic == 1
end