WatersOfOblivion-activeresourceinstanceauthentication 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README +15 -0
- data/Rakefile +22 -0
- data/init.rb +1 -0
- data/install.rb +1 -0
- data/lib/active_resource_instance_authentication.rb +99 -0
- data/rails/init.rb +4 -0
- data/test/active_resource_instance_authentication_test.rb +122 -0
- data/uninstall.rb +1 -0
- metadata +64 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 [name of plugin creator]
|
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.
|
data/README
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
=ActiveResourceInstanceAuthentication
|
2
|
+
|
3
|
+
This is just a little plugin to allow individual instances of ActiveResource
|
4
|
+
to use their own HTTP Basic Authenticated connection.
|
5
|
+
|
6
|
+
See ActiveResourceInstanceAuthentication::ActiveResourceExtensions for
|
7
|
+
documentation.
|
8
|
+
|
9
|
+
This is packaged as a Gem based plugin, so it can be installed by adding the
|
10
|
+
following line to <tt>config/environment.rb</tt> and running
|
11
|
+
<tt>rake gems:install</tt>.
|
12
|
+
|
13
|
+
config.gem "WatersOfOblivion-activeresourceinstanceauthentication", :lib => "activeresourceinstanceauthentication", :source => "http://gems.github.com/"
|
14
|
+
|
15
|
+
Copyright (c) 2009 Jonathan Bryant, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the active_resource_instance_authentication plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the active_resource_instance_authentication plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'ActiveResourceInstanceAuthentication'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'rails', 'init')
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
@@ -0,0 +1,99 @@
|
|
1
|
+
module ActiveResourceInstanceAuthentication
|
2
|
+
# This alters ActiveResource::Base to use authentication for each instance
|
3
|
+
# instead of the class. Each instance uses the class settings as defaults.
|
4
|
+
#
|
5
|
+
# class SomeModel < ActiveResource::Base
|
6
|
+
# self.user = "user"
|
7
|
+
# self.password = "password"
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
# # Uses "user":"password"
|
11
|
+
# model_one = SomeModel.new
|
12
|
+
#
|
13
|
+
# # Uses "admin":"secret"
|
14
|
+
# model_two = SomeModel.new
|
15
|
+
# model_two.user = "admin"
|
16
|
+
# model_two.password = "secret"
|
17
|
+
#
|
18
|
+
# It also adds a simpler way of performing authenticated actions: the
|
19
|
+
# +authenticate+ method.
|
20
|
+
#
|
21
|
+
# # Get an authenticated instance
|
22
|
+
# SomeModel.authenticate "user", "password"
|
23
|
+
#
|
24
|
+
# # Yield an authenticated instance
|
25
|
+
# SomeModel.authenticate "user", "password" do |m|
|
26
|
+
# # Call authenticated methods on m
|
27
|
+
# end
|
28
|
+
module ActiveResourceExtensions
|
29
|
+
def self.included(base)
|
30
|
+
base.send :extend, ClassMethods
|
31
|
+
|
32
|
+
base.class_eval do
|
33
|
+
def site
|
34
|
+
self.class.site
|
35
|
+
end
|
36
|
+
|
37
|
+
def timeout
|
38
|
+
@timeout ||= self.class.timeout
|
39
|
+
end
|
40
|
+
|
41
|
+
def timeout=(timeout)
|
42
|
+
@connection = nil
|
43
|
+
@timeout = timeout
|
44
|
+
end
|
45
|
+
|
46
|
+
def user
|
47
|
+
@user ||= self.class.user
|
48
|
+
end
|
49
|
+
|
50
|
+
def user=(user)
|
51
|
+
@connection = nil
|
52
|
+
@user = user
|
53
|
+
end
|
54
|
+
|
55
|
+
def password
|
56
|
+
@password ||= self.class.password
|
57
|
+
end
|
58
|
+
|
59
|
+
def password=(password)
|
60
|
+
@connection = nil
|
61
|
+
@password = password
|
62
|
+
end
|
63
|
+
|
64
|
+
def format
|
65
|
+
@format ||= self.class.format
|
66
|
+
end
|
67
|
+
|
68
|
+
def format=(format)
|
69
|
+
@connection = nil
|
70
|
+
@format = format
|
71
|
+
end
|
72
|
+
|
73
|
+
def connection(refresh = false)
|
74
|
+
if refresh || @connection.nil?
|
75
|
+
@connection = ActiveResource::Connection.new(site, format)
|
76
|
+
@connection.user = user if user
|
77
|
+
@connection.password = password if user
|
78
|
+
@connection.timeout = timeout if timeout
|
79
|
+
end
|
80
|
+
@connection
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
module ClassMethods
|
86
|
+
def authenticate(username, password)
|
87
|
+
obj = self.new
|
88
|
+
obj.user = username
|
89
|
+
obj.password = password
|
90
|
+
|
91
|
+
if block_given?
|
92
|
+
yield obj
|
93
|
+
else
|
94
|
+
obj
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'active_resource'
|
4
|
+
require 'active_resource_instance_authentication'
|
5
|
+
|
6
|
+
class ActiveResourceInstanceAuthenticationTest < Test::Unit::TestCase
|
7
|
+
def setup
|
8
|
+
ActiveResource::Base.send :include, ActiveResourceInstanceAuthentication::ActiveResourceExtensions
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_site
|
12
|
+
assert_respond_to ActiveResource::Base.new, :site
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_site_inherits_from_class
|
16
|
+
ar = ActiveResource::Base.new
|
17
|
+
assert_nil ar.site
|
18
|
+
ActiveResource::Base.site = "http://localhost/"
|
19
|
+
assert_equal URI.parse("http://localhost/"), ar.site
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_site=
|
23
|
+
ar = ActiveResource::Base.new
|
24
|
+
assert_respond_to ar, :site=
|
25
|
+
ar.site = "http://localhost/"
|
26
|
+
assert_equal URI.parse("http://localhost/"), ar.site
|
27
|
+
assert_nil ar.instance_variable_get("@connection")
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_timeout
|
31
|
+
assert_respond_to ActiveResource::Base.new, :timeout
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_timeout_inherits_from_class
|
35
|
+
ar = ActiveResource::Base.new
|
36
|
+
assert_nil ar.timeout
|
37
|
+
ActiveResource::Base.timeout = 30
|
38
|
+
assert_equal 30, ar.timeout
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_timeout=
|
42
|
+
ar = ActiveResource::Base.new
|
43
|
+
assert_respond_to ar, :timeout=
|
44
|
+
ar.timeout = 30
|
45
|
+
assert_equal 30, ar.timeout
|
46
|
+
assert_nil ar.instance_variable_get("@connection")
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_user
|
50
|
+
assert_respond_to ActiveResource::Base.new, :user
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_user_inherits_from_class
|
54
|
+
ar = ActiveResource::Base.new
|
55
|
+
assert_nil ar.user
|
56
|
+
ActiveResource::Base.user = "jdoe"
|
57
|
+
assert_equal "jdoe", ar.user
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_user=
|
61
|
+
ar = ActiveResource::Base.new
|
62
|
+
assert_respond_to ar, :user=
|
63
|
+
ar.user = "jdoe"
|
64
|
+
assert_equal "jdoe", ar.user
|
65
|
+
assert_nil ar.instance_variable_get("@connection")
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_password
|
69
|
+
assert_respond_to ActiveResource::Base.new, :password
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_password_inherits_from_class
|
73
|
+
ar = ActiveResource::Base.new
|
74
|
+
assert_nil ar.password
|
75
|
+
ActiveResource::Base.password = "password"
|
76
|
+
assert_equal "password", ar.password
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_password=
|
80
|
+
ar = ActiveResource::Base.new
|
81
|
+
assert_respond_to ar, :password=
|
82
|
+
ar.password = "password"
|
83
|
+
assert_equal "password", ar.password
|
84
|
+
assert_nil ar.instance_variable_get("@connection")
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_format
|
88
|
+
assert_respond_to ActiveResource::Base.new, :format
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_format_inherits_from_class
|
92
|
+
ar = ActiveResource::Base.new
|
93
|
+
assert_equal ActiveResource::Formats::XmlFormat, ar.format
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_format=
|
97
|
+
ar = ActiveResource::Base.new
|
98
|
+
ar.format = :xml
|
99
|
+
assert_equal :xml, ar.format
|
100
|
+
assert_nil ar.instance_variable_get("@connection")
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_connection
|
104
|
+
ar = ActiveResource::Base.new
|
105
|
+
assert_respond_to ar, :connection
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_authenticate
|
109
|
+
assert_respond_to ActiveResource::Base, :authenticate
|
110
|
+
ar = ActiveResource::Base.authenticate("foo", "bar")
|
111
|
+
assert ar.is_a? ActiveResource::Base
|
112
|
+
assert_equal "foo", ar.user
|
113
|
+
assert_equal "bar", ar.password
|
114
|
+
ar = ActiveResource::Base.authenticate("foo", "bar", "http://www.site.com/")
|
115
|
+
assert_equal URI.parse("http://www.site.com/"), ar.site
|
116
|
+
assert_raises RuntimeError do
|
117
|
+
ActiveResource::Base.authenticate "foo", "bar" do
|
118
|
+
raise "I yield to the block"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: WatersOfOblivion-activeresourceinstanceauthentication
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonathan Bryant
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-05 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: ""
|
17
|
+
email: watersofmemory@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- init.rb
|
26
|
+
- install.rb
|
27
|
+
- MIT-LICENSE
|
28
|
+
- Rakefile
|
29
|
+
- README
|
30
|
+
- uninstall.rb
|
31
|
+
- rails/init.rb
|
32
|
+
- test/active_resource_instance_authentication_test.rb
|
33
|
+
- lib/active_resource_instance_authentication.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://watersofoblivion.github.com/activeresourceinstanceauthentication/
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options:
|
38
|
+
- --title
|
39
|
+
- ActiveResource Instance Authentication
|
40
|
+
- --line-numbers
|
41
|
+
- --inline-source
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.2.0
|
60
|
+
signing_key:
|
61
|
+
specification_version: 2
|
62
|
+
summary: A Rails plugin ActiveResource extension for instance-level (instead of class-level) HTTP Basic Authentication
|
63
|
+
test_files:
|
64
|
+
- test/active_resource_instance_authentication_test.rb
|