em-http-oauth-request 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2009-12-09
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,11 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/em-http-oauth-request.rb
7
+ script/console
8
+ script/destroy
9
+ script/generate
10
+ test/test_em-http-oauth-request.rb
11
+ test/test_helper.rb
data/PostInstall.txt ADDED
File without changes
data/README.rdoc ADDED
@@ -0,0 +1,67 @@
1
+ = em-http-oauth-request
2
+
3
+ == Description
4
+
5
+ Allows you to issue requests with the OAuth gem using em-http-request.
6
+
7
+ The benefit to this is that you can issue asynchronous non-blocking OAuth requests.
8
+
9
+ == Installation
10
+
11
+ sudo gem install em-http-oauth-request
12
+
13
+ == Example
14
+
15
+ require 'rubygems'
16
+ require 'oauth'
17
+ require 'em-http-oauth-request'
18
+
19
+ EventMachine.run {
20
+ consumer = OAuth::Consumer.new('consumer-token', 'consumer-secret', :site => 'http://twitter.com')
21
+ atoken = 'access-token'
22
+ asecret = 'access-secret'
23
+ access_token = OAuth::AccessToken.new(consumer, atoken, asecret)
24
+
25
+ oauth_params = {:consumer => consumer, :token => access_token}
26
+ req = EventMachine::HttpRequest.new('http://api.twitter.com/1/statuses/home_timeline.json').get
27
+ oauth_helper = OAuth::Client::Helper.new(req, oauth_params)
28
+
29
+ req.options[:head] = (req.options[:head] || {}).merge!({"Authorization " => [oauth_helper.header]})
30
+ req.callback {
31
+ p req.response
32
+
33
+ EventMachine.stop
34
+ }
35
+ }
36
+
37
+
38
+ == Issues
39
+
40
+ There's currently an issue with em-http-request in which values of 'Authorization' headers are automatically Base64 encoded. While this is correct for Basic HTTP Authentication, it breaks OAuth requests, which require a number of parameters in the 'Authorization' header.
41
+
42
+ For the time being, a simple workaround is to add whitespace to the header (for example, 'Authorization ' in the example above). This works for Twitter's OAuth API, although it may not work for all OAuth providers.
43
+
44
+ == License
45
+
46
+ (The MIT License)
47
+
48
+ Copyright (c) 2009 Draconis Software, LLC
49
+
50
+ Permission is hereby granted, free of charge, to any person obtaining
51
+ a copy of this software and associated documentation files (the
52
+ 'Software'), to deal in the Software without restriction, including
53
+ without limitation the rights to use, copy, modify, merge, publish,
54
+ distribute, sublicense, and/or sell copies of the Software, and to
55
+ permit persons to whom the Software is furnished to do so, subject to
56
+ the following conditions:
57
+
58
+ The above copyright notice and this permission notice shall be
59
+ included in all copies or substantial portions of the Software.
60
+
61
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
62
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
63
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
64
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
65
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
66
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
67
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/em-http-oauth-request'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'em-http-oauth-request' do
14
+ self.developer 'Draconis Software', 'info@draconis.com'
15
+ self.rubyforge_name = self.name # TODO this is default value
16
+ self.extra_deps = [['oauth','>= 0.3.6'], ['em-http-request', '>= 0.2.4']]
17
+ self.summary = 'Allows em-http-request to be used for OAuth requests.'
18
+ self.url = 'http://github.com/draconis/em-http-oauth-request'
19
+ end
20
+
21
+ require 'newgem/tasks'
22
+ Dir['tasks/**/*.rake'].each { |t| load t }
23
+
24
+ # TODO - want other tests/tasks run by default? Add them to the list
25
+ # remove_task :default
26
+ # task :default => [:spec, :features]
@@ -0,0 +1,79 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module EmHttpOauthRequest
5
+ VERSION = '0.0.1'
6
+ end
7
+
8
+ require 'oauth'
9
+ require 'em-http'
10
+
11
+ module OAuth::RequestProxy::EventMachine
12
+ class Client < OAuth::RequestProxy::Base
13
+ # Proxy for signing Typhoeus::Request requests
14
+ # Usage example:
15
+ # EventMachine.run {
16
+ # oauth_params = {:consumer => oauth_consumer, :token => access_token}
17
+ # req = EventMachine::HttpRequest.new(uri).get(options)
18
+ # oauth_helper = OAuth::Client::Helper.new(req, oauth_params)
19
+ # req.options[:head] = (req.options[:head] || {}).merge!({"Authorization " => [oauth_helper.header]})
20
+ # req.callback {
21
+ # p req.response
22
+ #
23
+ # EventMachine.stop
24
+ # }
25
+ # }
26
+ #
27
+ # NOTE: currently, em-http-request automatically base64 encodes the 'Authorization' header, which
28
+ # breaks OAuth. It appears that making the key of the header 'Authorization ' (note the space)
29
+ # fixes this for the time being.
30
+ proxies ::EventMachine::HttpClient
31
+
32
+ def method
33
+ request.method
34
+ end
35
+
36
+ def uri
37
+ request.uri.to_s
38
+ end
39
+
40
+ def parameters
41
+ if options[:clobber_request]
42
+ options[:parameters]
43
+ else
44
+ all_parameters
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def all_parameters
51
+ request_params = {}
52
+ params = post_parameters.merge(query_parameters).merge(options[:parameters] || {})
53
+ if params
54
+ params.each do |k,v|
55
+ if request_params.has_key?(k)
56
+ request_params[k] << v
57
+ else
58
+ request_params[k] = [v].flatten
59
+ end
60
+ end
61
+ end
62
+ request_params
63
+ end
64
+
65
+ def query_parameters
66
+ request.options[:query] || {}
67
+ end
68
+
69
+ def post_parameters
70
+ # Post params are only used if posting form data
71
+ headers = request.options[:head] || {}
72
+ if((method == 'POST' || method == 'PUT') && headers['Content-Type'] && headers['Content-Type'].downcase == 'application/x-www-form-urlencoded')
73
+ request.options[:body] || {}
74
+ else
75
+ {}
76
+ end
77
+ end
78
+ end
79
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/em-http-oauth-request.rb'}"
9
+ puts "Loading em-http-oauth-request gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,93 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestEmHttpOauthRequest < Test::Unit::TestCase
4
+ def test_that_proxy_simple_get_request_works
5
+ EventMachine.run {
6
+ request = ::EventMachine::HttpRequest.new('http://example.com/test').get :query => {'key' => 'value'}
7
+
8
+ request_proxy = OAuth::RequestProxy.proxy(request)
9
+
10
+ expected_parameters = {'key' => ['value']}
11
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
12
+ assert_equal 'http://example.com/test', request_proxy.normalized_uri
13
+ assert_equal 'GET', request_proxy.method
14
+
15
+ EventMachine.stop
16
+ }
17
+ end
18
+
19
+ def test_that_proxy_simple_post_request_works_with_arguments
20
+ EventMachine.run {
21
+ request = ::EventMachine::HttpRequest.new('http://example.com/test').post
22
+ params = {'key' => 'value'}
23
+ request_proxy = OAuth::RequestProxy.proxy(request, {:parameters => params})
24
+
25
+ expected_parameters = {'key' => ['value']}
26
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
27
+ assert_equal 'http://example.com/test', request_proxy.normalized_uri
28
+ assert_equal 'POST', request_proxy.method
29
+
30
+ EventMachine.stop
31
+ }
32
+ end
33
+
34
+ def test_that_proxy_simple_post_request_works_with_form_data
35
+ EventMachine.run {
36
+ request = ::EventMachine::HttpRequest.new('http://example.com/test').post :body => {'key' => 'value'},
37
+ :head => {'Content-Type' => 'application/x-www-form-urlencoded'}
38
+ request_proxy = OAuth::RequestProxy.proxy(request)
39
+
40
+ expected_parameters = {'key' => ['value']}
41
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
42
+ assert_equal 'http://example.com/test', request_proxy.normalized_uri
43
+ assert_equal 'POST', request_proxy.method
44
+
45
+ EventMachine.stop
46
+ }
47
+ end
48
+
49
+ def test_that_proxy_simple_put_request_works_with_arguments
50
+ EventMachine.run {
51
+ request = ::EventMachine::HttpRequest.new('http://example.com/test').put
52
+ params = {'key' => 'value'}
53
+ request_proxy = OAuth::RequestProxy.proxy(request, {:parameters => params})
54
+
55
+ expected_parameters = {'key' => ['value']}
56
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
57
+ assert_equal 'http://example.com/test', request_proxy.normalized_uri
58
+ assert_equal 'PUT', request_proxy.method
59
+
60
+ EventMachine.stop
61
+ }
62
+ end
63
+
64
+ def test_that_proxy_simple_put_request_works_with_form_data
65
+ EventMachine.run {
66
+ request = ::EventMachine::HttpRequest.new('http://example.com/test').put :body => {'key' => 'value'},
67
+ :head => {'Content-Type' => 'application/x-www-form-urlencoded'}
68
+ request_proxy = OAuth::RequestProxy.proxy(request)
69
+
70
+ expected_parameters = {'key' => ['value']}
71
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
72
+ assert_equal 'http://example.com/test', request_proxy.normalized_uri
73
+ assert_equal 'PUT', request_proxy.method
74
+
75
+ EventMachine.stop
76
+ }
77
+ end
78
+
79
+ def test_that_proxy_post_request_works_with_mixed_parameter_sources
80
+ EventMachine.run {
81
+ request = ::EventMachine::HttpRequest.new('http://example.com/test').post :query => {'key' => 'value'}, :body => {'key2' => 'value2'},
82
+ :head => {'Content-Type' => 'application/x-www-form-urlencoded'}
83
+ request_proxy = OAuth::RequestProxy.proxy(request, :parameters => {'key3' => 'value3'})
84
+
85
+ expected_parameters = {'key' => ['value'], 'key2' => ['value2'], 'key3' => ['value3']}
86
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
87
+ assert_equal 'http://example.com/test', request_proxy.normalized_uri
88
+ assert_equal 'POST', request_proxy.method
89
+
90
+ EventMachine.stop
91
+ }
92
+ end
93
+ end
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/em-http-oauth-request'
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: em-http-oauth-request
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Draconis Software
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-10 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: oauth
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.3.6
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: em-http-request
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.4
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: hoe
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.4.0
44
+ version:
45
+ description: |-
46
+ Allows you to issue requests with the OAuth gem using em-http-request.
47
+
48
+ The benefit to this is that you can issue asynchronous non-blocking OAuth requests.
49
+ email:
50
+ - info@draconis.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - History.txt
57
+ - Manifest.txt
58
+ - PostInstall.txt
59
+ files:
60
+ - History.txt
61
+ - Manifest.txt
62
+ - PostInstall.txt
63
+ - README.rdoc
64
+ - Rakefile
65
+ - lib/em-http-oauth-request.rb
66
+ - script/console
67
+ - script/destroy
68
+ - script/generate
69
+ - test/test_em-http-oauth-request.rb
70
+ - test/test_helper.rb
71
+ has_rdoc: true
72
+ homepage: http://github.com/draconis/em-http-oauth-request
73
+ licenses: []
74
+
75
+ post_install_message:
76
+ rdoc_options:
77
+ - --main
78
+ - README.rdoc
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
86
+ version:
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: "0"
92
+ version:
93
+ requirements: []
94
+
95
+ rubyforge_project: em-http-oauth-request
96
+ rubygems_version: 1.3.5
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Allows em-http-request to be used for OAuth requests.
100
+ test_files:
101
+ - test/test_em-http-oauth-request.rb
102
+ - test/test_helper.rb