class Flickr::Photo
Attributes
Public Class Methods
# File lib/flickr.rb, line 436 def initialize(id=nil, api_key={}, extra_params={}) @id = id @api_key = api_key extra_params.each { |k,v| self.instance_variable_set("@#{k}", v) } # convert extra_params into instance variables @client = Flickr.new @api_key end
Public Instance Methods
Allows access to all photos instance variables through hash like interface, e.g. photo returns @datetaken instance variable. Useful for accessing any weird and wonderful parameter that may have been returned by Flickr
when finding the photo, e.g. those returned by the extras argument in flickr.people.getPublicPhotos
# File lib/flickr.rb, line 449 def [](param_name) instance_variable_get("@#{param_name}") end
Implements flickr.photos.notes.add
# File lib/flickr.rb, line 577 def add_note(note) end
Implements flickr.photos.addTags
# File lib/flickr.rb, line 599 def add_tag(tag) end
Implements flickr.photos.getContext
# File lib/flickr.rb, line 546 def context context = @client.photos_getContext('photo_id'=>@id) @previousPhoto = Photo.new(context['prevphoto'].delete('id'), @api_key, context['prevphoto']) if context['prevphoto']['id']!='0' @nextPhoto = Photo.new(context['nextphoto'].delete('id'), @api_key, context['nextphoto']) if context['nextphoto']['id']!='0' return [@previousPhoto, @nextPhoto] end
Implements flickr.photos.setDates
# File lib/flickr.rb, line 581 def dates=(dates) end
Implements flickr.photos.notes.delete
# File lib/flickr.rb, line 616 def deleteNote(note_id) end
# File lib/flickr.rb, line 488 def description @description || getInfo("description") end
# File lib/flickr.rb, line 595 def description=(title) end
Implements flickr.photos.notes.edit
# File lib/flickr.rb, line 620 def editNote(note_id) end
Implements flickr.photos.getExif
# File lib/flickr.rb, line 554 def exif @client.photos_getExif('photo_id'=>@id)['photo'] end
Returns the photo file data itself, in any specified size. Example: File.open(photo.title, ‘w’) { |f| f.puts photo.file }
# File lib/flickr.rb, line 536 def file(size='Medium') Net::HTTP.get_response(URI.parse(source(size))).body end
Unique filename for the image, based on the Flickr
NSID
# File lib/flickr.rb, line 541 def filename "#{@id}.jpg" end
# File lib/flickr.rb, line 476 def isfavorite @isfavorite.nil? ? getInfo("isfavorite") : @isfavorite end
# File lib/flickr.rb, line 480 def license @license.nil? ? getInfo("license") : @license end
converts string or symbol size to a capitalized string
# File lib/flickr.rb, line 504 def normalize_size(size) size ? size.to_s.capitalize : size end
# File lib/flickr.rb, line 492 def notes @notes.nil? ? getInfo("notes") : @notes end
Returns the owner of the photo as a Flickr::User
. If we have no info about the owner, we make an API call to get it. If we already have the owner’s id, create a user based on that. Either way, we cache the result so we don’t need to check again
# File lib/flickr.rb, line 461 def owner case @owner when Flickr::User @owner when String @owner = Flickr::User.new(@owner, nil, nil, nil, @api_key) else getInfo("owner") end end
Implements flickr.photos.getPerms
# File lib/flickr.rb, line 559 def permissions @client.photos_getPerms('photo_id'=>@id)['perms'] end
Implements flickr.photos.setPerms
# File lib/flickr.rb, line 585 def perms=(perms) end
Implements flickr.blogs.postPhoto
# File lib/flickr.rb, line 611 def postToBlog(blog_id, title='', description='') @client.blogs_postPhoto('photo_id'=>@id, 'title'=>title, 'description'=>description) end
the ‘pretty’ url for a photo (if the user has set up a custom name) eg, flickr.com/photos/granth/2584402507/ instead of
http://flickr.com/photos/23386158@N00/2584402507/
# File lib/flickr.rb, line 526 def pretty_url @url || getInfo("pretty_url") end
Implements flickr.photos.removeTag
# File lib/flickr.rb, line 603 def remove_tag(tag) end
Implements flickr.photos.transform.rotate
# File lib/flickr.rb, line 607 def rotate end
# File lib/flickr.rb, line 484 def rotation @rotation.nil? ? getInfo("rotation") : @rotation end
# File lib/flickr.rb, line 472 def server @server.nil? ? getInfo("server") : @server end
Returns the URL for the photo size page defaults to ‘Medium’ other valid sizes are in the VALID_SIZES hash
# File lib/flickr.rb, line 499 def size_url(size='Medium') uri_for_photo_from_self(size) || sizes(size)['url'] end
Implements flickr.photos.getSizes
# File lib/flickr.rb, line 564 def sizes(size=nil) size = normalize_size(size) sizes = @client.photos_getSizes('photo_id'=>@id)['sizes']['size'] sizes = sizes.find{|asize| asize['label']==size} if size return sizes end
Returns the URL for the image (default or any specified size)
# File lib/flickr.rb, line 531 def source(size='Medium') image_source_uri_from_self(size) || sizes(size)['source'] end
Implements flickr.photos.setMeta
# File lib/flickr.rb, line 593 def title=(title) end
Converts the Photo
to a string by returning its title
# File lib/flickr.rb, line 624 def to_s title end
the URL for the main photo page if getInfo has already been called, this will return the pretty url
for historical reasons, an optional size can be given ‘Medium’ returns the regular url; any other size returns a size page use size_url
instead
# File lib/flickr.rb, line 514 def url(size = nil) if normalize_size(size) != 'Medium' size_url(size) else @url || uri_for_photo_from_self end end
Private Instance Methods
Implements flickr.photos.getInfo
# File lib/flickr.rb, line 631 def getInfo(attrib="") return instance_variable_get("@#{attrib}") if @got_info info = @client.photos_getInfo('photo_id'=>@id)['photo'] @got_info = true info.each { |k,v| instance_variable_set("@#{k}", v)} @owner = User.new(info['owner']['nsid'], info['owner']['username'], nil, nil, @api_key) @tags = info['tags']['tag'] @notes = info['notes']['note']#.collect { |note| Note.new(note.id) } @url = info['urls']['url']['content'] # assumes only one url instance_variable_get("@#{attrib}") end
Builds source uri of image from params (often returned from other methods, e.g. User#photos
). As specified at: www.flickr.com/services/api/misc.urls.html. If size is given should be one the keys in the VALID_SIZES hash, i.e. “Square”, “Thumbnail”, “Medium”, “Large”, “Original”, “Small” (These are the values returned by flickr.photos.getSizes). If no size is given the uri for “Medium”-size image, i.e. with width of 500 is returned TODO: Handle “Original” size
# File lib/flickr.rb, line 652 def image_source_uri_from_self(size=nil) return unless @farm&&@server&&@id&&@secret s_size = VALID_SIZES[normalize_size(size)] # get the short letters array corresponding to the size s_size = s_size&&s_size[0] # the first element of this array is used to build the source uri if s_size.nil? "http://farm#{@farm}.static.flickr.com/#{@server}/#{@id}_#{@secret}.jpg" else "http://farm#{@farm}.static.flickr.com/#{@server}/#{@id}_#{@secret}_#{s_size}.jpg" end end
Builds uri of Flickr
page for photo. By default returns the main page for the photo, but if passed a size will return the simplified flickr page featuring the given size of the photo TODO: Handle “Original” size
# File lib/flickr.rb, line 667 def uri_for_photo_from_self(size=nil) return unless @owner&&@id size = normalize_size(size) s_size = VALID_SIZES[size] # get the short letters array corresponding to the size s_size = s_size&&s_size[1] # the second element of this array is used to build the uri of the flickr page for this size "http://www.flickr.com/photos/#{owner.id}/#{@id}" + (s_size ? "/sizes/#{s_size}/" : "") end