class Flickrie::UploadClient

Constants

CONTENT_TYPES

Public Instance Methods

replace(media, media_id, params = {}) click to toggle source
# File lib/flickrie/upload_client.rb, line 11
def replace(media, media_id, params = {})
  file = get_file(media, params[:content_type])
  title = file.original_filename.match(/\.\w{3,4}$/).pre_match
  post "replace", {photo: file, photo_id: media_id, title: title}.merge(params)
end
upload(media, params = {}) click to toggle source
# File lib/flickrie/upload_client.rb, line 5
def upload(media, params = {})
  file = get_file(media, params[:content_type])
  title = file.original_filename.match(/\.\w{3,4}$/).pre_match
  post "upload", {photo: file, title: title}.merge(params)
end

Private Instance Methods

determine_content_type(file_path) click to toggle source
# File lib/flickrie/upload_client.rb, line 57
def determine_content_type(file_path)
  extension = file_path[/\.\w+$/]
  content_type = CONTENT_TYPES.find { |k,v| k.include?(extension) }.last

rescue NoMethodError
  raise Error, "Content type for this extension (#{extension}) is not known"
end
get_file(object, content_type = nil) click to toggle source
# File lib/flickrie/upload_client.rb, line 38
def get_file(object, content_type = nil)
  file, content_type, file_path =
    case object.class.name
    when "String"
      # file path
      [File.open(object), content_type || determine_content_type(object), object]
    when "ActionDispatch::Http::UploadedFile"
      # file from Rails
      [object, object.content_type, object.tempfile]
    when "Hash"
      # file from Sinatra
      [object[:tempfile], object[:type], object[:tempfile].path]
    else
      raise Error, "Invalid file format"
    end

  Faraday::UploadIO.new(file, content_type, file_path)
end