Class: Flickr::Photos::Photo

Inherits:
Object
  • Object
show all
Defined in:
lib/flickr/photo.rb

Overview

wrapping class to hold an flickr photo

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flickr, attributes) ⇒ Photo

create a new instance of a flickr photo.

Params

  • flickr (Required)

    the flickr object
    
  • attributes (Required)

    a hash of attributes used to set the initial values of the photo object
    


44
45
46
47
48
49
# File 'lib/flickr/photo.rb', line 44

def initialize(flickr, attributes)
  @flickr = flickr
  attributes.each do |k,v|
    send("#{k}=", v)
  end
end

Instance Attribute Details

#commentsObject

:nodoc:



35
36
37
# File 'lib/flickr/photo.rb', line 35

def comments
  @comments
end

#descriptionObject

:nodoc:



280
281
282
# File 'lib/flickr/photo.rb', line 280

def description
  @description
end

#farmObject

Returns the value of attribute farm.



3
4
5
# File 'lib/flickr/photo.rb', line 3

def farm
  @farm
end

#geoObject

Returns the value of attribute geo.



12
13
14
# File 'lib/flickr/photo.rb', line 12

def geo
  @geo
end

#icon_serverObject

Returns the value of attribute icon_server.



12
13
14
# File 'lib/flickr/photo.rb', line 12

def icon_server
  @icon_server
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/flickr/photo.rb', line 3

def id
  @id
end

#info_addedObject

Returns the value of attribute info_added.



29
30
31
# File 'lib/flickr/photo.rb', line 29

def info_added
  @info_added
end

#is_familyObject

Returns the value of attribute is_family.



3
4
5
# File 'lib/flickr/photo.rb', line 3

def is_family
  @is_family
end

#is_friendObject

Returns the value of attribute is_friend.



3
4
5
# File 'lib/flickr/photo.rb', line 3

def is_friend
  @is_friend
end

#is_publicObject

Returns the value of attribute is_public.



3
4
5
# File 'lib/flickr/photo.rb', line 3

def is_public
  @is_public
end

#license_idObject

Returns the value of attribute license_id.



12
13
14
# File 'lib/flickr/photo.rb', line 12

def license_id
  @license_id
end

#machine_tagsObject

Returns the value of attribute machine_tags.



12
13
14
# File 'lib/flickr/photo.rb', line 12

def machine_tags
  @machine_tags
end

#mediaObject

Returns the value of attribute media.



12
13
14
# File 'lib/flickr/photo.rb', line 12

def media
  @media
end

#notesObject

:nodoc:



341
342
343
# File 'lib/flickr/photo.rb', line 341

def notes
  @notes
end

#o_dimsObject

Returns the value of attribute o_dims.



12
13
14
# File 'lib/flickr/photo.rb', line 12

def o_dims
  @o_dims
end

#o_heightObject

Returns the value of attribute o_height.



12
13
14
# File 'lib/flickr/photo.rb', line 12

def o_height
  @o_height
end

#o_widthObject

Returns the value of attribute o_width.



12
13
14
# File 'lib/flickr/photo.rb', line 12

def o_width
  @o_width
end

#original_formatObject

Returns the value of attribute original_format.



12
13
14
# File 'lib/flickr/photo.rb', line 12

def original_format
  @original_format
end

#original_secretObject

:nodoc:



285
286
287
# File 'lib/flickr/photo.rb', line 285

def original_secret
  @original_secret
end

#ownerObject

Returns the value of attribute owner.



3
4
5
# File 'lib/flickr/photo.rb', line 3

def owner
  @owner
end

#owner_nameObject

Returns the value of attribute owner_name.



12
13
14
# File 'lib/flickr/photo.rb', line 12

def owner_name
  @owner_name
end

#owner_realnameObject

:nodoc:



295
296
297
# File 'lib/flickr/photo.rb', line 295

def owner_realname
  @owner_realname
end

#owner_usernameObject

:nodoc:



290
291
292
# File 'lib/flickr/photo.rb', line 290

def owner_username
  @owner_username
end

#rotationObject

Returns the value of attribute rotation.



12
13
14
# File 'lib/flickr/photo.rb', line 12

def rotation
  @rotation
end

#secretObject

Returns the value of attribute secret.



3
4
5
# File 'lib/flickr/photo.rb', line 3

def secret
  @secret
end

#serverObject

Returns the value of attribute server.



3
4
5
# File 'lib/flickr/photo.rb', line 3

def server
  @server
end

#tagsObject

Returns the value of attribute tags.



12
13
14
# File 'lib/flickr/photo.rb', line 12

def tags
  @tags
end

#taken_atObject

Returns the value of attribute taken_at.



12
13
14
# File 'lib/flickr/photo.rb', line 12

def taken_at
  @taken_at
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/flickr/photo.rb', line 3

def title
  @title
end

#updated_atObject

Returns the value of attribute updated_at.



12
13
14
# File 'lib/flickr/photo.rb', line 12

def updated_at
  @updated_at
end

#uploaded_atObject

Returns the value of attribute uploaded_at.



12
13
14
# File 'lib/flickr/photo.rb', line 12

def uploaded_at
  @uploaded_at
end

#url_photopageObject

:nodoc:



300
301
302
# File 'lib/flickr/photo.rb', line 300

def url_photopage
  @url_photopage
end

#viewsObject

Returns the value of attribute views.



12
13
14
# File 'lib/flickr/photo.rb', line 12

def views
  @views
end

Class Method Details

.create_attributes(photo) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/flickr/photo.rb', line 50

def self.create_attributes(photo)
  {:id => photo['id'], 
    :secret => photo['secret'], 
    :server => photo['server'], 
    :farm => photo['farm'],
    :license_id => photo['license'],
    :rotation => photo['rotation'],
    :uploaded_at => (Time.at(photo['dateuploaded'].to_i) rescue nil),
    :owner => photo.owner['nsid'],
    :owner_username => photo.owner['username'],
    :owner_realname => photo.owner['realname'],
    :icon_server => photo.owner['iconserver'],
    :title => (photo.title.text rescue ''),
    :description => photo.at_xpath("description").text, # description is a method of XML::Node, so we use at_xpath instead
    :original_secret => photo['originalsecret'],
    :original_format => photo['originalformat'],


    :is_public => photo.visibility['ispublic'].to_i, 
    :is_friend => photo.visibility['isfriend'].to_i, 
    :is_family => photo.visibility['isfamily'].to_i,

    :taken_at => (Time.parse(photo.dates['taken']) rescue nil),
    :updated_at => (Time.at(photo.dates['lastupdate'].to_i) rescue nil),
    :tags => (photo.tags.xpath('tag') || []).map { |t|
      { :id => t['id'], :author => t['author'], 
        :raw => t['raw'], :machine_tag => t['machine_tag'],
        :text => t.text }},

    :url_photopage => photo.urls.url.text
 }
end

Instance Method Details

#add_comment(message) ⇒ Object

Add comment to a photo as the currently authenticated user.

Params

  • message (Required)

    text of the comment
    


206
207
208
209
# File 'lib/flickr/photo.rb', line 206

def add_comment(message)
  @flickr.send_request('flickr.photos.comments.addComment', {:photo_id => self.id, :comment_text => message}, :post)
  true
end

#add_note(message, x, y, w, h) ⇒ Object

Add a note to a photo. Coordinates and sizes are in pixels, based on the 500px image size shown on individual photo pages.

Params

  • message (Required)

    The text of the note
    
  • x (Required)

    The left coordinate of the note
    
  • y (Required)

    The top coordinate of the note
    
  • w (Required)

    The width of the note
    
  • h (Required)

    The height of the note
    


225
226
227
228
# File 'lib/flickr/photo.rb', line 225

def add_note(message, x, y, w, h)
  @flickr.send_request('flickr.photos.notes.add', {:photo_id => self.id, :note_x => x, :note_y => y, :note_w => w, :note_h => h, :note_text => message}, :post)
  true
end

#add_tags(tags) ⇒ Object

Add tags to a photo.

Params

  • tags (Required)

    comma seperated list of tags
    


195
196
197
198
# File 'lib/flickr/photo.rb', line 195

def add_tags(tags)
  @flickr.send_request('flickr.photos.addTags', {:photo_id => self.id, :tags => tags}, :post)
  true
end

#deleteObject

Delete this photo



184
185
186
187
# File 'lib/flickr/photo.rb', line 184

def delete()
  @flickr.send_request('flickr.photos.delete', {:photo_id => self.id}, :post)
  true
end

#image_url(size = :medium) ⇒ Object

retreive the url to the image stored on flickr

Params

  • size (Optional)

    the size of the image to return. Optional sizes are:
      :square - square 75x75
      :large_square - square 150x150
      :thumbnail - 100 on longest side
      :small - 240 on longest side
      :small_320 - 320 on longest side
      :medium - 500 on longest side
      :medium_640 - 640 on longest side
      :medium_800 - 800 on longest side
      :large - 1024 on longest side (only exists for very large original images)
      :original - original image, either a jpg, gif or png, depending on source format
    


125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/flickr/photo.rb', line 125

def image_url(size = :medium)
	# It turns out that flickr always stores all the sizes of the picture even when getSizes call returns otherwise.
	# Not calling getSizes is also very important for performance reasons.
	# Retrieving 30 search results means calling the API 31 times if you call getSizes every time.
	# Mind that you still need to call getSizes if you go out for the original image.
	if size == :original
 size_hash[size.to_s].source if size_hash.has_key? size.to_s
	else
 key = "_#{size_key(size.to_sym)}"
 key = "" if key == "_"
 "http://farm#{farm}.static.flickr.com/#{server}/#{id}_#{secret}#{key}.jpg"
	end
end

#licenseObject

return the license associated with the photo



243
244
245
# File 'lib/flickr/photo.rb', line 243

def license
  @flickr.photos.licenses[self.license_id]
end

#locationObject

Returns the location of the photo (if available) or nil if photo is not geo-tagged.



249
250
251
252
253
254
255
256
257
258
259
# File 'lib/flickr/photo.rb', line 249

def location
  begin
    @location ||= @flickr.photos.geo.get_location(self.id)
  rescue Flickr::Error => e
    if e.code == 2 # 2: Photo has no location information.
      return nil
    else
      raise e
    end
  end
end

#location=(location) ⇒ Object



261
262
263
264
265
266
267
268
# File 'lib/flickr/photo.rb', line 261

def location= location
  if !location.nil?
    @flickr.photos.geo.set_location(self.id, location.latitude, location.longitude, location.accuracy)
  else
    @flickr.photos.geo.remove_location(self.id)
  end
  @location = location
end

#photo_size(size = :medium) ⇒ Object

returns an instance of Flickr::Photos::Size for the required size

Params

* size (Optional)
  the size of the size instance to return.  Optional sizes are:
     :square - square 75x75
     :large_square - square 150x150
     :thumbnail - 100 on longest side
     :small - 240 on longest side
     :small_320 - 320 on longest side
     :medium - 500 on longest side
     :medium_640 - 640 on longest side
     :medium_800 - 800 on longest side
     :large - 1024 on longest side (only exists for very large original images)
     :original - original image, either a jpg, gif or png, depending on source format

Examples

Photo.photo_size(:square).source
Photo.photo_size(:large).width


105
106
107
# File 'lib/flickr/photo.rb', line 105

def photo_size(size = :medium)
  size_hash.fetch(size.to_s, size_hash['medium'])
end

#photopage_urlObject



139
140
141
142
# File 'lib/flickr/photo.rb', line 139

def photopage_url
	# Keeping the same convention as image_url (foo_url)
	url_photopage
end

#rotate(degrees) ⇒ Object

Rotate a photo.

Params

  • degrees (Required)

    The amount of degrees by which to rotate the photo (clockwise) from it's current orientation. Valid values are 90, 180 and 270.
    


236
237
238
239
# File 'lib/flickr/photo.rb', line 236

def rotate(degrees)
  @flickr.send_request('flickr.photos.transform.rotate', {:photo_id => self.id, :degrees => degrees}, :post)
  true
end

#save_as(filename, size = :medium) ⇒ Object

save the current photo to the local computer

Params

  • filename (Required)

    name of the new file omiting the extention (ex. photo_1)
    
  • size (Optional)

    the size of the image to return. Optional sizes are:
      :square - square 75x75
      :large_square - square 150x150
      :thumbnail - 100 on longest side
      :small - 240 on longest side
      :small_320 - 320 on longest side
      :medium - 500 on longest side
      :medium_640 - 640 on longest side
      :medium_800 - 800 on longest side
      :large - 1024 on longest side (only exists for very large original images)
      :original - original image, either a jpg, gif or png, depending on source format
    


168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/flickr/photo.rb', line 168

def save_as(filename, size = :medium)
  format = size.to_sym == :original ? (self.original_format || 'jpg') : 'jpg'
  filename = "#{filename}.#{format}"

  if File.exists?(filename) or not self.url(size)
    false
  else
    f = File.new(filename, 'w+')
    f.puts open(self.url(size)).read
    f.close
    f
  end
end

#set_license(license_id) ⇒ Object

Sets the license for a photo.

Params

  • license_id (Required)

    The license to apply, or 0 (zero) to remove the current license.
    


275
276
277
278
# File 'lib/flickr/photo.rb', line 275

def set_license(license_id)
  @flickr.send_request('flickr.photos.licenses.setLicense', {:photo_id => self.id, :license_id => license_id}, :post)
  true
end

#size_hashObject



346
347
348
349
350
351
352
353
354
# File 'lib/flickr/photo.rb', line 346

def size_hash
  @size_hash ||= begin
    hash = {}
    sizes.each do |size|
      hash[size.label.downcase] = size
    end
    hash
  end
end

#sizesObject

:nodoc:



328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/flickr/photo.rb', line 328

def sizes # :nodoc:
  @sizes ||= begin
    rsp = @flickr.send_request('flickr.photos.getSizes', :photo_id => self.id)
    
    _sizes = []
    rsp.sizes.size.each do |size|
      _sizes << Flickr::Photos::Size.new(:label => size[:label], :width => size[:width].to_i,
        :height => size[:height].to_i, :source => size[:source], :url => size[:url])
    end
    _sizes
  end
end

#url(size = :medium) ⇒ Object

Alias to image_url method



83
84
85
# File 'lib/flickr/photo.rb', line 83

def url(size = :medium)
  image_url(size)
end

#video_urlObject



144
145
146
147
148
# File 'lib/flickr/photo.rb', line 144

def video_url
  if size_hash['video player']
    size_hash['video player'].source
  end
end