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.

Parameters:

  • opts (Hash) (defaults to: {})

    options parameters

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

Returns:



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

    Parameters:

    • opts (Hash) (defaults to: {})

      options paramaters

    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(: bookmarks, bookmarks) ⇒ CloudApp::Drop

    Parameters:

    • bookmarks (Array)

      array of bookmark option parameters (containing :name and :redirect_url)

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

    Parameters:

    • opts (Hash) (defaults to: {})

      options paramaters

    Options Hash (opts):

    • :file (String)

      Path to file (only required for :upload kind)

    • :private (Boolean)

      override the account default privacy setting

Parameters:

  • kind (Symbol)

    type of drop (can be :bookmark, :bookmarks or :upload)

Returns:



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.

Parameters:

  • href (String)

    href attribute of the drop

Returns:



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”.

Parameters:

  • id (String)

    cl.ly slug id

Returns:



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.

Parameters:

  • href (String)

    href attribute of the drop

Returns:



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.

Parameters:

  • href (String)

    href attribute of drop

  • opts (Hash) (defaults to: {})

    options paramaters

Options Hash (opts):

  • :name (String)

    for renaming the drop

  • :privacy (Boolean)

    set drop privacy

Returns:



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?

Returns:

  • (Boolean)


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

def archive?
  item_type == 'archive'
end

#audio?Boolean

Is the drop an audio file?

Returns:

  • (Boolean)


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

def audio?
  item_type == 'audio'
end

#bookmark?Boolean

Is the drop a bookmark?

Returns:

  • (Boolean)


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

def bookmark?
  item_type == 'bookmark'
end

#deleteCloudApp::Drop

Send the drop to the trash.

Returns:



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?

Returns:

  • (Boolean)


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

Returns:

  • (String)


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.

Returns:



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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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.

Parameters:

  • opts (Hash) (defaults to: {})

    options paramaters

Options Hash (opts):

  • :name (String)

    for renaming the drop

  • :privacy (Boolean)

    set the drop’s privacy

Returns:



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?

Returns:

  • (Boolean)


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

def video?
  item_type == 'video'
end