Class: Flickr::Uploader

Inherits:
Base show all
Defined in:
lib/flickr/uploader.rb

Defined Under Namespace

Classes: FormPart, MultiPartForm, Status

Constant Summary

Constants inherited from Base

Base::AUTH_ENDPOINT, Base::REST_ENDPOINT, Base::UPLOAD_ENDPOINT

Instance Attribute Summary

Attributes inherited from Base

#api_key, #api_secret, #token, #token_cache

Instance Method Summary collapse

Methods inherited from Base

#auth, #contacts, #people, #photos, #photosets, #send_request, #sign_request, #test, #uploader, #urls

Constructor Details

#initialize(flickr) ⇒ Uploader

Returns a new instance of Uploader.



2
3
4
# File 'lib/flickr/uploader.rb', line 2

def initialize(flickr)
  @flickr = flickr
end

Instance Method Details

#statusObject

Returns information for the calling user related to photo uploads.

  • Bandwidth and filesize numbers are provided in bytes.

  • Bandwidth is specified in bytes per month.

  • Pro accounts display 99 for the number of remaining sets, since they have unlimited sets. Free accounts will display either 3, 2, 1, or 0.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/flickr/uploader.rb', line 87

def status
  rsp = @flickr.send_request('flickr.people.getUploadStatus')

  Flickr::Uploader::Status.new(@flickr, :nsid => rsp.user[:id],
                                        :is_pro => (rsp.user[:ispro] == "1" ? true : false),
                                        :username => rsp.user.username.to_s,
                                        :max_bandwidth => rsp.user.bandwidth[:maxbytes],
                                        :used_bandwidth => rsp.user.bandwidth[:usedbytes],
                                        :remaining_bandwidth => rsp.user.bandwidth[:remainingbytes],
                                        :max_filesize => rsp.user.filesize[:maxbytes],
                                        :max_videosize => rsp.user.videosize[:maxbytes],
                                        :sets_created => rsp.user.sets[:created].to_i,
                                        :sets_remaining => (rsp.user[:ispro] == "1" ? 99 : rsp.user.sets[:remaining].to_i))
end

#upload(filename, options = {}) ⇒ Object

upload a photo to flickr

Params

  • filename (Required)

    path to the file to upload
    
  • options (Optional)

    options to attach to the photo (See Below)
    

Options

  • title (Optional)

    The title of the photo.
    
  • description (Optional)

    A description of the photo. May contain some limited HTML.
    
  • tags (Optional)

    A space-seperated list of tags to apply to the photo.
    
  • privacy (Optional)

    Specifies who can view the photo. valid valus are:
      :public
      :private
      :friends
      :family
      :friends_and_family
    
  • safety_level (Optional)

    sets the safety level of the photo. valid values are:
      :safe
      :moderate
      :restricted
    
  • content_type (Optional)

    tells what type of image you are uploading. valid values are:
      :photo
      :screenshot
      :other
    
  • hidden (Optional)

    boolean that determines if the photo shows up in global searches
    


41
42
43
# File 'lib/flickr/uploader.rb', line 41

def upload(filename, options = {})
  upload_data(File.new(filename, 'rb').read, MIME::Types.of(filename), options.merge(:filename => filename))
end

#upload_data(photo, mimetype, options = {}) ⇒ Object

upload a photo to flickr

Params

  • photo (Required)

    image stored in a variable
    
  • mimetype (Required)

    mime type of the image
    
  • options (Optional)

    see upload method
    


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/flickr/uploader.rb', line 55

def upload_data(photo, mimetype, options = {})
  filename = options.delete(:filename) || Time.now.to_s
  options = upload_options(options)
  @flickr.sign_request(options)

  form = Flickr::Uploader::MultiPartForm.new

  options.each do |k,v|
    form.parts << Flickr::Uploader::FormPart.new(k.to_s, v.to_s)
  end

  form.parts << Flickr::Uploader::FormPart.new('photo', photo, mimetype, filename)

  headers = {"Content-Type" => "multipart/form-data; boundary=" + form.boundary}

  rsp = Net::HTTP.start('api.flickr.com', :use_ssl => true).post("/services/upload/", form.to_s, headers).body

  xm = XmlMagic.new(rsp)

  if xm[:stat] == 'ok'
    xm
  else
    raise "#{xm.err[:code]}: #{xm.err[:msg]}"
  end
end