Class: Flickr::Photo

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil, api_key = nil, extra_params = {}) ⇒ Photo

Returns a new instance of Photo.



401
402
403
404
405
406
# File 'lib/flickr.rb', line 401

def initialize(id=nil, api_key=nil, 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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



399
400
401
# File 'lib/flickr.rb', line 399

def client
  @client
end

#idObject (readonly)

Returns the value of attribute id.



399
400
401
# File 'lib/flickr.rb', line 399

def id
  @id
end

#titleObject

Returns the value of attribute title.



399
400
401
# File 'lib/flickr.rb', line 399

def title
  @title
end

Instance Method Details

#[](param_name) ⇒ Object

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



414
415
416
# File 'lib/flickr.rb', line 414

def [](param_name)
  instance_variable_get("@#{param_name}")
end

#add_note(note) ⇒ Object

Implements flickr.photos.notes.add



542
543
# File 'lib/flickr.rb', line 542

def add_note(note)
end

#add_tag(tag) ⇒ Object

Implements flickr.photos.addTags



564
565
# File 'lib/flickr.rb', line 564

def add_tag(tag)
end

#contextObject

Implements flickr.photos.getContext



511
512
513
514
515
516
# File 'lib/flickr.rb', line 511

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) ⇒ Object

Implements flickr.photos.setDates



546
547
# File 'lib/flickr.rb', line 546

def dates=(dates)
end

#deleteNote(note_id) ⇒ Object

Implements flickr.photos.notes.delete



581
582
# File 'lib/flickr.rb', line 581

def deleteNote(note_id)
end

#descriptionObject



453
454
455
# File 'lib/flickr.rb', line 453

def description
  @description || getInfo("description")
end

#description=(title) ⇒ Object



560
561
# File 'lib/flickr.rb', line 560

def description=(title)
end

#editNote(note_id) ⇒ Object

Implements flickr.photos.notes.edit



585
586
# File 'lib/flickr.rb', line 585

def editNote(note_id)
end

#exifObject

Implements flickr.photos.getExif



519
520
521
# File 'lib/flickr.rb', line 519

def exif
  @client.photos_getExif('photo_id'=>@id)['photo']
end

#file(size = 'Medium') ⇒ Object

Returns the photo file data itself, in any specified size. Example: File.open(photo.title, ‘w’) { |f| f.puts photo.file }



501
502
503
# File 'lib/flickr.rb', line 501

def file(size='Medium')
  Net::HTTP.get_response(URI.parse(source(size))).body
end

#filenameObject

Unique filename for the image, based on the Flickr NSID



506
507
508
# File 'lib/flickr.rb', line 506

def filename
  "#{@id}.jpg"
end

#isfavoriteObject



441
442
443
# File 'lib/flickr.rb', line 441

def isfavorite
  @isfavorite.nil? ? getInfo("isfavorite") : @isfavorite
end

#licenseObject



445
446
447
# File 'lib/flickr.rb', line 445

def license
  @license.nil? ? getInfo("license") : @license
end

#normalize_size(size) ⇒ Object

converts string or symbol size to a capitalized string



469
470
471
# File 'lib/flickr.rb', line 469

def normalize_size(size)
  size ? size.to_s.capitalize : size
end

#notesObject



457
458
459
# File 'lib/flickr.rb', line 457

def notes
  @notes.nil? ? getInfo("notes") : @notes
end

#ownerObject

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



426
427
428
429
430
431
432
433
434
435
# File 'lib/flickr.rb', line 426

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

#permissionsObject

Implements flickr.photos.getPerms



524
525
526
# File 'lib/flickr.rb', line 524

def permissions
  @client.photos_getPerms('photo_id'=>@id)['perms']
end

#perms=(perms) ⇒ Object

Implements flickr.photos.setPerms



550
551
# File 'lib/flickr.rb', line 550

def perms=(perms)
end

#postToBlog(blog_id, title = '', description = '') ⇒ Object

Implements flickr.blogs.postPhoto



576
577
578
# File 'lib/flickr.rb', line 576

def postToBlog(blog_id, title='', description='')
  @client.blogs_postPhoto('photo_id'=>@id, 'title'=>title, 'description'=>description)
end

#pretty_urlObject

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/


491
492
493
# File 'lib/flickr.rb', line 491

def pretty_url
  @url || getInfo("pretty_url")
end

#remove_tag(tag) ⇒ Object

Implements flickr.photos.removeTag



568
569
# File 'lib/flickr.rb', line 568

def remove_tag(tag)
end

#rotateObject

Implements flickr.photos.transform.rotate



572
573
# File 'lib/flickr.rb', line 572

def rotate
end

#rotationObject



449
450
451
# File 'lib/flickr.rb', line 449

def rotation
  @rotation.nil? ? getInfo("rotation") : @rotation
end

#serverObject



437
438
439
# File 'lib/flickr.rb', line 437

def server
  @server.nil? ? getInfo("server") : @server
end

#size_url(size = 'Medium') ⇒ Object

Returns the URL for the photo size page defaults to ‘Medium’ other valid sizes are in the VALID_SIZES hash



464
465
466
# File 'lib/flickr.rb', line 464

def size_url(size='Medium')
  uri_for_photo_from_self(size) || sizes(size)['url']
end

#sizes(size = nil) ⇒ Object

Implements flickr.photos.getSizes



529
530
531
532
533
534
# File 'lib/flickr.rb', line 529

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') ⇒ Object

Returns the URL for the image (default or any specified size)



496
497
498
# File 'lib/flickr.rb', line 496

def source(size='Medium')
  image_source_uri_from_self(size) || sizes(size)['source']
end

#tagsObject

flickr.tags.getListPhoto



537
538
539
# File 'lib/flickr.rb', line 537

def tags
  @client.tags_getListPhoto('photo_id'=>@id)['photo']['tags']
end

#tags=(tags) ⇒ Object

Implements flickr.photos.setTags



554
555
# File 'lib/flickr.rb', line 554

def tags=(tags)
end

#to_sObject

Converts the Photo to a string by returning its title



589
590
591
# File 'lib/flickr.rb', line 589

def to_s
  title
end

#url(size = nil) ⇒ Object

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



479
480
481
482
483
484
485
# File 'lib/flickr.rb', line 479

def url(size = nil)
  if normalize_size(size) != 'Medium'
    size_url(size)
  else
    @url || uri_for_photo_from_self
  end
end