panda 0.4.4 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.4
1
+ 0.4.5
data/lib/panda/panda.rb CHANGED
@@ -3,19 +3,23 @@ require 'restclient'
3
3
  class Panda
4
4
  attr_reader :api_host, :api_port, :access_key, :secret_key, :api_version
5
5
 
6
- def initialize(auth_params={})
7
- params = {:api_host => 'api.pandastream.com', :api_port => 80 }.merge(auth_params)
8
-
6
+ DEFAULT_API_PORT=80
7
+ DEFAULT_API_HOST="api.pandastream.com"
8
+
9
+ def initialize(auth_params)
9
10
  @api_version = 2
10
- @cloud_id = params["cloud_id"] || params[:cloud_id]
11
- @access_key = params["access_key"] || params[:access_key]
12
- @secret_key = params["secret_key"] || params[:secret_key]
13
- @api_host = params["api_host"] || params[:api_host]
14
- @api_port = params["api_port"] || params[:api_port]
15
- @prefix = params["prefix_url"] || "v#{@api_version}"
11
+
12
+ if auth_params.class == String
13
+ init_from_url(auth_params)
14
+ else
15
+ init_from_hash(auth_params)
16
+ end
17
+
16
18
  @connection = RestClient::Resource.new(api_url)
17
19
  end
18
-
20
+
21
+
22
+
19
23
  def get(request_uri, params={})
20
24
  rescue_restclient_exception do
21
25
  query = signed_query("GET", request_uri, params)
@@ -41,7 +45,7 @@ class Panda
41
45
  body_of @connection[request_uri + '?' + query].delete
42
46
  end
43
47
  end
44
-
48
+
45
49
  def authentication_params(verb, request_uri, params)
46
50
  auth_params = {}
47
51
  auth_params['cloud_id'] = @cloud_id
@@ -50,43 +54,72 @@ class Panda
50
54
  auth_params['signature'] = ApiAuthentication.authenticate(verb, request_uri, @api_host, @secret_key, params.merge(auth_params))
51
55
  return auth_params
52
56
  end
53
-
57
+
54
58
  def signed_query(*args)
55
59
  ApiAuthentication.hash_to_query(signed_params(*args))
56
60
  end
57
-
61
+
58
62
  def signed_params(verb, request_uri, params = {}, timestamp_str = nil)
59
63
  auth_params = params
60
64
  auth_params['cloud_id'] = @cloud_id
61
65
  auth_params['access_key'] = @access_key
62
66
  auth_params['timestamp'] = timestamp_str || Time.now.iso8601(6)
63
-
67
+
64
68
  params_to_sign = auth_params.reject{|k,v| ['file'].include?(k)}
65
69
  auth_params['signature'] = ApiAuthentication.generate_signature(verb, request_uri, @api_host, @secret_key, params_to_sign)
66
70
  auth_params
67
71
  end
68
-
72
+
69
73
  def api_url
70
74
  "http://#{@api_host}:#{@api_port}/#{@prefix}"
71
75
  end
72
-
76
+
73
77
  def setup_bucket(params={})
74
78
  granting_params = { :s3_videos_bucket => params[:bucket], :user_aws_key => params[:access_key], :user_aws_secret => params[:secret_key] }
75
79
  put("/clouds/#{@cloud_id}.json", granting_params)
76
80
  end
77
-
81
+
78
82
  private
79
-
80
- def rescue_restclient_exception(&block)
81
- begin
82
- yield
83
- rescue RestClient::Exception => e
84
- e.http_body
83
+
84
+ def rescue_restclient_exception(&block)
85
+ begin
86
+ yield
87
+ rescue RestClient::Exception => e
88
+ e.http_body
89
+ end
90
+ end
91
+
92
+ # API change on rest-client 1.4
93
+ def body_of(response)
94
+ response.respond_to?(:body) ? response.body : response
95
+ end
96
+
97
+
98
+ def init_from_url(url)
99
+ params = url.scan(/^([^:@]+):([^:@]+)@([^:@]+)(:[\d]+)?\/([^:@]+)$/).flatten
100
+
101
+ @access_key = params[0]
102
+ @secret_key = params[1]
103
+ @cloud_id = params[4]
104
+ @api_host = params[2]
105
+
106
+ if params[3]
107
+ @api_port = params[3][1..-1]
108
+ else
109
+ @api_port = DEFAULT_API_PORT
110
+ end
111
+ @prefix = "v#{@api_version}"
112
+
113
+ end
114
+
115
+ def init_from_hash(hash_params)
116
+ params = { :api_host => DEFAULT_API_HOST, :api_port => DEFAULT_API_PORT }.merge(hash_params)
117
+
118
+ @cloud_id = params["cloud_id"] || params[:cloud_id]
119
+ @access_key = params["access_key"] || params[:access_key]
120
+ @secret_key = params["secret_key"] || params[:secret_key]
121
+ @api_host = params["api_host"] || params[:api_host]
122
+ @api_port = params["api_port"] || params[:api_port]
123
+ @prefix = params["prefix_url"] || "v#{@api_version}"
85
124
  end
86
- end
87
-
88
- # API change on rest-client 1.4
89
- def body_of(response)
90
- response.respond_to?(:body) ? response.body : response
91
- end
92
125
  end
data/panda.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{panda}
8
- s.version = "0.4.4"
8
+ s.version = "0.4.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["New Bamboo"]
12
- s.date = %q{2010-03-08}
12
+ s.date = %q{2010-03-11}
13
13
  s.description = %q{Panda Client}
14
14
  s.email = %q{info@pandastream.com}
15
15
  s.extra_rdoc_files = [
data/spec/panda_spec.rb CHANGED
@@ -3,74 +3,89 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
3
  describe Panda do
4
4
  before(:each) do
5
5
  FakeWeb.allow_net_connect = false
6
- @panda = Panda.new({"access_key" => "my_access_key", "secret_key" => "my_secret_key", "api_host" => "myapihost", "api_port" => 85, "cloud_id" => 'my_cloud_id' })
7
6
  Time.stub!(:now).and_return(mock("time", :iso8601 => "2009-11-04T17:54:11+00:00"))
8
7
  end
9
-
10
-
11
- it "should make get request with signed request to panda server" do
12
- FakeWeb.register_uri(:get, "http://myapihost:85/v2/videos?timestamp=2009-11-04T17%3A54%3A11%2B00%3A00&signature=CxSYPM65SeeWH4CE%2FLcq7Ny2NtwxlpS8QOXG2BKe4p8%3D&access_key=my_access_key&cloud_id=my_cloud_id", :body => "abc")
13
- @panda.get("/videos").should == "abc"
14
- end
15
8
 
16
- it "should make delete request with signed request to panda server" do
17
- FakeWeb.register_uri(:delete, "http://myapihost:85/v2/videos/1?timestamp=2009-11-04T17%3A54%3A11%2B00%3A00&signature=t0IYclDXgjZFRYaMf0Gbg%2B5vOqp7q8QQRN8tlQ3bk8Q%3D&access_key=my_access_key&cloud_id=my_cloud_id", :query => {})
18
- @panda.delete("/videos/1").should
19
- FakeWeb.should have_requested(:delete, "http://myapihost:85/v2/videos/1?timestamp=2009-11-04T17%3A54%3A11%2B00%3A00&signature=t0IYclDXgjZFRYaMf0Gbg%2B5vOqp7q8QQRN8tlQ3bk8Q%3D&access_key=my_access_key&cloud_id=my_cloud_id")
20
- end
21
-
22
- it "should create a signed version of the parameters" do
23
- signed_params = @panda.signed_params('POST',
24
- '/videos.json',
25
- {"param1" => 'one', "param2" => 'two'}
26
- )
27
- signed_params.should == {
28
- 'access_key' => "my_access_key",
29
- 'timestamp' => "2009-11-04T17:54:11+00:00",
30
- 'cloud_id' => 'my_cloud_id',
31
- 'signature' => 'w66goW6Ve5CT9Ibbx3ryvq4XM8OfIfSZe5oapgZBaUs=',
32
- 'param1' => 'one',
33
- 'param2' => 'two'
34
- }
35
- end
9
+ describe "Connected with Hash" do
10
+ before(:each) do
11
+ @panda = Panda.new({"access_key" => "my_access_key", "secret_key" => "my_secret_key", "api_host" => "myapihost", "api_port" => 85, "cloud_id" => 'my_cloud_id' })
12
+ end
36
13
 
37
- it "should create a signed version of the parameters without additional arguments" do
38
- @panda.signed_params('POST', '/videos.json').should == {
39
- 'access_key' => "my_access_key",
40
- 'timestamp' => "2009-11-04T17:54:11+00:00",
41
- 'cloud_id' => 'my_cloud_id',
42
- 'signature' => 'TI2n/dsSllxFhxcEShRGKWtDSqxu+kuJUPs335NavMo='
43
- }
44
- end
14
+ it "should make get request with signed request to panda server" do
15
+ FakeWeb.register_uri(:get, "http://myapihost:85/v2/videos?timestamp=2009-11-04T17%3A54%3A11%2B00%3A00&signature=CxSYPM65SeeWH4CE%2FLcq7Ny2NtwxlpS8QOXG2BKe4p8%3D&access_key=my_access_key&cloud_id=my_cloud_id", :body => "abc")
16
+ @panda.get("/videos").should == "abc"
17
+ end
18
+
19
+ it "should make delete request with signed request to panda server" do
20
+ FakeWeb.register_uri(:delete, "http://myapihost:85/v2/videos/1?timestamp=2009-11-04T17%3A54%3A11%2B00%3A00&signature=t0IYclDXgjZFRYaMf0Gbg%2B5vOqp7q8QQRN8tlQ3bk8Q%3D&access_key=my_access_key&cloud_id=my_cloud_id", :query => {})
21
+ @panda.delete("/videos/1").should
22
+ FakeWeb.should have_requested(:delete, "http://myapihost:85/v2/videos/1?timestamp=2009-11-04T17%3A54%3A11%2B00%3A00&signature=t0IYclDXgjZFRYaMf0Gbg%2B5vOqp7q8QQRN8tlQ3bk8Q%3D&access_key=my_access_key&cloud_id=my_cloud_id")
23
+ end
45
24
 
25
+ it "should create a signed version of the parameters" do
26
+ signed_params = @panda.signed_params('POST',
27
+ '/videos.json',
28
+ {"param1" => 'one', "param2" => 'two'}
29
+ )
30
+ signed_params.should == {
31
+ 'access_key' => "my_access_key",
32
+ 'timestamp' => "2009-11-04T17:54:11+00:00",
33
+ 'cloud_id' => 'my_cloud_id',
34
+ 'signature' => 'w66goW6Ve5CT9Ibbx3ryvq4XM8OfIfSZe5oapgZBaUs=',
35
+ 'param1' => 'one',
36
+ 'param2' => 'two'
37
+ }
38
+ end
46
39
 
47
- it "should create a signed version of the parameters and difficult characters" do
48
- signed_params = @panda.signed_params('POST',
49
- '/videos.json',
50
- {"tilde" => '~', "space" => ' '}
51
- )
52
- signed_params.should == {
53
- 'access_key' => "my_access_key",
54
- 'timestamp' => "2009-11-04T17:54:11+00:00",
55
- 'cloud_id' => 'my_cloud_id',
56
- 'signature' => 'w5P9+xPpQpRlweTh0guFYqQOmF+ZuTKXCmaKpUP3sH0=',
57
- 'tilde' => '~',
58
- 'space' => ' '
59
- }
40
+ it "should create a signed version of the parameters without additional arguments" do
41
+ @panda.signed_params('POST', '/videos.json').should == {
42
+ 'access_key' => "my_access_key",
43
+ 'timestamp' => "2009-11-04T17:54:11+00:00",
44
+ 'cloud_id' => 'my_cloud_id',
45
+ 'signature' => 'TI2n/dsSllxFhxcEShRGKWtDSqxu+kuJUPs335NavMo='
46
+ }
47
+ end
48
+
49
+
50
+ it "should create a signed version of the parameters and difficult characters" do
51
+ signed_params = @panda.signed_params('POST',
52
+ '/videos.json',
53
+ {"tilde" => '~', "space" => ' '}
54
+ )
55
+ signed_params.should == {
56
+ 'access_key' => "my_access_key",
57
+ 'timestamp' => "2009-11-04T17:54:11+00:00",
58
+ 'cloud_id' => 'my_cloud_id',
59
+ 'signature' => 'w5P9+xPpQpRlweTh0guFYqQOmF+ZuTKXCmaKpUP3sH0=',
60
+ 'tilde' => '~',
61
+ 'space' => ' '
62
+ }
63
+ end
64
+
65
+ it "should return a json file for every http code" do
66
+ FakeWeb.register_uri(:get, "http://myapihost:85/v2/videos?timestamp=2009-11-04T17%3A54%3A11%2B00%3A00&signature=CxSYPM65SeeWH4CE%2FLcq7Ny2NtwxlpS8QOXG2BKe4p8%3D&access_key=my_access_key&cloud_id=my_cloud_id", :body => "abc")
67
+
68
+ resource = RestClient::Resource.new("http://myapihost:85/v2")
69
+ RestClient::Resource.stub!(:new).and_return(resource)
70
+
71
+ e = RestClient::Exception.new({:body => "abc", :code => 400})
72
+ e.stub!(:http_body).and_return("abc")
73
+
74
+ resource.stub!(:get).and_raise(e)
75
+
76
+ panda = Panda.new({"access_key" => "my_access_key", "secret_key" => "my_secret_key", "api_host" => "myapihost", "api_port" => 85, "cloud_id" => 'my_cloud_id' })
77
+ panda.get("/videos").should == "abc"
78
+ end
60
79
  end
61
80
 
62
- it "should return a json file for every http code" do
63
- FakeWeb.register_uri(:get, "http://myapihost:85/v2/videos?timestamp=2009-11-04T17%3A54%3A11%2B00%3A00&signature=CxSYPM65SeeWH4CE%2FLcq7Ny2NtwxlpS8QOXG2BKe4p8%3D&access_key=my_access_key&cloud_id=my_cloud_id", :body => "abc")
64
-
65
- resource = RestClient::Resource.new("http://myapihost:85/v2")
66
- RestClient::Resource.stub!(:new).and_return(resource)
67
-
68
- e = RestClient::Exception.new({:body => "abc", :code => 400})
69
- e.stub!(:http_body).and_return("abc")
70
-
71
- resource.stub!(:get).and_raise(e)
72
-
73
- panda = Panda.new({"access_key" => "my_access_key", "secret_key" => "my_secret_key", "api_host" => "myapihost", "api_port" => 85, "cloud_id" => 'my_cloud_id' })
74
- panda.get("/videos").should == "abc"
81
+ describe "Connected with a string url" do
82
+ before(:each) do
83
+ @panda2 = Panda.new("f4676630-0522-11df-8148-1231350015b1:1ucba5EDJGf9aIl3/ve1klCkRQqiYZ1bzI+Dpvl3@myhost:85/eefa5d2cbe74289a500e62e2789347f3")
84
+ end
85
+
86
+ it "should make get request" do
87
+ FakeWeb.register_uri(:get, "http://myhost:85/v2/videos?timestamp=2009-11-04T17%3A54%3A11%2B00%3A00&signature=YcoBrpnRTwpgEpA%2FPO%2FMJr4d94suUqt5acQv67owUew%3D&access_key=f4676630-0522-11df-8148-1231350015b1&cloud_id=eefa5d2cbe74289a500e62e2789347f3", :body => "abc")
88
+ @panda2.get("/videos").should == "abc"
89
+ end
75
90
  end
76
91
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: panda
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - New Bamboo
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-08 00:00:00 +00:00
12
+ date: 2010-03-11 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency