cloudapp_api 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,132 @@
1
+ module CloudApp
2
+
3
+ # An ActiveResource-like interface through which to interract with the CloudApp API.
4
+ #
5
+ # @example Create a CloudApp account
6
+ # CloudApp::Account.create :email => "arthur@dent.com", :password => "towel"
7
+ #
8
+ # @example Most other account actions require authentication first
9
+ # CloudApp.authenticate "username", "password"
10
+ #
11
+ # @example Usage via the Account class
12
+ # # View account details
13
+ # @account = CloudApp::Account.find
14
+ #
15
+ # # Change default security
16
+ # CloudApp::Account.update :private_items => false
17
+ #
18
+ # # Change email
19
+ # CloudApp::Account.update :email => "ford@prefect.com", :current_password => "towel"
20
+ #
21
+ # # Change password
22
+ # CloudApp::Account.update :password => "happy frood", :current_password => "towel"
23
+ #
24
+ # # Set custom domain
25
+ # CloudApp::Account.update :domain => "dent.com", :domain_home_page => "http://hhgproject.org"
26
+ #
27
+ # # Forgot password
28
+ # CloudApp::Account.reset :email => "arthur@dent.com"
29
+ #
30
+ # @example Usage via the class instance
31
+ # # Change default security
32
+ # @account.update :private_items => false
33
+ #
34
+ # # Change email
35
+ # @account.update :email => "ford@prefect.com", :current_password => "towel"
36
+ #
37
+ # # Change password
38
+ # @account.update :password => "happy frood", :current_password => "towel"
39
+ #
40
+ # # Set custom domain
41
+ # @account.update :domain => "dent.com", :domain_home_page => "http://hhgproject.org"
42
+ #
43
+ # # Forgot password
44
+ # @account.reset
45
+ #
46
+ class Account < Base
47
+
48
+ # Get the basic details of the authenticated account.
49
+ # Requires authentication.
50
+ # @return [CloudApp::Account]
51
+ def self.find
52
+ res = get "/account", :digest_auth => @@auth
53
+ res.ok? ? Account.new(res) : res
54
+ end
55
+
56
+ # Create a CloudApp account.
57
+ # @example Provide a user details param
58
+ # { :email => "arthur@dent.com", :password => "towel" }
59
+ # @param [Hash] of user credentials
60
+ # @return [CloudApp::Account]
61
+ def self.create(opts = {})
62
+ res = post "/register", :body => {:user => opts}
63
+ res.ok? ? Account.new(res) : res
64
+ end
65
+
66
+ # Modify the authenticated accounts details. Can change the default security of newly
67
+ # created items, the accounts email address, password, and custom domain details.
68
+ # @example Options for changing default security of new items
69
+ # { :private_items => false }
70
+ # @example Options for changing email address
71
+ # { :email => "ford@prefect.com", :current_password => "towel" }
72
+ # @example Options for modifying password
73
+ # { :password => "happy frood", :current_password => "towel" }
74
+ # @example Options for changing custom domain
75
+ # { :domain => "dent.com", :domain_home_page => "http://hhgproject.org" }
76
+ # Note that to custom domains requires and account with a Pro subscription.
77
+ # Requires authentication.
78
+ # @param [Hash] account parameters
79
+ # @return [CloudApp::Account]
80
+ def self.update(opts = {})
81
+ res = put "/account", {:body => {:user => opts}, :digest_auth => @@auth}
82
+ res.ok? ? Account.new(res) : res
83
+ end
84
+
85
+ # Dispatch an email containing a link to reset the account's password.
86
+ # @example Requires the account email address in a hash
87
+ # { :email => "arthur@dent.com" }
88
+ # @param [Hash] account credentials
89
+ # @return [Boolean]
90
+ def self.reset(opts = {})
91
+ res = post "/reset", :body => {:user => opts}
92
+ res.ok? ? true : res
93
+ end
94
+
95
+ attr_reader :id, :email, :domain, :domain_home_page, :private_items,
96
+ :subscribed, :alpha, :created_at, :updated_at, :activated_at
97
+
98
+ # Create a new CloudApp::Account object.
99
+ # Only used internally
100
+ # @param [Hash] attributes
101
+ # @param [CloudApp::Account]
102
+ def initialize(attributes = {})
103
+ load(attributes)
104
+ end
105
+
106
+ # Modify the authenticated accounts details. Can change the default security of newly
107
+ # created items, the accounts email address, password, and custom domain details.
108
+ # @example Options for changing default security of new items
109
+ # { :private_items => false }
110
+ # @example Options for changing email address
111
+ # { :email => "ford@prefect.com", :current_password => "towel" }
112
+ # @example Options for modifying password
113
+ # { :password => "happy frood", :current_password => "towel" }
114
+ # @example Options for changing custom domain
115
+ # { :domain => "dent.com", :domain_home_page => "http://hhgproject.org" }
116
+ # Note that to custom domains requires and account with a Pro subscription.
117
+ # Requires authentication.
118
+ # @param [Hash] account parameters
119
+ # @return [CloudApp::Account]
120
+ def update(opts = {})
121
+ self.class.update opts
122
+ end
123
+
124
+ # Dispatch an email containing a link to reset the account's password.
125
+ # @return [Boolean]
126
+ def reset
127
+ self.class.reset :email => self.email
128
+ end
129
+
130
+ end
131
+
132
+ end
data/lib/cloudapp/base.rb CHANGED
@@ -2,12 +2,14 @@ require "httparty"
2
2
 
3
3
  module CloudApp
4
4
 
5
+ # Globally set request headers
5
6
  HEADERS = {
6
- "User-Agent" => "Ruby.CloudApp.API",
7
- "Accept" => "application/json",
8
- "Content-Type" => "application/json"
7
+ "User-Agent" => "Ruby.CloudApp.API",
8
+ "Accept" => "application/json",
9
+ "Content-Type" => "application/json"
9
10
  }
10
-
11
+
12
+ # Base class for setting HTTParty configurations globally
11
13
  class Base
12
14
 
13
15
  include HTTParty
@@ -15,55 +17,24 @@ module CloudApp
15
17
  headers HEADERS
16
18
  format :json
17
19
 
20
+ # Sets the authentication credentials in a class variable.
21
+ # @param [String] cl.ly username
22
+ # @param [String] cl.ly password
23
+ # @return [Hash] authentication credentials
18
24
  def self.authenticate(username, password)
19
25
  @@auth = {:username => username, :password => password}
20
26
  end
21
27
 
22
- def self.find(id)
23
- res = get "http://cl.ly/#{id}"
24
- res.ok? ? Item.new(res) : res
25
- end
26
-
27
- def self.all(opts = nil)
28
- opts = opts.nil? ? "" : "?#{opts.to_params}"
29
- res = get "/items#{opts}", :digest_auth => @@auth
30
- res.ok? ? res.collect{|i| Item.new(i)} : res
31
- end
32
-
33
- def self.create(kind, opts = {})
34
- case kind
35
- when :bookmark
36
- res = post "/items", {:query => {:item => opts}, :digest_auth => @@auth}
37
- when :upload
38
- res = get "/items/new", :digest_auth => @@auth
39
- return res unless res.ok?
40
- res = post res['url'], Multipart.new(res['params'].merge!(:file => File.new(opts[:file]))).payload.merge!(:digest_auth => @@auth)
41
- else
42
- return false
43
- end
44
- res.ok? ? Item.new(res) : res
45
- end
46
-
47
- def initialize(attributes = {})
48
- load(attributes)
49
- end
50
-
51
- def delete
52
- res = self.class.delete self.href, :digest_auth => @@auth
53
- res.ok? ? true : res
54
- end
55
-
56
28
  private
57
29
 
30
+ # Sets the attributes for object.
31
+ # @param [Hash] attributes
58
32
  def load(attributes = {})
59
33
  attributes.each do |key, val|
60
34
  self.instance_variable_set("@#{key}", val)
61
- self.class.send(:define_method, key, proc{self.instance_variable_get("@#{key}")})
62
35
  end
63
36
  end
64
-
37
+
65
38
  end
66
39
 
67
- class Item < Base ; end
68
-
69
40
  end
@@ -1,33 +1,119 @@
1
1
  module CloudApp
2
2
 
3
+ # A client interface through which to interract with the CloudApp API.
4
+ #
5
+ # @example Creating a client instance and set authentication credentials
6
+ # @client = CloudApp::Client.new
7
+ # @client.authenticate "username", "password"
8
+ #
9
+ # @example Creating editing and deleting cl.ly items
10
+ # # Find a single item by it's slug
11
+ # item = @client.item "2wr4"
12
+ #
13
+ # # Get a list of all items
14
+ # items = @client.all
15
+ #
16
+ # # Create a new bookmark
17
+ # item = @client.bookmark "http://getcloudapp.com", "CloudApp"
18
+ #
19
+ # # Upload a file
20
+ # item = @client.upload "/path/to/image.png"
21
+ #
22
+ # # Rename a file
23
+ # @client.rename "2wr4", "Big Screenshot"
24
+ #
25
+ # # Set an items privacy
26
+ # @client.privacy "2wr4", true
27
+ #
28
+ # # Delete an item
29
+ # @client.delete "2wr4"
30
+ #
3
31
  class Client
4
32
 
33
+ # Creates a new CloudApp::Client instance.
34
+ # @example Optionally provide an authentication hash
35
+ # { :username => "testuser@test.com", :password => "password" }
36
+ # @param [Hash] authentication credentials.
37
+ # @return [CloudApp::Client]
5
38
  def initialize(opts = {})
6
39
  if opts[:username] && opts[:password]
7
40
  Base.authenticate(opts[:username], opts[:password])
8
41
  end
9
42
  end
10
43
 
44
+ # Sets the authentication credentials in a class variable.
45
+ # @param [String] cl.ly username
46
+ # @param [String] cl.ly password
47
+ # @return [Hash] authentication credentials
11
48
  def authenticate(username, password)
12
49
  Base.authenticate(username, password)
13
50
  end
14
51
 
52
+ # Get metadata about a cl.ly URL like name, type, or view count.
53
+ # Finds the item by it's slug id, for example "2wr4".
54
+ # @param [String] cl.ly slug id
55
+ # @return [CloudApp::Item]
15
56
  def item(id)
16
57
  Item.find(id)
17
58
  end
18
59
 
60
+ # Page through your items.
61
+ # @example Optionally pass a hash of parameters.
62
+ # :page => 1 # Page number starting at 1
63
+ # :per_page => 5 # Number of items per page
64
+ # :type => image # Filter items by type (image, bookmark, text, archive, audio, video, or unknown)
65
+ # :deleted => true # Show trashed items
66
+ # Requires authentication.
67
+ # @param [Hash] options parameters
68
+ # @return [Array] of CloudApp::Item items
19
69
  def items(opts = {})
20
70
  Item.all(opts)
21
71
  end
22
-
72
+
73
+ # Create a new cl.ly item by bookmarking a link.
74
+ # Requires authentication.
75
+ # @param [String] url to bookmark
76
+ # @param [String] name of bookmark
77
+ # @return [CloudApp::Item]
23
78
  def bookmark(url, name = "")
24
79
  Item.create(:bookmark, {:name => name, :redirect_url => url})
25
80
  end
26
81
 
82
+ # Create a new cl.ly item by uploading a file.
83
+ # Requires authentication.
84
+ # @param [String] local path to file
85
+ # @return [CloudApp::Item]
27
86
  def upload(file)
28
87
  Item.create(:upload, :file => file)
29
88
  end
30
-
89
+
90
+ # Change the name of an item.
91
+ # Finds the item by it's slug id, for example "2wr4".
92
+ # Requires authentication.
93
+ # @param [String] cl.ly item id
94
+ # @param [String] new item name
95
+ # @return [CloudApp::Item]
96
+ def rename(id, name = "")
97
+ item = Item.find(id)
98
+ item.class == Item ? item.update(:name => name) : item
99
+ end
100
+
101
+ # Modify an item with a private URL to have a public URL or vice versa.
102
+ # Finds the item by it's slug id, for example "2wr4".
103
+ # Requires authentication.
104
+ # @param [String] cl.ly item id
105
+ # @param [Boolean] privacy setting
106
+ # @return [CloudApp::Item]
107
+ def privacy(id, privacy = false)
108
+ item = Item.find(id)
109
+ item.class == Item ? item.update(:private => privacy) : item
110
+ end
111
+
112
+ # Send an item to the trash.
113
+ # Finds the item by it's slug id, for example "2wr4".
114
+ # Requires authentication.
115
+ # @param [String] cl.ly item id
116
+ # @return [CloudApp::Item]
31
117
  def delete(id)
32
118
  item = Item.find(id)
33
119
  item.class == Item ? item.delete : item
@@ -1,18 +1,21 @@
1
- if HTTParty::VERSION <= "0.5.2"
2
- ["httparty", "net_digest_auth"].each do |inc|
3
- require File.join(File.dirname(__FILE__), "monkey_patch", inc)
4
- end
5
- end
1
+ require "json"
6
2
 
7
- module HTTParty
8
- class Response < HTTParty::BasicObject
3
+ module HTTParty #:nodoc:
4
+
5
+ class Response < HTTParty::BasicObject #:nodoc:
9
6
  def ok?
10
- self.code == 200
7
+ [200, 201, 202].include?(self.code)
11
8
  end
12
9
  end
13
10
 
14
- class Request
11
+ class Request #:nodoc:
12
+
15
13
  private
14
+
15
+ def body
16
+ options[:body].is_a?(Hash) ? options[:body].to_json : options[:body]
17
+ end
18
+
16
19
  def setup_raw_request
17
20
  # This is a cloudapp hack to ensure the correct headers are set on redirect from S3
18
21
  if @redirect
@@ -26,5 +29,7 @@ module HTTParty
26
29
  @raw_request.basic_auth(username, password) if options[:basic_auth]
27
30
  setup_digest_auth if options[:digest_auth]
28
31
  end
32
+
29
33
  end
34
+
30
35
  end
@@ -0,0 +1,147 @@
1
+ module CloudApp
2
+
3
+ # An ActiveResource-like interface through which to interract with the CloudApp API.
4
+ #
5
+ # @example Gets started by Authenticating
6
+ # CloudApp.authenticate "username", "password"
7
+ #
8
+ # @example Usage via the Item class
9
+ # # Find a single item by it's slug
10
+ # item = CloudApp::Item.find "2wr4"
11
+ #
12
+ # # Get a list of all items
13
+ # items = CloudApp::Item.all
14
+ #
15
+ # # Create a new bookmark
16
+ # item = CloudApp::Item.create :bookmark, :name => "CloudApp", :redirect_url => "http://getcloudapp.com"
17
+ #
18
+ # # Upload a file
19
+ # item = CloudApp::Item.create :upload, :file => "/path/to/image.png"
20
+ #
21
+ # # Rename a file
22
+ # CloudApp::Item.update "http://my.cl.ly/items/1912565", :name => "Big Screenshot"
23
+ #
24
+ # # Set an items privacy
25
+ # CloudApp::Item.update "http://my.cl.ly/items/1912565", :private => true
26
+ #
27
+ # # Delete an item
28
+ # CloudApp::Item.delete "http://my.cl.ly/items/1912565"
29
+ #
30
+ # @example Usage via the class instance
31
+ # # Rename a file
32
+ # @item.update :name => "Big Screenshot"
33
+ #
34
+ # # Set an items privacy
35
+ # @item.update :private => true
36
+ #
37
+ # # Delete an item
38
+ # @tem.delete
39
+ #
40
+ class Item < Base
41
+
42
+ # Get metadata about a cl.ly URL like name, type, or view count.
43
+ # Finds the item by it's slug id, for example "2wr4".
44
+ # @param [String] cl.ly slug id
45
+ # @return [CloudApp::Item]
46
+ def self.find(id)
47
+ res = get "http://cl.ly/#{id}"
48
+ res.ok? ? Item.new(res) : res
49
+ end
50
+
51
+ # Page through your items. Optionally pass a hash of parameters.
52
+ # @example Optionally pass a hash of parameters.
53
+ # :page => 1 # Page number starting at 1
54
+ # :per_page => 5 # Number of items per page
55
+ # :type => image # Filter items by type (image, bookmark, text, archive, audio, video, or unknown)
56
+ # :deleted => true # Show trashed items
57
+ # Requires authentication.
58
+ # @param [Hash] options parameters
59
+ # @return [Array] of CloudApp::Item items
60
+ def self.all(opts = {})
61
+ res = get "/items?#{opts.to_params}", :digest_auth => @@auth
62
+ res.ok? ? res.collect{|i| Item.new(i)} : res
63
+ end
64
+
65
+ # Create a new cl.ly item. Can be one of two types:
66
+ # :bookmark # Bookmark link
67
+ # :upload # Upload file
68
+ # Depending on the type of item, a parameter hash is required.
69
+ # @example Options for a bookmark
70
+ # { :name => "CloudApp", :redirect_url => "http://getcloudapp.com" }
71
+ # @example Options for a file upload
72
+ # { :file => "/path/to/image.png" }
73
+ # Requires authentication.
74
+ # @param [Symbol] type of cl.ly item
75
+ # @param [Hash] item parameters
76
+ # @return [CloudApp::Item]
77
+ def self.create(kind, opts = {})
78
+ case kind
79
+ when :bookmark
80
+ res = post "/items", {:body => {:item => opts}, :digest_auth => @@auth}
81
+ when :upload
82
+ res = get "/items/new", :digest_auth => @@auth
83
+ return res unless res.ok?
84
+ res = post res['url'], Multipart.new(res['params'].merge!(:file => File.new(opts[:file]))).payload.merge!(:digest_auth => @@auth)
85
+ else
86
+ # TODO raise an error
87
+ return false
88
+ end
89
+ res.ok? ? Item.new(res) : res
90
+ end
91
+
92
+ # Modify a cl.ly item. Can currently modify it's name or security setting by passing parameters.
93
+ # @example Options for renaming
94
+ # { :name => "CloudApp" }
95
+ # @example Options for modifying privacy
96
+ # { :privacy => true }
97
+ # Requires authentication.
98
+ # @param [String] href of cl.ly item
99
+ # @param [Hash] item parameters
100
+ # @return [CloudApp::Item]
101
+ def self.update(href, opts = {})
102
+ res = put href, {:body => {:item => opts}, :digest_auth => @@auth}
103
+ res.ok? ? Item.new(res) : res
104
+ end
105
+
106
+ # Send an item to the trash.
107
+ # Requires authentication.
108
+ # @param [String] href of cl.ly item
109
+ # @return [CloudApp::Item]
110
+ def self.delete(href)
111
+ res = Base.delete href, :digest_auth => @@auth
112
+ res.ok? ? Item.new(res) : res
113
+ end
114
+
115
+ attr_reader :href, :name, :private, :url, :content_url, :item_type, :view_counter,
116
+ :icon, :remote_url, :redirect_url, :created_at, :updated_at, :deleted_at
117
+
118
+ # Create a new CloudApp::Item object.
119
+ # Only used internally
120
+ # @param [Hash] attributes
121
+ # @param [CloudApp::Item]
122
+ def initialize(attributes = {})
123
+ load(attributes)
124
+ end
125
+
126
+ # Modify the cl.ly item. Can currently modify it's name or security setting by passing parameters.
127
+ # @example Options for renaming
128
+ # { :name => "CloudApp" }
129
+ # @example Options for modifying privacy
130
+ # { :privacy => true }
131
+ # Requires authentication.
132
+ # @param [Hash] item parameters
133
+ # @return [CloudApp::Item]
134
+ def update(opts = {})
135
+ self.class.update self.href, opts
136
+ end
137
+
138
+ # Send the item to the trash.
139
+ # Requires authentication.
140
+ # @return [CloudApp::Item]
141
+ def delete
142
+ self.class.delete self.href
143
+ end
144
+
145
+ end
146
+
147
+ end