oauth-active-resource 0.4.4 → 0.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +6 -6
- data/VERSION.yml +1 -1
- data/lib/oauth_active_resource.rb +14 -15
- data/lib/oauth_active_resource/connection.rb +8 -9
- data/lib/oauth_active_resource/fake_oauth_access_token.rb +5 -12
- data/lib/oauth_active_resource/resource.rb +19 -19
- data/lib/oauth_active_resource/unique_resource_array.rb +14 -16
- data/oauth-active-resource.gemspec +6 -3
- metadata +3 -2
data/Rakefile
CHANGED
@@ -4,14 +4,14 @@ require 'rake'
|
|
4
4
|
begin
|
5
5
|
require 'jeweler'
|
6
6
|
Jeweler::Tasks.new do |gem|
|
7
|
-
gem.name
|
8
|
-
gem.summary
|
9
|
-
gem.email
|
7
|
+
gem.name = "oauth-active-resource"
|
8
|
+
gem.summary = "An OAuth enabled ActiveResource wrapper"
|
9
|
+
gem.email = "johannes@wagener.cc"
|
10
10
|
gem.homepage = "http://github.com/jwagener/oauth-active-resource"
|
11
|
-
gem.authors
|
11
|
+
gem.authors = [ "Johannes Wagener", "Hannes Tyden" ]
|
12
12
|
gem.add_dependency "oauth", ">= 0.3.6"
|
13
|
-
gem.add_dependency "activeresource"
|
14
|
-
gem.add_dependency "multipart"
|
13
|
+
gem.add_dependency "activeresource"
|
14
|
+
gem.add_dependency "multipart"
|
15
15
|
end
|
16
16
|
rescue LoadError
|
17
17
|
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
data/VERSION.yml
CHANGED
@@ -1,19 +1,20 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
|
3
3
|
gem 'activeresource'
|
4
|
-
require '
|
4
|
+
require 'active_resource'
|
5
|
+
|
6
|
+
gem 'oauth'
|
7
|
+
require 'oauth'
|
5
8
|
|
6
9
|
require 'digest/md5'
|
7
10
|
|
8
11
|
module OAuthActiveResource
|
9
|
-
|
10
12
|
# TODO check if klass has ancestor OAuthActiveResource
|
11
13
|
def self.register(add_to_module, model_module, options = {})
|
12
|
-
|
13
14
|
oauth_connection = options[:access_token]
|
14
15
|
|
15
16
|
if oauth_connection.nil?
|
16
|
-
oauth_connection = FakeOAuthAccessToken.new
|
17
|
+
oauth_connection = FakeOAuthAccessToken.new
|
17
18
|
end
|
18
19
|
|
19
20
|
site = options[:site]
|
@@ -22,7 +23,7 @@ module OAuthActiveResource
|
|
22
23
|
model_module.constants.each do |klass|
|
23
24
|
# TODO check if klass.is_a OAuthActiveResource
|
24
25
|
sub = Class.new(model_module.const_get(klass)) do
|
25
|
-
self.site = site
|
26
|
+
self.site = site
|
26
27
|
@oauth_connection = oauth_connection
|
27
28
|
end
|
28
29
|
const_set(klass, sub)
|
@@ -37,30 +38,28 @@ module OAuthActiveResource
|
|
37
38
|
def self.destroy
|
38
39
|
name = self.model_name.split('::').last
|
39
40
|
self.parent.send :remove_const, name
|
40
|
-
end
|
41
|
+
end
|
41
42
|
end
|
42
43
|
|
43
44
|
# Obscure (=Hash) token+secret, b/c it should stay one
|
44
45
|
if oauth_connection.nil?
|
45
46
|
dynamic_module_name = "AnonymousConsumer"
|
46
47
|
else
|
47
|
-
hash = Digest::MD5.hexdigest("#{oauth_connection.token}#{oauth_connection.secret}")
|
48
|
+
hash = Digest::MD5.hexdigest("#{oauth_connection.token}#{oauth_connection.secret}")
|
48
49
|
dynamic_module_name = "OAuthConsumer#{hash}"
|
49
50
|
end
|
50
51
|
|
51
52
|
if add_to_module.const_defined? dynamic_module_name
|
52
|
-
mod = add_to_module.const_get dynamic_module_name
|
53
|
+
mod = add_to_module.const_get dynamic_module_name
|
53
54
|
else
|
54
|
-
add_to_module.const_set(dynamic_module_name, mod)
|
55
|
+
add_to_module.const_set(dynamic_module_name, mod)
|
55
56
|
end
|
56
57
|
|
57
58
|
return mod
|
58
59
|
end
|
59
|
-
|
60
60
|
end
|
61
61
|
|
62
|
-
|
63
|
-
require 'oauth_active_resource/
|
64
|
-
require 'oauth_active_resource/
|
65
|
-
require 'oauth_active_resource/
|
66
|
-
require 'oauth_active_resource/fake_oauth_access_token'
|
62
|
+
require File.expand_path('oauth_active_resource/connection', File.dirname(__FILE__))
|
63
|
+
require File.expand_path('oauth_active_resource/resource', File.dirname(__FILE__))
|
64
|
+
require File.expand_path('oauth_active_resource/unique_resource_array', File.dirname(__FILE__))
|
65
|
+
require File.expand_path('oauth_active_resource/fake_oauth_access_token', File.dirname(__FILE__))
|
@@ -1,7 +1,6 @@
|
|
1
1
|
module OAuthActiveResource
|
2
|
-
|
3
2
|
class Connection < ActiveResource::Connection
|
4
|
-
def initialize(oauth_connection, *args)
|
3
|
+
def initialize(oauth_connection, *args)
|
5
4
|
@oauth_connection = oauth_connection
|
6
5
|
super(*args)
|
7
6
|
end
|
@@ -19,25 +18,25 @@ module OAuthActiveResource
|
|
19
18
|
# ugly code to insert the error_message into response
|
20
19
|
error_message = "#{format.decode response.body}"
|
21
20
|
if not error_message.nil? or error_message == ""
|
22
|
-
exc.response.instance_eval do ||
|
21
|
+
exc.response.instance_eval do ||
|
23
22
|
@message = error_message
|
24
23
|
end
|
25
|
-
end
|
24
|
+
end
|
26
25
|
ensure
|
27
26
|
raise exc
|
28
27
|
end
|
29
28
|
end
|
30
29
|
|
31
|
-
|
32
|
-
def request(method, path, *arguments)
|
30
|
+
private
|
31
|
+
def request(method, path, *arguments)
|
33
32
|
if @oauth_connection == nil
|
34
33
|
super(method, path, *arguments)
|
35
34
|
else
|
36
|
-
response = @oauth_connection.request(method, "#{site.scheme}://#{site.host}:#{site.port}#{path}", *arguments)
|
35
|
+
response = @oauth_connection.request(method, "#{site.scheme}://#{site.host}:#{site.port}#{path}", *arguments)
|
37
36
|
handle_response(response)
|
38
37
|
end
|
39
|
-
rescue Timeout::Error => e
|
38
|
+
rescue Timeout::Error => e
|
40
39
|
raise TimeoutError.new(e.message)
|
41
|
-
end
|
40
|
+
end
|
42
41
|
end
|
43
42
|
end
|
@@ -1,16 +1,14 @@
|
|
1
1
|
module OAuthActiveResource
|
2
|
-
|
3
2
|
# just simulates the request and sign! methods of a oauth access token
|
4
|
-
class FakeOAuthAccessToken < OAuth::Consumer
|
5
|
-
|
3
|
+
class FakeOAuthAccessToken < ::OAuth::Consumer
|
6
4
|
attr_accessor :token, :secret
|
7
5
|
def initialize()
|
8
6
|
@key = nil
|
9
|
-
token
|
10
|
-
secret
|
7
|
+
token = 'Anonymous'
|
8
|
+
secret = 'Anonymous'
|
11
9
|
|
12
10
|
# ensure that keys are symbols
|
13
|
-
@options = @@default_options
|
11
|
+
@options = @@default_options
|
14
12
|
end
|
15
13
|
|
16
14
|
def request(http_method, path, token = nil, request_options = {}, *arguments)
|
@@ -18,12 +16,9 @@ module OAuthActiveResource
|
|
18
16
|
@http = create_http(path)
|
19
17
|
_uri = URI.parse(path)
|
20
18
|
path = "#{_uri.path}#{_uri.query ? "?#{_uri.query}" : ""}"
|
21
|
-
|
22
19
|
end
|
23
20
|
|
24
|
-
|
25
|
-
|
26
|
-
rsp
|
21
|
+
http.request(create_http_request(http_method, path, token, request_options, *arguments))
|
27
22
|
end
|
28
23
|
|
29
24
|
def get(path, headers = {})
|
@@ -49,7 +44,5 @@ module OAuthActiveResource
|
|
49
44
|
def sign!(*args)
|
50
45
|
# do nothing
|
51
46
|
end
|
52
|
-
|
53
47
|
end
|
54
48
|
end
|
55
|
-
|
@@ -11,14 +11,14 @@ module OAuthActiveResource
|
|
11
11
|
|
12
12
|
def self.connection(refresh = false)
|
13
13
|
@connection = Connection.new(oauth_connection, site,format) if @connection.nil? || refresh
|
14
|
-
@connection.timeout = timeout if timeout
|
15
|
-
return @connection
|
14
|
+
@connection.timeout = timeout if timeout
|
15
|
+
return @connection
|
16
16
|
end
|
17
17
|
|
18
18
|
#TODO remove when soundcloud api is fixed
|
19
19
|
# if self has no id, try extracting from uri
|
20
20
|
def load(*args)
|
21
|
-
super(*args)
|
21
|
+
super(*args)
|
22
22
|
self.id = self.uri.split('/').last if self.id.nil? and defined? self.uri
|
23
23
|
end
|
24
24
|
|
@@ -30,16 +30,16 @@ module OAuthActiveResource
|
|
30
30
|
|
31
31
|
def self.load_collection(*args)
|
32
32
|
instantiate_collection(*args)
|
33
|
-
end
|
33
|
+
end
|
34
34
|
|
35
35
|
#
|
36
36
|
# belongs_to :user
|
37
37
|
# => will look for a user-id tag and load this user
|
38
|
-
#
|
38
|
+
#
|
39
39
|
def self.belongs_to(*args)
|
40
|
-
args.each do |k|
|
40
|
+
args.each do |k|
|
41
41
|
name = k.to_s
|
42
|
-
define_method(k) do
|
42
|
+
define_method(k) do
|
43
43
|
if @belongs_to_cache.nil?
|
44
44
|
@belongs_to_cache = {}
|
45
45
|
end
|
@@ -47,7 +47,7 @@ module OAuthActiveResource
|
|
47
47
|
resource = find_or_create_resource_for(name)
|
48
48
|
@belongs_to_cache[name] = resource.find(self.send("#{name}_id"))
|
49
49
|
end
|
50
|
-
return @belongs_to_cache[name]
|
50
|
+
return @belongs_to_cache[name]
|
51
51
|
end
|
52
52
|
end
|
53
53
|
end
|
@@ -58,29 +58,29 @@ module OAuthActiveResource
|
|
58
58
|
# User 123 (http://example.com/users/123/) has many friends
|
59
59
|
# The list of friends can be accessed by http://example.com/users/123/friends
|
60
60
|
# Our class definition:
|
61
|
-
#
|
61
|
+
#
|
62
62
|
# class User < Resource
|
63
63
|
# has_many :friends
|
64
64
|
# end
|
65
|
-
#
|
65
|
+
#
|
66
66
|
# user = User.find(123)
|
67
67
|
# user.friends.each do |friend|
|
68
68
|
# p friend.name
|
69
69
|
# end
|
70
|
-
#
|
71
|
-
# # adding a friend
|
70
|
+
#
|
71
|
+
# # adding a friend
|
72
72
|
# stranger = User.find(987)
|
73
73
|
# user.friends << stranger
|
74
74
|
# user.friends.save
|
75
75
|
# => sends a PUT with collection of friends to http://example.com/users/123/friends ## OUTDATED!!?
|
76
76
|
|
77
77
|
def self.has_many(*args)
|
78
|
-
args.each do |k|
|
78
|
+
args.each do |k|
|
79
79
|
name = k.to_s
|
80
80
|
singular = name.singularize
|
81
|
-
define_method(k) do |*options|
|
81
|
+
define_method(k) do |*options|
|
82
82
|
|
83
|
-
options = options.first || {}
|
83
|
+
options = options.first || {}
|
84
84
|
#if @has_many_cache.nil?
|
85
85
|
# @has_many_cache = {}
|
86
86
|
#end
|
@@ -92,13 +92,13 @@ module OAuthActiveResource
|
|
92
92
|
resource = find_or_create_resource_for(singular)
|
93
93
|
@has_many_cache[cache_name] = OAuthActiveResource::UniqueResourceArray.new(self.connection,resource,collection_path,options)
|
94
94
|
end
|
95
|
-
return @has_many_cache[cache_name]
|
95
|
+
return @has_many_cache[cache_name]
|
96
96
|
end
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
100
100
|
|
101
|
-
# ignore is added because the multipart gem is adding an extra new line
|
101
|
+
# ignore is added because the multipart gem is adding an extra new line
|
102
102
|
# to the last parameter which will break parsing of track[sharing]
|
103
103
|
def self.multipart_bug_fix(params)
|
104
104
|
ordered_params = ActiveSupport::OrderedHash.new
|
@@ -122,9 +122,9 @@ module OAuthActiveResource
|
|
122
122
|
files.each do |k,v|
|
123
123
|
file_hash[k] = Net::HTTP::FileForPost.new(v)
|
124
124
|
end
|
125
|
-
req.set_multipart_data(file_hash, params)
|
125
|
+
req.set_multipart_data(file_hash, params)
|
126
126
|
|
127
|
-
oauth_connection.sign!(req) if not oauth_connection.nil?
|
127
|
+
oauth_connection.sign!(req) if not oauth_connection.nil?
|
128
128
|
res = Net::HTTP.new(site.host, site.port).start do |http|
|
129
129
|
http.request(req)
|
130
130
|
end
|
@@ -38,8 +38,8 @@ module OAuthActiveResource
|
|
38
38
|
|
39
39
|
# see has_many in Resource
|
40
40
|
class UniqueResourceArray < UniqueArray
|
41
|
-
def initialize(connection, resource, collection_path,options = {})
|
42
|
-
super()
|
41
|
+
def initialize(connection, resource, collection_path,options = {})
|
42
|
+
super()
|
43
43
|
|
44
44
|
@connection = connection
|
45
45
|
@collection_path = collection_path
|
@@ -58,20 +58,19 @@ module OAuthActiveResource
|
|
58
58
|
return "<#{pt}> #{self.map { |obj| obj.to_xml({:skip_instruct => true})}.join(' ')} </#{pt}>"
|
59
59
|
end
|
60
60
|
|
61
|
-
|
62
61
|
# DEPRECATED...
|
63
|
-
#def find(look_for)
|
64
|
-
#
|
65
|
-
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
#
|
69
|
-
#
|
70
|
-
#
|
71
|
-
#
|
72
|
-
#
|
73
|
-
#
|
74
|
-
#end
|
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
|
75
74
|
|
76
75
|
def save
|
77
76
|
response = @connection.handle_response( @connection.put("#{@collection_path}",self.to_xml) )
|
@@ -83,5 +82,4 @@ module OAuthActiveResource
|
|
83
82
|
return self
|
84
83
|
end
|
85
84
|
end
|
86
|
-
|
87
85
|
end
|
@@ -1,12 +1,15 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
1
4
|
# -*- encoding: utf-8 -*-
|
2
5
|
|
3
6
|
Gem::Specification.new do |s|
|
4
7
|
s.name = %q{oauth-active-resource}
|
5
|
-
s.version = "0.4.
|
8
|
+
s.version = "0.4.5"
|
6
9
|
|
7
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
-
s.authors = ["Johannes Wagener"]
|
9
|
-
s.date = %q{
|
11
|
+
s.authors = ["Johannes Wagener", "Hannes Tyden"]
|
12
|
+
s.date = %q{2010-05-26}
|
10
13
|
s.email = %q{johannes@wagener.cc}
|
11
14
|
s.extra_rdoc_files = [
|
12
15
|
"LICENSE",
|
metadata
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: oauth-active-resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Johannes Wagener
|
8
|
+
- Hannes Tyden
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
12
|
|
12
|
-
date:
|
13
|
+
date: 2010-05-26 00:00:00 +02:00
|
13
14
|
default_executable:
|
14
15
|
dependencies:
|
15
16
|
- !ruby/object:Gem::Dependency
|