class Shrine::Storage::Flickr

Attributes

album[R]
flickr[R]
person[R]
upload_options[R]

Public Class Methods

new(user:, access_token:, album: nil, upload_options: {}, store_info: nil) click to toggle source
# File lib/shrine/storage/flickr.rb, line 10
def initialize(user:, access_token:, album: nil, upload_options: {}, store_info: nil)
  @flickr = ::Flickr.new(*access_token)
  @person = @flickr.people.find(user)
  @upload_options = upload_options
  @store_info = store_info
  @album = @flickr.sets.find(album) if album
end

Public Instance Methods

clear!() click to toggle source
# File lib/shrine/storage/flickr.rb, line 64
def clear!
  if album
    album.photos.each(&:delete)
  else
    person.photos.each(&:delete)
  end
end
delete(id) click to toggle source
# File lib/shrine/storage/flickr.rb, line 50
def delete(id)
  photo(photo_id(id)).delete
rescue ::Flickr::ApiError
end
exists?(id) click to toggle source
# File lib/shrine/storage/flickr.rb, line 43
def exists?(id)
  !!photo(photo_id(id)).get_info!
rescue ::Flickr::ApiError => error
  raise error if error.code != 1
  false
end
open(id, **options) click to toggle source
# File lib/shrine/storage/flickr.rb, line 37
def open(id, **options)
  Down::Http.open(url(id, size: "Original"), **options)
rescue Down::NotFound
  raise Shrine::FileNotFound, "file #{id.inspect} not found on storage"
end
update(id, options = {}) click to toggle source
# File lib/shrine/storage/flickr.rb, line 33
def update(id, options = {})
  photo(photo_id(id)).set_meta(options)
end
upload(io, id, shrine_metadata: {}, **upload_options) click to toggle source
# File lib/shrine/storage/flickr.rb, line 18
def upload(io, id, shrine_metadata: {}, **upload_options)
  options = { title: shrine_metadata["filename"] }
  options.update(@upload_options)
  options.update(upload_options)

  photo_id = flickr.upload(io, options)
  album.add_photo(photo_id) if album

  photo = photo(photo_id).get_info!
  update_id!(photo, id)
  update_metadata!(photo, shrine_metadata)

  photo.attributes
end
url(id, size: nil, **options) click to toggle source
# File lib/shrine/storage/flickr.rb, line 55
def url(id, size: nil, **options)
  if size
    size = size.to_s.tr("_", " ").capitalize if size.is_a?(Symbol)
    "https://farm%s.staticflickr.com/%s/%s_%s%s.%s" % size_data(size, id)
  else
    "https://www.flickr.com/photos/#{person.id}/#{photo_id(id)}"
  end
end

Protected Instance Methods

photo(id) click to toggle source
# File lib/shrine/storage/flickr.rb, line 74
def photo(id)
  flickr.photos.find(id)
end

Private Instance Methods

photo_id(id) click to toggle source
# File lib/shrine/storage/flickr.rb, line 110
def photo_id(id)
  id.split("-").fetch(2)
end
size_data(size, id) click to toggle source
# File lib/shrine/storage/flickr.rb, line 80
def size_data(size, id)
  farm, server, photo_id, secret, original_secret, original_format = id.split(/\W/)

  case size
  when "Square 75"  then [farm, server, photo_id, secret, "_s", "jpg"]
  when "Square 150" then [farm, server, photo_id, secret, "_q", "jpg"]
  when "Thumbnail"  then [farm, server, photo_id, secret, "_t", "jpg"]
  when "Small 240"  then [farm, server, photo_id, secret, "_m", "jpg"]
  when "Small 320"  then [farm, server, photo_id, secret, "_n", "jpg"]
  when "Medium 500" then [farm, server, photo_id, secret, "",   "jpg"]
  when "Medium 640" then [farm, server, photo_id, secret, "_z", "jpg"]
  when "Medium 800" then [farm, server, photo_id, secret, "_c", "jpg"]
  when "Large 1024" then [farm, server, photo_id, secret, "_b", "jpg"]
  when "Original"   then [farm, server, photo_id, original_secret, "_o", original_format]
  when "Large 1600", "Large 2048"
    raise Shrine::Error, "#{size.inspect} size isn't available"
  else
    raise Shrine::Error, "unknown size: #{size.inspect}"
  end
end
update_id!(photo, id) click to toggle source
# File lib/shrine/storage/flickr.rb, line 101
def update_id!(photo, id)
  info_id = "#{photo.farm}-#{photo.server}-#{photo.id}-#{photo.secret}-#{photo["originalsecret"]}.#{photo["originalformat"]}"
  id.replace(info_id)
end
update_metadata!(photo, metadata) click to toggle source
# File lib/shrine/storage/flickr.rb, line 106
def update_metadata!(photo, metadata)
  metadata["flickr"] = photo.attributes if @store_info
end