Class: CloudApp::Item

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

Overview

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

Examples:

Gets started by Authenticating

CloudApp.authenticate "username", "password"

Usage via the Item class

# Find a single item by it's slug
item = CloudApp::Item.find "2wr4"

# Get a list of all items
items = CloudApp::Item.all

# Create a new bookmark
item = CloudApp::Item.create :bookmark, :name => "CloudApp", :redirect_url => "http://getcloudapp.com"

# Upload a file
item = CloudApp::Item.create :upload, :file => "/path/to/image.png"

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

# Set an items privacy
CloudApp::Item.update "http://my.cl.ly/items/1912565", :private => true

# Delete an item
CloudApp::Item.delete "http://my.cl.ly/items/1912565"

Usage via the class instance

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

# Set an items privacy
@item.update :private => true

# Delete an item
@tem.delete

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

authenticate

Constructor Details

#initialize(attributes = {}) ⇒ Item

Create a new CloudApp::Item object.

Only used internally.

Parameters:



127
128
129
# File 'lib/cloudapp/item.rb', line 127

def initialize(attributes = {})
  load(attributes)
end

Instance Attribute Details

#content_urlObject (readonly)

Returns the value of attribute content_url.



118
119
120
# File 'lib/cloudapp/item.rb', line 118

def content_url
  @content_url
end

#created_atObject (readonly)

Returns the value of attribute created_at.



118
119
120
# File 'lib/cloudapp/item.rb', line 118

def created_at
  @created_at
end

#deleted_atObject (readonly)

Returns the value of attribute deleted_at.



118
119
120
# File 'lib/cloudapp/item.rb', line 118

def deleted_at
  @deleted_at
end

#hrefObject (readonly)

Returns the value of attribute href.



118
119
120
# File 'lib/cloudapp/item.rb', line 118

def href
  @href
end

#iconObject (readonly)

Returns the value of attribute icon.



118
119
120
# File 'lib/cloudapp/item.rb', line 118

def icon
  @icon
end

#item_typeObject (readonly)

Returns the value of attribute item_type.



118
119
120
# File 'lib/cloudapp/item.rb', line 118

def item_type
  @item_type
end

#nameObject (readonly)

Returns the value of attribute name.



118
119
120
# File 'lib/cloudapp/item.rb', line 118

def name
  @name
end

#privateObject (readonly)

Returns the value of attribute private.



118
119
120
# File 'lib/cloudapp/item.rb', line 118

def private
  @private
end

#redirect_urlObject (readonly)

Returns the value of attribute redirect_url.



118
119
120
# File 'lib/cloudapp/item.rb', line 118

def redirect_url
  @redirect_url
end

#remote_urlObject (readonly)

Returns the value of attribute remote_url.



118
119
120
# File 'lib/cloudapp/item.rb', line 118

def remote_url
  @remote_url
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



118
119
120
# File 'lib/cloudapp/item.rb', line 118

def updated_at
  @updated_at
end

#urlObject (readonly)

Returns the value of attribute url.



118
119
120
# File 'lib/cloudapp/item.rb', line 118

def url
  @url
end

#view_counterObject (readonly)

Returns the value of attribute view_counter.



118
119
120
# File 'lib/cloudapp/item.rb', line 118

def view_counter
  @view_counter
end

Class Method Details

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

Page through your items.

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 items

Returns:



63
64
65
66
# File 'lib/cloudapp/item.rb', line 63

def self.all(opts = {})
  res = get "/items?#{opts.to_params}", :digest_auth => @@auth
  res.ok? ? res.collect{|i| Item.new(i)} : res
end

.create(kind, opts = {}) ⇒ CloudApp::Item

Create a new cl.ly item.

Requires authentication.

Parameters:

  • kind (Symbol)

    type of cl.ly item (can be :bookmark or :upload)

  • 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)

  • :file (String)

    Path to file (only required for :upload kind)

Returns:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cloudapp/item.rb', line 78

def self.create(kind, opts = {})
  case kind
  when :bookmark
    res = post "/items", {:body => {:item => opts}, :digest_auth => @@auth}
  when :upload
    res = get "/items/new", :digest_auth => @@auth
    return res unless res.ok?
    res = post res['url'], Multipart.new(res['params'].merge!(:file => File.new(opts[:file]))).payload.merge!(:digest_auth => @@auth)
  else
    # TODO raise an error
    return false
  end
  res.ok? ? Item.new(res) : res
end

.delete(href) ⇒ CloudApp::Item

Send an item to the trash.

Requires authentication.

Parameters:

  • href (String)

    href attribute of cl.ly item

Returns:



113
114
115
116
# File 'lib/cloudapp/item.rb', line 113

def self.delete(href)
  res = Base.delete href, :digest_auth => @@auth
  res.ok? ? Item.new(res) : res
end

.find(id) ⇒ CloudApp::Item

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

Finds the item by it’s slug id, for example “2wr4”.

Parameters:

  • id (String)

    cl.ly slug id

Returns:



48
49
50
51
# File 'lib/cloudapp/item.rb', line 48

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

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

Modify a cl.ly item. Can currently modify it’s name or security setting by passing parameters.

Requires authentication.

Parameters:

  • href (String)

    href attribute of cl.ly item

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

    item parameters

Options Hash (opts):

  • :name (String)

    for renaming the item

  • :privacy (Boolean)

    set item privacy

Returns:



102
103
104
105
# File 'lib/cloudapp/item.rb', line 102

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

Instance Method Details

#deleteCloudApp::Item

Send the item to the trash.

Requires authentication.

Returns:



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

def delete
  self.class.delete self.href
end

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

Modify the item. Can currently modify it’s name or security setting by passing parameters.

Requires authentication.

Parameters:

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

    item parameters

Options Hash (opts):

  • :name (String)

    for renaming the item

  • :privacy (Boolean)

    set item privacy

Returns:



139
140
141
# File 'lib/cloudapp/item.rb', line 139

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