Class: CloudApp::Drop

Inherits:
Base
  • Object
show all
Defined in:
lib/cloudapp/drop.rb

Overview

An ActiveResource-like interface through which to interract with CloudApp drops.

Examples:

Gets started by Authenticating

CloudApp.authenticate "username", "password"

Usage via the Drop class

# Find a single drop by it's slug
@drop = CloudApp::Drop.find "2wr4"

# Get a list of all drops
@drops = CloudApp::Drop.all

# Create a new bookmark
@drop = CloudApp::Drop.create :bookmark, :name => "CloudApp", :redirect_url => "http://getcloudapp.com"

# Create multiple bookmarks
bookmarks = [
  { :name => "Authur Dent", :redirect_url => "http://en.wikipedia.org/wiki/Arthur_Dent" },
  { :name => "Zaphod Beeblebrox", :redirect_url => "http://en.wikipedia.org/wiki/Zaphod_Beeblebrox" }
]
@drops = CloudApp::Drop.create :bookmarks, bookmarks

# Upload a file
@drop = CloudApp::Drop.create :upload, :file => "/path/to/image.png"
@drop = CloudApp::Drop.create :upload, :file => "/path/to/image.png", :private => true

# Rename a file
CloudApp::Drop.update "http://my.cl.ly/items/1912565", :name => "Big Screenshot"

# Set a drop's privacy
CloudApp::Drop.update "http://my.cl.ly/items/1912565", :private => true

# Delete a drop
CloudApp::Drop.delete "http://my.cl.ly/items/1912565"

# Recover a deleted drop
CloudApp::Drop.recover "http://my.cl.ly/items/1912565"

Usage via the class instance

# Rename a file
@drop.update :name => "Big Screenshot"

# Set the drop's privacy
@drop.update :private => true

# Delete a drop
@drop.delete

# Recover a deleted drop
@drop.recover

Direct Known Subclasses

Item

Instance Attribute Summary collapse

Attributes inherited from Base

#data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

authenticate, bad_response, #initialize

Constructor Details

This class inherits a constructor from CloudApp::Base

Instance Attribute Details

#content_urlObject (readonly)

Returns the value of attribute content_url


153
154
155
# File 'lib/cloudapp/drop.rb', line 153

def content_url
  @content_url
end

#created_atObject (readonly)

Returns the value of attribute created_at


153
154
155
# File 'lib/cloudapp/drop.rb', line 153

def created_at
  @created_at
end

#deleted_atObject (readonly)

Returns the value of attribute deleted_at


153
154
155
# File 'lib/cloudapp/drop.rb', line 153

def deleted_at
  @deleted_at
end

#hrefObject (readonly)

Returns the value of attribute href


153
154
155
# File 'lib/cloudapp/drop.rb', line 153

def href
  @href
end

#iconObject (readonly)

Returns the value of attribute icon


153
154
155
# File 'lib/cloudapp/drop.rb', line 153

def icon
  @icon
end

#item_typeObject (readonly)

Returns the value of attribute item_type


153
154
155
# File 'lib/cloudapp/drop.rb', line 153

def item_type
  @item_type
end

#nameObject (readonly)

Returns the value of attribute name


153
154
155
# File 'lib/cloudapp/drop.rb', line 153

def name
  @name
end

#privateObject (readonly)

Returns the value of attribute private


153
154
155
# File 'lib/cloudapp/drop.rb', line 153

def private
  @private
end

#redirect_urlObject (readonly)

Returns the value of attribute redirect_url


153
154
155
# File 'lib/cloudapp/drop.rb', line 153

def redirect_url
  @redirect_url
end

#remote_urlObject (readonly)

Returns the value of attribute remote_url


153
154
155
# File 'lib/cloudapp/drop.rb', line 153

def remote_url
  @remote_url
end

#sourceObject (readonly)

Returns the value of attribute source


153
154
155
# File 'lib/cloudapp/drop.rb', line 153

def source
  @source
end

#updated_atObject (readonly)

Returns the value of attribute updated_at


153
154
155
# File 'lib/cloudapp/drop.rb', line 153

def updated_at
  @updated_at
end

#urlObject (readonly)

Returns the value of attribute url


153
154
155
# File 'lib/cloudapp/drop.rb', line 153

def url
  @url
end

#view_counterObject (readonly)

Returns the value of attribute view_counter


153
154
155
# File 'lib/cloudapp/drop.rb', line 153

def view_counter
  @view_counter
end

Class Method Details

.all(opts = {}) ⇒ Array[CloudApp::Drop]

Page through your drops.

Requires authentication.

Options Hash (opts):

  • :page (Integer)

    Page number starting at 1

  • :per_page (Integer)

    Number of items per page

  • :type (String)

    Filter items by type (image, bookmark, text, archive, audio, video, or unknown)

  • :deleted (Boolean)

    Show trashed drops


77
78
79
80
# File 'lib/cloudapp/drop.rb', line 77

def self.all(opts = {})
  res = get "/items", {:query => (opts.empty? ? nil : opts), :digest_auth => @@auth}
  res.ok? ? res.collect{|i| Drop.new(i)} : bad_response(res)
end

.self.create(:bookmark, opts = {}) ⇒ CloudApp::Drop .self.create(:bookmarks, bookmarks) ⇒ CloudApp::Drop .self.create(:upload, opts = {}) ⇒ CloudApp::Drop

Create a new drop. Multiple bookmarks can be created at once by passing an array of bookmark options parameters.

Requires authentication.

Overloads:

  • .self.create(:bookmark, opts = {}) ⇒ CloudApp::Drop

    Options Hash (opts):

    • :name (String)

      Name of bookmark (only required for :bookmark kind)

    • :redirect_url (String)

      Redirect URL (only required for :bookmark kind)

  • .self.create(:upload, opts = {}) ⇒ CloudApp::Drop

    Options Hash (opts):

    • :file (String)

      Path to file (only required for :upload kind)

    • :private (Boolean)

      override the account default privacy setting


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cloudapp/drop.rb', line 99

def self.create(kind, opts = {})
  case kind
  when :bookmark
    res = post "/items", {:body => {:item => opts}, :digest_auth => @@auth}
  when :bookmarks
    res = post "/items", {:body => {:items => opts}, :digest_auth => @@auth}
  when :upload
    r = get "/items/new", {:query => ({:item => {:private => opts[:private]}} if opts.has_key?(:private)), :digest_auth => @@auth}
    return bad_response(r) unless r.ok?
    res = post r['url'], Multipart.new(r['params'].merge!(:file => File.new(opts[:file]))).payload.merge!(:digest_auth => @@auth)
  else
    # TODO raise an error
    return false
  end
  res.ok? ? (res.is_a?(Array) ? res.collect{|i| Drop.new(i)} : Drop.new(res)) : bad_response(res)
end

.delete(href) ⇒ CloudApp::Drop

Send a drop to the trash.

Requires authentication.


136
137
138
139
140
# File 'lib/cloudapp/drop.rb', line 136

def self.delete(href)
  # Use delete on the Base class to avoid recursion
  res = Base.delete href, :digest_auth => @@auth
  res.ok? ? Drop.new(res) : bad_response(res)
end

.find(id) ⇒ CloudApp::Drop

Get metadata about a cl.ly URL like name, type, or view count.

Finds the drop by it's slug id, for example “2wr4”.


62
63
64
65
# File 'lib/cloudapp/drop.rb', line 62

def self.find(id)
  res = get "http://cl.ly/#{id}"
  res.ok? ? Drop.new(res) : bad_response(res)
end

.recover(href) ⇒ CloudApp::Drop

Recover a drop from the trash.

Requires authentication.


148
149
150
151
# File 'lib/cloudapp/drop.rb', line 148

def self.recover(href)
  res = put href, {:body => {:deleted => true, :item => {:deleted_at => nil}}, :digest_auth => @@auth}
  res.ok? ? Drop.new(res) : bad_response(res)
end

.update(href, opts = {}) ⇒ CloudApp::Drop

Modify a drop. Can currently modify it's name or security setting by passing parameters.

Requires authentication.

Options Hash (opts):

  • :name (String)

    for renaming the drop

  • :privacy (Boolean)

    set drop privacy


125
126
127
128
# File 'lib/cloudapp/drop.rb', line 125

def self.update(href, opts = {})
  res = put href, {:body => {:item => opts}, :digest_auth => @@auth}
  res.ok? ? Drop.new(res) : bad_response(res)
end

Instance Method Details

#archive?Boolean

Is the drop an archive?


204
205
206
# File 'lib/cloudapp/drop.rb', line 204

def archive?
  item_type == 'archive'
end

#audio?Boolean

Is the drop an audio file?


211
212
213
# File 'lib/cloudapp/drop.rb', line 211

def audio?
  item_type == 'audio'
end

#bookmark?Boolean

Is the drop a bookmark?


190
191
192
# File 'lib/cloudapp/drop.rb', line 190

def bookmark?
  item_type == 'bookmark'
end

#deleteCloudApp::Drop

Send the drop to the trash.


169
170
171
# File 'lib/cloudapp/drop.rb', line 169

def delete
  self.class.delete self.href
end

#image?Boolean

Is the drop an image?


183
184
185
# File 'lib/cloudapp/drop.rb', line 183

def image?
  item_type == 'image'
end

#rawString

Return the drops raw data as a string, useful for returning the text of documents


232
233
234
# File 'lib/cloudapp/drop.rb', line 232

def raw
  @raw ||= HTTParty.get(content_url).to_s
end

#recoverCloudApp::Drop

Recover the drop from the trash.


176
177
178
# File 'lib/cloudapp/drop.rb', line 176

def recover
  self.class.recover self.href
end

#text?Boolean

Is the drop a text file?


197
198
199
# File 'lib/cloudapp/drop.rb', line 197

def text?
  item_type == 'text'
end

#unknown?Boolean

Is the drop an unknown file type?


225
226
227
# File 'lib/cloudapp/drop.rb', line 225

def unknown?
  item_type == 'unknown'
end

#update(opts = {}) ⇒ CloudApp::Drop

Modify the drop. Can currently modify it's name or security setting by passing parameters.

Options Hash (opts):

  • :name (String)

    for renaming the drop

  • :privacy (Boolean)

    set the drop's privacy


162
163
164
# File 'lib/cloudapp/drop.rb', line 162

def update(opts = {})
  self.class.update self.href, opts
end

#video?Boolean

Is the drop a video file?


218
219
220
# File 'lib/cloudapp/drop.rb', line 218

def video?
  item_type == 'video'
end