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 cl.ly items

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

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

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

# Upload a file
item = @client.upload "/path/to/image.png"

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

# Set an items privacy
@client.privacy "2wr4", true

# Delete an item
@client.delete "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



41
42
43
44
45
# File 'lib/cloudapp/client.rb', line 41

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



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

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

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

Create a new cl.ly item by bookmarking a link.

Requires authentication.

Parameters:

  • url (String)

    url to bookmark

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

    name of bookmark

Returns:



87
88
89
# File 'lib/cloudapp/client.rb', line 87

def bookmark(url, name = "")
  Item.create(:bookmark, {:name => name, :redirect_url => url})
end

#delete(id) ⇒ CloudApp::Item

Send an item to the trash.

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

Requires authentication.

Parameters:

  • id (String)

    cl.ly item id

Returns:



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

def delete(id)
  item = Item.find(id)
  item.class == Item ? item.delete : item
end

#item(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:



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

def item(id)
  Item.find(id)
end

#items(opts = {}) ⇒ Array[CloudApp::Item]

Page through your items.

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 items

Returns:



76
77
78
# File 'lib/cloudapp/client.rb', line 76

def items(opts = {})
  Item.all(opts)
end

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

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

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

Requires authentication.

Parameters:

  • id (String)

    cl.ly item id

  • privacy (Boolean) (defaults to: false)

    privacy setting

Returns:



124
125
126
127
# File 'lib/cloudapp/client.rb', line 124

def privacy(id, privacy = false)
  item = Item.find(id)
  item.class == Item ? item.update(:private => privacy) : item
end

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

Change the name of an item.

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

Requires authentication.

Parameters:

  • id (String)

    cl.ly item id

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

    new item name

Returns:



110
111
112
113
# File 'lib/cloudapp/client.rb', line 110

def rename(id, name = "")
  item = Item.find(id)
  item.class == Item ? item.update(:name => name) : item
end

#upload(file) ⇒ CloudApp::Item

Create a new cl.ly item by uploading a file.

Requires authentication.

Parameters:

  • file (String)

    local path to file

Returns:



97
98
99
# File 'lib/cloudapp/client.rb', line 97

def upload(file)
  Item.create(:upload, :file => file)
end