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_cache

Instance Method Summary collapse

Methods inherited from Base

#auth, #people, #photos, #send_request, #sign_request, #uploader

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.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/flickr/uploader.rb', line 77

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

NOT WORKING … FILE UPLOADS IN NET::HTTP SUX

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
    


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/flickr/uploader.rb', line 43

def upload(filename, options = {})
  photo = File.new(filename, 'r').read
  mimetype = MIME::Types.of(filename)

  upload_options = {}
  @flickr.sign_request(upload_options)
  
  form = Flickr::Uploader::MultiPartForm.new
      
  upload_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').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