jwagener-oauth-active-resource 0.1.1 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +0 -4
- data/VERSION.yml +1 -1
- data/lib/oauth_active_resource.rb +17 -3
- data/lib/oauth_active_resource/connection.rb +7 -2
- data/lib/oauth_active_resource/fake_oauth_access_token.rb +55 -0
- data/lib/oauth_active_resource/resource.rb +37 -10
- data/lib/oauth_active_resource/unique_resource_array.rb +86 -0
- data/spec/oauth_active_resource_spec.rb +19 -2
- data/spec/spec_helper.rb +21 -0
- metadata +4 -3
- data/lib/oauth_active_resource/collection.rb +0 -28
data/Rakefile
CHANGED
@@ -12,10 +12,6 @@ begin
|
|
12
12
|
gem.add_dependency "oauth"
|
13
13
|
gem.add_dependency "activeresource"
|
14
14
|
gem.add_dependency "multipart"
|
15
|
-
|
16
|
-
# gem.add_dependency "uri"
|
17
|
-
|
18
|
-
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
19
15
|
end
|
20
16
|
rescue LoadError
|
21
17
|
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
data/VERSION.yml
CHANGED
@@ -11,6 +11,11 @@ module OAuthActiveResource
|
|
11
11
|
def self.register(add_to_module, model_module, options = {})
|
12
12
|
|
13
13
|
oauth_connection = options[:access_token]
|
14
|
+
|
15
|
+
if oauth_connection.nil?
|
16
|
+
oauth_connection = FakeOAuthAccessToken.new
|
17
|
+
end
|
18
|
+
|
14
19
|
site = options[:site]
|
15
20
|
|
16
21
|
mod = Module.new do
|
@@ -29,6 +34,10 @@ module OAuthActiveResource
|
|
29
34
|
super(name,*args)
|
30
35
|
end
|
31
36
|
|
37
|
+
def self.destroy
|
38
|
+
name = self.model_name.split('::').last
|
39
|
+
p self.parent.send :remove_const, name
|
40
|
+
end
|
32
41
|
end
|
33
42
|
|
34
43
|
# Obscure (=Hash) token+secret, b/c it should stay one
|
@@ -39,7 +48,12 @@ module OAuthActiveResource
|
|
39
48
|
dynamic_module_name = "OAuthConsumer#{hash}"
|
40
49
|
end
|
41
50
|
|
42
|
-
add_to_module.
|
51
|
+
if add_to_module.const_defined? dynamic_module_name
|
52
|
+
mod = add_to_module.const_get dynamic_module_name
|
53
|
+
else
|
54
|
+
add_to_module.const_set(dynamic_module_name, mod)
|
55
|
+
end
|
56
|
+
|
43
57
|
return mod
|
44
58
|
end
|
45
59
|
|
@@ -48,5 +62,5 @@ end
|
|
48
62
|
|
49
63
|
require 'oauth_active_resource/connection'
|
50
64
|
require 'oauth_active_resource/resource'
|
51
|
-
require 'oauth_active_resource/
|
52
|
-
|
65
|
+
require 'oauth_active_resource/unique_resource_array'
|
66
|
+
require 'oauth_active_resource/fake_oauth_access_token'
|
@@ -11,13 +11,18 @@ module OAuthActiveResource
|
|
11
11
|
request(:get, path, build_request_headers(headers, :get))
|
12
12
|
end
|
13
13
|
|
14
|
+
# make handle_response public
|
15
|
+
def handle_response(*args)
|
16
|
+
super(*args)
|
17
|
+
end
|
18
|
+
|
14
19
|
private
|
15
20
|
def request(method, path, *arguments)
|
16
21
|
if @oauth_connection == nil
|
17
22
|
super(method, path, *arguments)
|
18
23
|
else
|
19
|
-
|
20
|
-
handle_response(
|
24
|
+
response = @oauth_connection.request(method, "#{site.scheme}://#{site.host}:#{site.port}#{path}", *arguments)
|
25
|
+
handle_response(response)
|
21
26
|
end
|
22
27
|
rescue Timeout::Error => e
|
23
28
|
raise TimeoutError.new(e.message)
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module OAuthActiveResource
|
2
|
+
|
3
|
+
# just simulates the request and sign! methods of a oauth access token
|
4
|
+
class FakeOAuthAccessToken < OAuth::Consumer
|
5
|
+
|
6
|
+
attr_accessor :token, :secret
|
7
|
+
def initialize()
|
8
|
+
@key = nil
|
9
|
+
token = 'Anonymous'
|
10
|
+
secret = 'Anonymous'
|
11
|
+
|
12
|
+
# ensure that keys are symbols
|
13
|
+
@options = @@default_options
|
14
|
+
end
|
15
|
+
|
16
|
+
def request(http_method, path, token = nil, request_options = {}, *arguments)
|
17
|
+
if path !~ /^\//
|
18
|
+
@http = create_http(path)
|
19
|
+
_uri = URI.parse(path)
|
20
|
+
path = "#{_uri.path}#{_uri.query ? "?#{_uri.query}" : ""}"
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
rsp = http.request(create_http_request(http_method, path, token, request_options, *arguments))
|
25
|
+
|
26
|
+
rsp
|
27
|
+
end
|
28
|
+
|
29
|
+
def get(path, headers = {})
|
30
|
+
request(:get, path, headers)
|
31
|
+
end
|
32
|
+
|
33
|
+
def head(path, headers = {})
|
34
|
+
request(:head, path, headers)
|
35
|
+
end
|
36
|
+
|
37
|
+
def post(path, body = '', headers = {})
|
38
|
+
request(:post, path, body, headers)
|
39
|
+
end
|
40
|
+
|
41
|
+
def put(path, body = '', headers = {})
|
42
|
+
request(:put, path, body, headers)
|
43
|
+
end
|
44
|
+
|
45
|
+
def delete(path, headers = {})
|
46
|
+
request(:delete, path, headers)
|
47
|
+
end
|
48
|
+
|
49
|
+
def sign!(*args)
|
50
|
+
# do nothing
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
@@ -21,7 +21,37 @@ module OAuthActiveResource
|
|
21
21
|
super(*args)
|
22
22
|
self.id = self.uri.split('/').last if self.id.nil? and defined? self.uri
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
|
+
def ==(other)
|
26
|
+
return (other.is_a? Resource) && (self.is_a? Resource) && (self.element_name == other.element_name) && (self.id == other.id)
|
27
|
+
rescue
|
28
|
+
return super(other)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.load_collection(*args)
|
32
|
+
instantiate_collection(*args)
|
33
|
+
end
|
34
|
+
|
35
|
+
#
|
36
|
+
# belongs_to :user
|
37
|
+
# => will look for a user-id tag and load this user
|
38
|
+
#
|
39
|
+
def self.belongs_to(*args)
|
40
|
+
args.each do |k|
|
41
|
+
name = k.to_s
|
42
|
+
define_method(k) do
|
43
|
+
if @belongs_to_cache.nil?
|
44
|
+
@belongs_to_cache = {}
|
45
|
+
end
|
46
|
+
if not @belongs_to_cache[name]
|
47
|
+
resource = find_or_create_resource_for(name)
|
48
|
+
@belongs_to_cache[name] = resource.find(self.send("#{name}_id"))
|
49
|
+
end
|
50
|
+
return @belongs_to_cache[name]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
25
55
|
# has_many allows resources with sub-resources which arent nested to be accessable.
|
26
56
|
#
|
27
57
|
# Example:
|
@@ -53,16 +83,16 @@ module OAuthActiveResource
|
|
53
83
|
@has_many_cache = {}
|
54
84
|
end
|
55
85
|
if not @has_many_cache[name]
|
56
|
-
|
86
|
+
|
87
|
+
collection_path = "/#{self.element_name.pluralize}/#{self.id}/#{name}"
|
57
88
|
resource = find_or_create_resource_for(singular)
|
58
|
-
@has_many_cache[name] = OAuthActiveResource::
|
89
|
+
@has_many_cache[name] = OAuthActiveResource::UniqueResourceArray.new(self.connection,resource,collection_path)
|
59
90
|
end
|
60
91
|
return @has_many_cache[name]
|
61
92
|
end
|
62
93
|
end
|
63
94
|
end
|
64
95
|
|
65
|
-
|
66
96
|
# allows you to POST/PUT an oauth authenticated multipart request
|
67
97
|
def self.send_multipart_request(method,path,file_param_name,file,params={})
|
68
98
|
req = Net::HTTP::Post.new(path)
|
@@ -70,12 +100,9 @@ module OAuthActiveResource
|
|
70
100
|
params[:_method] = "PUT"
|
71
101
|
end
|
72
102
|
post_file = Net::HTTP::FileForPost.new(file)
|
73
|
-
req.set_multipart_data({file_param_name => post_file},params)
|
74
|
-
|
75
|
-
|
76
|
-
uri = URI.parse oauth_connection.consumer.site
|
77
|
-
|
78
|
-
res = Net::HTTP.new(uri.host,uri.port).start do |http|
|
103
|
+
req.set_multipart_data({file_param_name => post_file},params)
|
104
|
+
oauth_connection.sign!(req) if not oauth_connection.nil?
|
105
|
+
res = Net::HTTP.new(site.host, site.port).start do |http|
|
79
106
|
http.request(req)
|
80
107
|
end
|
81
108
|
res
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
module OAuthActiveResource
|
4
|
+
class UniqueArray < Array
|
5
|
+
def initialize(*args)
|
6
|
+
if args.size == 1 and args[0].is_a? Array then
|
7
|
+
super(args[0].uniq)
|
8
|
+
else
|
9
|
+
super(*args)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def insert(i, v)
|
14
|
+
super(i, v) unless include?(v)
|
15
|
+
end
|
16
|
+
|
17
|
+
def <<(v)
|
18
|
+
super(v) unless include?(v)
|
19
|
+
end
|
20
|
+
|
21
|
+
def []=(*args)
|
22
|
+
# note: could just call super(*args) then uniq!, but this is faster
|
23
|
+
|
24
|
+
# there are three different versions of this call:
|
25
|
+
# 1. start, length, value
|
26
|
+
# 2. index, value
|
27
|
+
# 3. range, value
|
28
|
+
# We just need to get the value
|
29
|
+
v = case args.size
|
30
|
+
when 3 then args[2]
|
31
|
+
when 2 then args[1]
|
32
|
+
else nil
|
33
|
+
end
|
34
|
+
|
35
|
+
super(*args) if v.nil? or not include?(v)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# see has_many in Resource
|
40
|
+
class UniqueResourceArray < UniqueArray
|
41
|
+
def initialize(connection, resource, collection_path)
|
42
|
+
super()
|
43
|
+
|
44
|
+
@connection = connection
|
45
|
+
@collection_path = collection_path
|
46
|
+
@resource = resource
|
47
|
+
reload
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_json
|
51
|
+
return "[ #{self.map { |obj| obj.to_json }.join(',')} ]"
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_xml
|
55
|
+
# or use __method__ here?
|
56
|
+
pt = @resource.element_name.pluralize
|
57
|
+
return "<#{pt}> #{self.map { |obj| obj.to_xml({:skip_instruct => true})}.join(' ')} </#{pt}>"
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
# DEPRECATED...
|
62
|
+
#def find(look_for)
|
63
|
+
# if not (look_for.is_a? String or look_for.is_a? Integer)
|
64
|
+
# look_for_id = look_for
|
65
|
+
# else
|
66
|
+
# look_for_id = look_for.id
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
# self.each do |obj|
|
70
|
+
# obj.id == look_for_id and return obj
|
71
|
+
# end
|
72
|
+
# return nil
|
73
|
+
#end
|
74
|
+
|
75
|
+
def save
|
76
|
+
response = @connection.handle_response( @connection.put("#{@collection_path}",self.to_xml) )
|
77
|
+
self.replace( @resource.load_collection( @connection.format.decode(response.body) ) )
|
78
|
+
end
|
79
|
+
|
80
|
+
def reload
|
81
|
+
self.replace(@resource.find(:all, :from => @collection_path))
|
82
|
+
return self
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
@@ -1,7 +1,24 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
gem 'oauth'
|
4
|
+
require 'oauth'
|
5
|
+
|
1
6
|
require 'spec_helper'
|
2
7
|
|
3
8
|
describe "OauthActiveResource" do
|
4
|
-
|
5
|
-
|
9
|
+
|
10
|
+
it "should register a new Module fork" do
|
11
|
+
cl = TestClient.register
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should destroy it self" do
|
15
|
+
consumer_token = OAuth::ConsumerToken.new('test123','test123')
|
16
|
+
access_token = OAuth::AccessToken.new(consumer_token, 'access_token', 'access_secret')
|
17
|
+
|
18
|
+
old_count = TestClient.constants.count
|
19
|
+
sc = TestClient.register({:access_token => access_token })
|
20
|
+
TestClient.constants.count.should be (old_count+1)
|
21
|
+
sc.destroy
|
22
|
+
TestClient.constants.count.should be old_count
|
6
23
|
end
|
7
24
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,8 +2,29 @@ require 'spec'
|
|
2
2
|
|
3
3
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
4
4
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
|
8
|
+
gem 'oauth'
|
9
|
+
require 'oauth'
|
10
|
+
|
11
|
+
|
5
12
|
require 'oauth_active_resource'
|
6
13
|
|
14
|
+
|
15
|
+
|
7
16
|
Spec::Runner.configure do |config|
|
8
17
|
|
9
18
|
end
|
19
|
+
|
20
|
+
# Test Modul
|
21
|
+
module TestClient
|
22
|
+
def self.register(options = {})
|
23
|
+
OAuthActiveResource.register(self.ancestors.first, self.ancestors.first.const_get('Models'), options)
|
24
|
+
end
|
25
|
+
module Models
|
26
|
+
class XZ < OAuthActiveResource::Resource
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jwagener-oauth-active-resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Johannes Wagener
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-05-
|
12
|
+
date: 2009-05-25 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -57,9 +57,10 @@ files:
|
|
57
57
|
- Rakefile
|
58
58
|
- VERSION.yml
|
59
59
|
- lib/oauth_active_resource.rb
|
60
|
-
- lib/oauth_active_resource/collection.rb
|
61
60
|
- lib/oauth_active_resource/connection.rb
|
61
|
+
- lib/oauth_active_resource/fake_oauth_access_token.rb
|
62
62
|
- lib/oauth_active_resource/resource.rb
|
63
|
+
- lib/oauth_active_resource/unique_resource_array.rb
|
63
64
|
- spec/oauth_active_resource_spec.rb
|
64
65
|
- spec/spec_helper.rb
|
65
66
|
has_rdoc: true
|
@@ -1,28 +0,0 @@
|
|
1
|
-
module OAuthActiveResource
|
2
|
-
# see has_many in Resource
|
3
|
-
class Collection < Set
|
4
|
-
def initialize(connection, resource, collection_uri)
|
5
|
-
super()
|
6
|
-
@connection = connection
|
7
|
-
@collection_uri = collection_uri
|
8
|
-
@resource = resource
|
9
|
-
reload
|
10
|
-
end
|
11
|
-
|
12
|
-
def to_json
|
13
|
-
return "[ #{self.map { |obj| obj.to_json }.join(',')} ]"
|
14
|
-
end
|
15
|
-
|
16
|
-
def to_xml
|
17
|
-
raise "NotImplemented"
|
18
|
-
end
|
19
|
-
|
20
|
-
def save
|
21
|
-
@connection.put("#{@resource.class.site}#{@collection_uri}",self.to_json,{ 'Accept'=>'application/json', 'Content-Type' => 'application/json' })
|
22
|
-
end
|
23
|
-
|
24
|
-
def reload
|
25
|
-
self.replace(@resource.find(:all, :from => @collection_uri))
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|