panda 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 New Bamboo
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,13 @@
1
+ = Panda
2
+
3
+ == Usage
4
+
5
+ sudo gem install panda
6
+
7
+ require 'panda'
8
+ Panda.connect!('access_key', 'secret_key', 'localhost', 5678)
9
+ Panda.get('/videos')
10
+
11
+ == Copyright
12
+
13
+ Copyright (c) 2009 New Bamboo. See LICENSE for details.
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "panda"
8
+ gem.summary = %Q{Panda Client}
9
+ gem.description = %Q{Panda Client}
10
+ gem.email = "bambinos@new-bamboo.co.uk"
11
+ gem.homepage = "http://github.com/newbamboo/panda_gem"
12
+ gem.authors = ["New Bamboo"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ gem.add_dependency "ruby-hmac", ">= 0.3.2"
16
+ gem.add_dependency "rest-client", ">= 1.0.3"
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ require 'spec/rake/spectask'
24
+ Spec::Rake::SpecTask.new(:spec) do |spec|
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.spec_files = FileList['spec/**/*_spec.rb']
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ end
34
+
35
+ task :spec => :check_dependencies
36
+
37
+ task :default => :spec
38
+
39
+ require 'rake/rdoctask'
40
+ Rake::RDocTask.new do |rdoc|
41
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "panda #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'hmac'
3
+
4
+ $:.unshift File.dirname(__FILE__) + '/panda'
5
+
6
+ require 'api_authentication'
7
+ require 'video'
@@ -0,0 +1,65 @@
1
+ require 'cgi'
2
+ require 'time'
3
+ require 'hmac'
4
+ require 'hmac-sha2'
5
+ require 'base64'
6
+
7
+ module Panda
8
+ class ApiAuthentication
9
+ def self.authenticate(verb, request_uri, host, secret_key, params_given={})
10
+ # Ensure all param keys are strings
11
+ params = {}; params_given.each {|k,v| params[k.to_s] = v }
12
+
13
+ query_string = canonical_querystring(params)
14
+
15
+ string_to_sign = verb + "\n" +
16
+ host.downcase + "\n" +
17
+ request_uri + "\n" +
18
+ query_string
19
+
20
+ hmac = HMAC::SHA256.new( secret_key )
21
+ hmac.update( string_to_sign )
22
+ # chomp is important! the base64 encoded version will have a newline at the end
23
+ signature = Base64.encode64(hmac.digest).chomp
24
+
25
+ return signature
26
+ end
27
+
28
+ # Insist on specific method of URL encoding, RFC3986.
29
+ def self.url_encode(string)
30
+ # It's kinda like CGI.escape, except CGI.escape is encoding a tilde when
31
+ # it ought not to be, so we turn it back. Also space NEEDS to be %20 not +.
32
+ return CGI.escape(string).gsub("%7E", "~").gsub("+", "%20")
33
+ end
34
+
35
+ # param keys should be strings, not symbols please. return a string joined
36
+ # by & in canonical order.
37
+ def self.canonical_querystring(params)
38
+ # I hope this built-in sort sorts by byte order, that's what's required.
39
+ values = params.keys.sort.collect {|key| [url_encode(key), url_encode(params[key])].join("=") }
40
+
41
+ return values.join("&")
42
+ end
43
+
44
+ def self.add_params_to_request_uri(request_uri, params)
45
+ request_uri + '?' + hash_to_query(params)
46
+ end
47
+ # Insist on specific method of URL encoding, RFC3986.
48
+ def self.url_encode(string)
49
+ string = string.to_s
50
+ # It's kinda like CGI.escape, except CGI.escape is encoding a tilde when
51
+ # it ought not to be, so we turn it back. Also space NEEDS to be %20 not +.
52
+ return CGI.escape(string).gsub("%7E", "~").gsub("+", "%20")
53
+ end
54
+
55
+ # Turns a hash into a query string, returns the query string.
56
+ # url-encodes everything to Amazon's specifications.
57
+ def self.hash_to_query(hash)
58
+ hash.collect do |key, value|
59
+
60
+ url_encode(key) + "=" + url_encode(value)
61
+
62
+ end.join("&")
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,86 @@
1
+ require 'rubygems'
2
+ require 'rest_client'
3
+ require 'api_authentication'
4
+
5
+ module Panda
6
+ class << self
7
+ attr_writer :rest_client_options
8
+ attr_reader :connection
9
+
10
+ def connect!(access_key, secret_key, api_host='api.pandastream.com', api_port=80)
11
+ @api_version = 2
12
+ @access_key = access_key
13
+ @secret_key = secret_key
14
+ @api_host = api_host
15
+ @api_port = api_port
16
+ @rest_client_options = {}
17
+
18
+ # raise Exception.new("You must supply a :secret_key") unless @secret_key
19
+
20
+ @connection = RestClient::Resource.new(api_url)
21
+ end
22
+
23
+ def api_url
24
+ "http://#{@api_host}:#{@api_port}/v#{@api_version}"
25
+ end
26
+
27
+ # NOTE: get params, MUST be given as the params hash, not as part of the url string
28
+ def get(request_uri, params={})
29
+ params = authenticate("GET", request_uri, params)
30
+ puts ApiAuthentication.add_params_to_request_uri(request_uri, params)
31
+ RestClient::Resource.new(concat_urls(api_url, ApiAuthentication.add_params_to_request_uri(request_uri, params))).get
32
+ end
33
+
34
+ def post(request_uri, params)
35
+ params = authenticate("POST", request_uri, params)
36
+ RestClient::Resource.new(concat_urls(api_url, request_uri)).post(params)
37
+ end
38
+
39
+ def put(request_uri, params)
40
+ params = authenticate("PUT", request_uri, params)
41
+ RestClient::Resource.new(concat_urls(api_url, request_uri)).put(params)
42
+ end
43
+
44
+ def delete(request_uri, params={})
45
+ params = authenticate("DELETE", request_uri, params)
46
+ puts ApiAuthentication.add_params_to_request_uri(request_uri, params)
47
+ RestClient::Resource.new(concat_urls(api_url, ApiAuthentication.add_params_to_request_uri(request_uri, params))).delete
48
+ end
49
+
50
+ # From rest-client/lib/restclient/resource.rb
51
+
52
+ def concat_urls(url, suburl) # :nodoc:
53
+ url = url.to_s
54
+ suburl = suburl.to_s
55
+ if url.slice(-1, 1) == '/' or suburl.slice(0, 1) == '/'
56
+ url + suburl
57
+ else
58
+ "#{url}/#{suburl}"
59
+ end
60
+ end
61
+
62
+ # Authentication
63
+
64
+ def authenticate(verb, request_uri, params)
65
+ params['access_key'] = @access_key
66
+ params['timestamp'] = Time.now.iso8601
67
+ params['signature'] = ApiAuthentication.authenticate(verb, request_uri, @api_host, @secret_key, params)
68
+ return params
69
+ end
70
+
71
+ end
72
+
73
+ # class AccountKeyNotSet < PandaError; end
74
+ #
75
+ # # 4xx Client Error
76
+ # class ClientError < ConnectionError; end # :nodoc:
77
+ #
78
+ # # 401 Unauthorized
79
+ # class UnauthorizedAccess < ClientError; end # :nodoc
80
+ #
81
+ # # 404 Not Found
82
+ # class VideoNotFound < ClientError; end # :nodoc:
83
+ #
84
+ # # 5xx Server Error
85
+ # class ServerError < ConnectionError; end # :nodoc:
86
+ end
File without changes
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{panda}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["New Bamboo"]
12
+ s.date = %q{2009-11-04}
13
+ s.description = %q{Panda Client}
14
+ s.email = %q{bambinos@new-bamboo.co.uk}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "lib/panda.rb",
26
+ "lib/panda/api_authentication.rb",
27
+ "lib/panda/video.rb",
28
+ "log/debug.log",
29
+ "panda.gemspec",
30
+ "spec/panda_spec.rb",
31
+ "spec/spec.opts",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/newbamboo/panda_gem}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.5}
38
+ s.summary = %q{Panda Client}
39
+ s.test_files = [
40
+ "spec/panda_spec.rb",
41
+ "spec/spec_helper.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
50
+ s.add_runtime_dependency(%q<ruby-hmac>, [">= 0.3.2"])
51
+ s.add_runtime_dependency(%q<rest-client>, [">= 1.0.3"])
52
+ else
53
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
54
+ s.add_dependency(%q<ruby-hmac>, [">= 0.3.2"])
55
+ s.add_dependency(%q<rest-client>, [">= 1.0.3"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
59
+ s.add_dependency(%q<ruby-hmac>, [">= 0.3.2"])
60
+ s.add_dependency(%q<rest-client>, [">= 1.0.3"])
61
+ end
62
+ end
63
+
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Panda" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'panda'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: panda
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - New Bamboo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-04 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: ruby-hmac
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.2
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: rest-client
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.3
44
+ version:
45
+ description: Panda Client
46
+ email: bambinos@new-bamboo.co.uk
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - LICENSE
53
+ - README.rdoc
54
+ files:
55
+ - .gitignore
56
+ - LICENSE
57
+ - README.rdoc
58
+ - Rakefile
59
+ - VERSION
60
+ - lib/panda.rb
61
+ - lib/panda/api_authentication.rb
62
+ - lib/panda/video.rb
63
+ - log/debug.log
64
+ - panda.gemspec
65
+ - spec/panda_spec.rb
66
+ - spec/spec.opts
67
+ - spec/spec_helper.rb
68
+ has_rdoc: true
69
+ homepage: http://github.com/newbamboo/panda_gem
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --charset=UTF-8
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ version:
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.3.5
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Panda Client
96
+ test_files:
97
+ - spec/panda_spec.rb
98
+ - spec/spec_helper.rb