Class: CloudApp::Client

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

Overview

A client interface through which to interract with the CloudApp API.

Examples:

Creating a client instance and set authentication credentials

@client = CloudApp::Client.new
@client.authenticate "username", "password"

Creating editing and deleting drops

# Find a single drop by it's slug
drop = @client.drop "2wr4"

# Get a list of all drops
drops = @client.all

# Create a new bookmark
drop = @client.bookmark "http://getcloudapp.com", "CloudApp"

# Create multiple new 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 = @client.bookmark bookmarks

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

# Rename a file
@client.rename "2wr4", "Big Screenshot"

# Set a drop's privacy
@client.privacy "2wr4", true

# Delete an drop
@client.delete "2wr4"

# Recover a deleted drop
@client.recover "2wr4"

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ CloudApp::Client

Creates a new CloudApp::Client instance.

You can pass :username and :password parameters to the call.

Parameters:

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

    authentication credentials.

Options Hash (opts):

  • :username (String)

    cl.ly username

  • :password (String)

    cl.ly username



52
53
54
55
56
# File 'lib/cloudapp/client.rb', line 52

def initialize(opts = {})
  if opts[:username] && opts[:password]
    Base.authenticate(opts[:username], opts[:password])
  end
end

Instance Method Details

#authenticate(username, password) ⇒ Hash

Sets the authentication credentials in a class variable.

Parameters:

  • username (String)

    cl.ly username

  • password (String)

    cl.ly password

Returns:

  • (Hash)

    authentication credentials



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

def authenticate(username, password)
  Base.authenticate(username, password)
end

#bookmark(url, name = "") ⇒ CloudApp::Drop #bookmark(opts) ⇒ CloudApp::Drop

Create one or more new bookmark drops.

Requires authentication.

Overloads:

  • #bookmark(url, name = "") ⇒ CloudApp::Drop

    Parameters:

    • url (String)

      url to bookmark

    • name (String) (defaults to: "")

      name of bookmark

  • #bookmark(opts) ⇒ CloudApp::Drop

    Parameters:

    • opts (Array)

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

Returns:



105
106
107
108
109
110
111
112
# File 'lib/cloudapp/client.rb', line 105

def bookmark(*args)
  if args[0].is_a? Array
    Drop.create(:bookmarks, args)
  else
    url, name = args[0], (args[1] || "")
    Drop.create(:bookmark, {:name => name, :redirect_url => url})
  end
end

#delete(id) ⇒ CloudApp::Drop

Send the drop to the trash.

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

Requires authentication.

Parameters:

  • id (String)

    drop id

Returns:



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

def delete(id)
  drop = Drop.find(id)
  drop.delete
end

#drop(id) ⇒ CloudApp::Drop Also known as: item

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:



73
74
75
# File 'lib/cloudapp/client.rb', line 73

def drop(id)
  Drop.find(id)
end

#drops(opts = {}) ⇒ Array[CloudApp::Drop] Also known as: items

Page through your drops.

Requires authentication.

Parameters:

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

    options parameters

Options Hash (opts):

  • :page (Integer) — default: 1

    Page number starting at 1

  • :per_page (Integer) — default: 5

    Number of items per page

  • :type (String) — default: 'image'

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

  • :deleted (Boolean) — default: true

    Show trashed drops

Returns:



89
90
91
# File 'lib/cloudapp/client.rb', line 89

def drops(opts = {})
  Drop.all(opts)
end

#privacy(id, privacy = false) ⇒ CloudApp::Drop

Modify a drop with a private URL to have a public URL or vice versa.

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

Requires authentication.

Parameters:

  • id (String)

    drop id

  • privacy (Boolean) (defaults to: false)

    privacy setting

Returns:



149
150
151
152
# File 'lib/cloudapp/client.rb', line 149

def privacy(id, privacy = false)
  drop = Drop.find(id)
  drop.update(:private => privacy)
end

#recover(id) ⇒ CloudApp::Drop

Recover a deleted drop from the trash.

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

Requires authentication.

Parameters:

  • id (String)

    drop id

Returns:



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

def recover(id)
  drop = Drop.find(id)
  drop.recover
end

#rename(id, name = "") ⇒ CloudApp::Drop

Change the name of the drop.

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

Requires authentication.

Parameters:

  • id (String)

    drop id

  • name (String) (defaults to: "")

    new drop name

Returns:



135
136
137
138
# File 'lib/cloudapp/client.rb', line 135

def rename(id, name = "")
  drop = Drop.find(id)
  drop.update(:name => name)
end

#upload(file, opts = {}) ⇒ CloudApp::Drop

Create a new drop by uploading a file.

Requires authentication.

Parameters:

  • file (String)

    local path to file

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

    options paramaters

Options Hash (opts):

  • :private (Boolean)

    override the account default privacy setting

Returns:



122
123
124
# File 'lib/cloudapp/client.rb', line 122

def upload(file, opts = {})
  Drop.create(:upload, opts.merge(:file => file))
end