cloudapp_api 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -2,11 +2,8 @@
2
2
 
3
3
  A simple Ruby wrapper for the [CloudApp API](http://support.getcloudapp.com/faqs/developers/api). Uses [HTTParty](http://github.com/jnunemaker/httparty) with a simple ActiveResource-like interface.
4
4
 
5
- Note: This is functional, but very much a work in progress.
6
-
7
5
  ## TODO
8
6
 
9
- * Get file uploads working
10
7
  * Add tests
11
8
  * Improve the docs
12
9
 
@@ -67,13 +64,11 @@ If you are using the client interface, you must create a client instance.
67
64
 
68
65
  ### Upload a file
69
66
 
70
- NOTE! This is not working yet
71
-
72
67
  @item = client.upload file_name
73
68
 
74
69
  # or ..
75
70
 
76
- @item = CloudApp::Item.create :file, {:path => file_name}
71
+ @item = CloudApp::Item.create :upload, {:file => file_name}
77
72
 
78
73
  ### Delete an item
79
74
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/cloudapp_api.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cloudapp_api}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Aaron Russell"]
12
- s.date = %q{2010-05-17}
12
+ s.date = %q{2010-05-18}
13
13
  s.description = %q{A simple Ruby wrapper for the CloudApp API. Uses HTTParty with a simple ActiveResource-like interface.}
14
14
  s.email = %q{aaron@gc4.co.uk}
15
15
  s.extra_rdoc_files = [
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "lib/cloudapp/httparty.rb",
30
30
  "lib/cloudapp/monkey_patch/httparty.rb",
31
31
  "lib/cloudapp/monkey_patch/net_digest_auth.rb",
32
+ "lib/cloudapp/multipart.rb",
32
33
  "lib/cloudapp_api.rb",
33
34
  "test/helper.rb",
34
35
  "test/test_cloudly.rb"
data/lib/cloudapp/base.rb CHANGED
@@ -1,12 +1,18 @@
1
1
  require "httparty"
2
2
 
3
3
  module CloudApp
4
+
5
+ HEADERS = {
6
+ "User-Agent" => "Ruby.CloudApp.API",
7
+ "Accept" => "application/json",
8
+ "Content-Type" => "application/json"
9
+ }
4
10
 
5
11
  class Base
6
12
 
7
13
  include HTTParty
8
14
  base_uri "my.cl.ly"
9
- headers "Accept" => "application/json", "Content-Type" => "application/json"
15
+ headers HEADERS
10
16
  format :json
11
17
 
12
18
  def self.authenticate(username, password)
@@ -14,44 +20,40 @@ module CloudApp
14
20
  end
15
21
 
16
22
  def self.find(id)
17
- res = get("http://cl.ly/#{id}")
23
+ res = get "http://cl.ly/#{id}"
18
24
  res.ok? ? Item.new(res) : res
19
25
  end
20
26
 
21
27
  def self.all(opts = {})
22
- res = get("/items", opts.merge!(:digest_auth => @@auth))
28
+ res = get "/items", opts.merge!(:digest_auth => @@auth)
23
29
  res.ok? ? res.collect{|i| Item.new(i)} : res
24
30
  end
25
31
 
26
32
  def self.create(kind, opts = {})
27
33
  case kind
28
34
  when :bookmark
29
- res = post "/items", {:digest_auth => @@auth, :query => opts}
30
- res.ok? ? Item.new(res) : res
31
- when :file
32
- res = get "/items/new", {:digest_auth => @@auth}
35
+ res = post "/items", {:query => {:item => opts}, :digest_auth => @@auth}
36
+ when :upload
37
+ res = get "/items/new", :digest_auth => @@auth
33
38
  return res unless res.ok?
34
- res = post res['url'], {
35
- :headers => {"Content-Type" => "multipart/form-data"},
36
- :params => res['params'].merge!(:file => "@#{opts[:path]}")
37
- }
38
- res.ok? ? Item.new(res) : res
39
+ res = post res['url'], Multipart.new(res['params'].merge!(:file => File.new(opts[:file]))).payload.merge!(:digest_auth => @@auth)
39
40
  else
40
- false
41
+ return false
41
42
  end
43
+ res.ok? ? Item.new(res) : res
42
44
  end
43
45
 
44
- attr_accessor :attributes
45
-
46
46
  def initialize(attributes = {})
47
47
  load(attributes)
48
48
  end
49
49
 
50
50
  def delete
51
- res = self.class.delete(self.href, :digest_auth => @@auth)
51
+ res = self.class.delete self.href, :digest_auth => @@auth
52
52
  res.ok? ? true : res
53
53
  end
54
54
 
55
+ private
56
+
55
57
  def load(attributes = {})
56
58
  attributes.each do |key, val|
57
59
  self.instance_variable_set("@#{key}", val)
@@ -21,11 +21,11 @@ module CloudApp
21
21
  end
22
22
 
23
23
  def bookmark(url, name = "")
24
- Item.create(:bookmark, :item => {:name => name, :redirect_url => url})
24
+ Item.create(:bookmark, {:name => name, :redirect_url => url})
25
25
  end
26
26
 
27
27
  def upload(file)
28
- Item.create(:file, :path => file)
28
+ Item.create(:upload, :file => file)
29
29
  end
30
30
 
31
31
  def delete(id)
@@ -10,4 +10,21 @@ module HTTParty
10
10
  @code == 200
11
11
  end
12
12
  end
13
+
14
+ class Request
15
+ private
16
+ def setup_raw_request
17
+ # This is a cloudapp hack to ensure the correct headers are set on redirect from S3
18
+ if @redirect
19
+ options[:headers] = CloudApp::HEADERS
20
+ options[:query] = {}
21
+ options[:body] = {}
22
+ end
23
+ @raw_request = http_method.new(uri.request_uri)
24
+ @raw_request.body = body if body
25
+ @raw_request.initialize_http_header(options[:headers])
26
+ @raw_request.basic_auth(username, password) if options[:basic_auth]
27
+ setup_digest_auth if options[:digest_auth]
28
+ end
29
+ end
13
30
  end
@@ -22,14 +22,6 @@ module HTTParty
22
22
  credentials[:password]
23
23
  end
24
24
 
25
- def setup_raw_request
26
- @raw_request = http_method.new(uri.request_uri)
27
- @raw_request.body = body if body
28
- @raw_request.initialize_http_header(options[:headers])
29
- @raw_request.basic_auth(username, password) if options[:basic_auth]
30
- setup_digest_auth if options[:digest_auth]
31
- end
32
-
33
25
  def setup_digest_auth
34
26
  res = http.head(uri.request_uri, options[:headers])
35
27
  if res['www-authenticate'] != nil && res['www-authenticate'].length > 0
@@ -0,0 +1,49 @@
1
+ require "mime/types"
2
+
3
+ module CloudApp
4
+
5
+ class Multipart
6
+
7
+ EOL = "\r\n"
8
+
9
+ def initialize(params)
10
+ @params = params
11
+ file = @params.delete(:file)
12
+ bound = "--#{boundary}"
13
+ @body = bound + EOL
14
+ @params.each do |key, val|
15
+ @body += create_regular_field(key, val)
16
+ @body += EOL + bound + EOL
17
+ end
18
+ @body += create_file_field(file)
19
+ @body += EOL + bound + "--#{EOL}"
20
+ end
21
+
22
+ def payload
23
+ {
24
+ :headers => {"Content-Type" => "multipart/form-data; boundary=#{boundary}"},
25
+ :body => @body
26
+ }
27
+ end
28
+
29
+ def boundary
30
+ @boundary ||= "#{HEADERS["User-Agent"]}.#{Array.new(16/2) { rand(256) }.pack("C*").unpack("H*").first}"
31
+ end
32
+
33
+ def create_regular_field(key, val)
34
+ %Q{Content-Disposition: form-data; name="#{key}"} + EOL + EOL + val
35
+ end
36
+
37
+ def create_file_field(file)
38
+ %Q{Content-Disposition: form-data; name="file"; filename="#{File.basename(file.path)}"} + EOL +
39
+ "Content-Type: #{mime_for(file.path)}" + EOL + EOL +
40
+ file.read
41
+ end
42
+
43
+ def mime_for(path)
44
+ (MIME::Types.type_for(path)[0] || MIME::Types["application/octet-stream"][0]).simplified
45
+ end
46
+
47
+ end
48
+
49
+ end
data/lib/cloudapp_api.rb CHANGED
@@ -1,12 +1,12 @@
1
1
  require "httparty"
2
2
 
3
- ["base", "client", "httparty"].each do |inc|
3
+ ["base", "client", "multipart", "httparty"].each do |inc|
4
4
  require File.join(File.dirname(__FILE__), "cloudapp", inc)
5
5
  end
6
6
 
7
7
  module CloudApp
8
8
 
9
- VERSION = "0.0.1"
9
+ VERSION = "0.0.2"
10
10
 
11
11
  def CloudApp.authenticate(username, password)
12
12
  Base.authenticate(username, password)
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Aaron Russell
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-17 00:00:00 +01:00
17
+ date: 2010-05-18 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -65,6 +65,7 @@ files:
65
65
  - lib/cloudapp/httparty.rb
66
66
  - lib/cloudapp/monkey_patch/httparty.rb
67
67
  - lib/cloudapp/monkey_patch/net_digest_auth.rb
68
+ - lib/cloudapp/multipart.rb
68
69
  - lib/cloudapp_api.rb
69
70
  - test/helper.rb
70
71
  - test/test_cloudly.rb