class Flickr::Photo

Attributes

client[R]
id[R]
title[R]

Public Class Methods

new(id=nil, api_key={}, extra_params={}) click to toggle source
# 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

[](param_name) click to toggle source

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
add_note(note) click to toggle source

Implements flickr.photos.notes.add

# File lib/flickr.rb, line 577
def add_note(note)
end
add_tag(tag) click to toggle source

Implements flickr.photos.addTags

# File lib/flickr.rb, line 599
def add_tag(tag)
end
context() click to toggle source

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
dates=(dates) click to toggle source

Implements flickr.photos.setDates

# File lib/flickr.rb, line 581
def dates=(dates)
end
deleteNote(note_id) click to toggle source

Implements flickr.photos.notes.delete

# File lib/flickr.rb, line 616
def deleteNote(note_id)
end
description() click to toggle source
# File lib/flickr.rb, line 488
def description
  @description || getInfo("description")
end
description=(title) click to toggle source
# File lib/flickr.rb, line 595
def description=(title)
end
editNote(note_id) click to toggle source

Implements flickr.photos.notes.edit

# File lib/flickr.rb, line 620
def editNote(note_id)
end
exif() click to toggle source

Implements flickr.photos.getExif

# File lib/flickr.rb, line 554
def exif
  @client.photos_getExif('photo_id'=>@id)['photo']
end
file(size='Medium') click to toggle source

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
filename() click to toggle source

Unique filename for the image, based on the Flickr NSID

# File lib/flickr.rb, line 541
def filename
  "#{@id}.jpg"
end
isfavorite() click to toggle source
# File lib/flickr.rb, line 476
def isfavorite
  @isfavorite.nil? ? getInfo("isfavorite") : @isfavorite
end
license() click to toggle source
# File lib/flickr.rb, line 480
def license
  @license.nil? ? getInfo("license") : @license
end
normalize_size(size) click to toggle source

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
notes() click to toggle source
# File lib/flickr.rb, line 492
def notes
  @notes.nil? ? getInfo("notes") : @notes
end
owner() click to toggle source

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
permissions() click to toggle source

Implements flickr.photos.getPerms

# File lib/flickr.rb, line 559
def permissions
  @client.photos_getPerms('photo_id'=>@id)['perms']
end
perms=(perms) click to toggle source

Implements flickr.photos.setPerms

# File lib/flickr.rb, line 585
def perms=(perms)
end
postToBlog(blog_id, title='', description='') click to toggle source

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
pretty_url() click to toggle source

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
remove_tag(tag) click to toggle source

Implements flickr.photos.removeTag

# File lib/flickr.rb, line 603
def remove_tag(tag)
end
rotate() click to toggle source

Implements flickr.photos.transform.rotate

# File lib/flickr.rb, line 607
def rotate
end
rotation() click to toggle source
# File lib/flickr.rb, line 484
def rotation
  @rotation.nil? ? getInfo("rotation") : @rotation
end
server() click to toggle source
# File lib/flickr.rb, line 472
def server
  @server.nil? ? getInfo("server") : @server
end
size_url(size='Medium') click to toggle source

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
sizes(size=nil) click to toggle source

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
source(size='Medium') click to toggle source

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
tags() click to toggle source

flickr.tags.getListPhoto

# File lib/flickr.rb, line 572
def tags
  @client.tags_getListPhoto('photo_id'=>@id)['photo']['tags']
end
tags=(tags) click to toggle source

Implements flickr.photos.setTags

# File lib/flickr.rb, line 589
def tags=(tags)
end
title=(title) click to toggle source

Implements flickr.photos.setMeta

# File lib/flickr.rb, line 593
def title=(title)
end
to_s() click to toggle source

Converts the Photo to a string by returning its title

# File lib/flickr.rb, line 624
def to_s
  title
end
url(size = nil) click to toggle source

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

getInfo(attrib="") click to toggle source

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
image_source_uri_from_self(size=nil) click to toggle source

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
uri_for_photo_from_self(size=nil) click to toggle source

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