oauth-active-resource 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/.document +5 -0
  2. data/.gitignore +5 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +7 -0
  5. data/Rakefile +49 -0
  6. data/VERSION.yml +4 -0
  7. data/doc/classes/Collection.html +205 -0
  8. data/doc/classes/Collection.src/M000001.html +22 -0
  9. data/doc/classes/Collection.src/M000002.html +18 -0
  10. data/doc/classes/Collection.src/M000003.html +18 -0
  11. data/doc/classes/Collection.src/M000004.html +18 -0
  12. data/doc/classes/Collection.src/M000005.html +18 -0
  13. data/doc/classes/OAuthActiveResource.html +165 -0
  14. data/doc/classes/OAuthActiveResource.src/M000006.html +63 -0
  15. data/doc/classes/OAuthActiveResource.src/M000007.html +20 -0
  16. data/doc/classes/OAuthActiveResource/Connection.html +158 -0
  17. data/doc/classes/OAuthActiveResource/Connection.src/M000012.html +19 -0
  18. data/doc/classes/OAuthActiveResource/Connection.src/M000013.html +18 -0
  19. data/doc/classes/OAuthActiveResource/Resource.html +233 -0
  20. data/doc/classes/OAuthActiveResource/Resource.src/M000008.html +20 -0
  21. data/doc/classes/OAuthActiveResource/Resource.src/M000009.html +19 -0
  22. data/doc/classes/OAuthActiveResource/Resource.src/M000010.html +32 -0
  23. data/doc/classes/OAuthActiveResource/Resource.src/M000011.html +31 -0
  24. data/doc/created.rid +1 -0
  25. data/doc/files/lib/oauth_active_resource/collection_rb.html +101 -0
  26. data/doc/files/lib/oauth_active_resource/connection_rb.html +101 -0
  27. data/doc/files/lib/oauth_active_resource/resource_rb.html +110 -0
  28. data/doc/files/lib/oauth_active_resource_rb.html +112 -0
  29. data/doc/fr_class_index.html +30 -0
  30. data/doc/fr_file_index.html +30 -0
  31. data/doc/fr_method_index.html +39 -0
  32. data/doc/index.html +24 -0
  33. data/doc/rdoc-style.css +208 -0
  34. data/lib/oauth_active_resource.rb +66 -0
  35. data/lib/oauth_active_resource/connection.rb +43 -0
  36. data/lib/oauth_active_resource/fake_oauth_access_token.rb +55 -0
  37. data/lib/oauth_active_resource/resource.rb +119 -0
  38. data/lib/oauth_active_resource/unique_resource_array.rb +87 -0
  39. data/oauth-active-resource.gemspec +86 -0
  40. data/spec/oauth_active_resource_spec.rb +24 -0
  41. data/spec/spec_helper.rb +30 -0
  42. metadata +126 -0
@@ -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
+
@@ -0,0 +1,119 @@
1
+ require 'multipart'
2
+ require 'uri'
3
+
4
+ module OAuthActiveResource
5
+ class Resource < ActiveResource::Base
6
+ @oauth_connection = nil # Defaults to be anonymous
7
+
8
+ class << self
9
+ attr_accessor :oauth_connection
10
+ end
11
+
12
+ def self.connection(refresh = false)
13
+ @connection = Connection.new(oauth_connection, site,format) if @connection.nil? || refresh
14
+ @connection.timeout = timeout if timeout
15
+ return @connection
16
+ end
17
+
18
+ #TODO remove when soundcloud api is fixed
19
+ # if self has no id, try extracting from uri
20
+ def load(*args)
21
+ super(*args)
22
+ self.id = self.uri.split('/').last if self.id.nil? and defined? self.uri
23
+ end
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
+
55
+ # has_many allows resources with sub-resources which arent nested to be accessable.
56
+ #
57
+ # Example:
58
+ # User 123 (http://example.com/users/123/) has many friends
59
+ # The list of friends can be accessed by http://example.com/users/123/friends
60
+ # Our class definition:
61
+ #
62
+ # class User < Resource
63
+ # has_many :friends
64
+ # end
65
+ #
66
+ # user = User.find(123)
67
+ # user.friends.each do |friend|
68
+ # p friend.name
69
+ # end
70
+ #
71
+ # # adding a friend
72
+ # stranger = User.find(987)
73
+ # user.friends << stranger
74
+ # user.friends.save
75
+ # => sends a PUT with collection of friends to http://example.com/users/123/friends ## OUTDATED!!?
76
+
77
+ def self.has_many(*args)
78
+ args.each do |k|
79
+ name = k.to_s
80
+ singular = name.singularize
81
+ define_method(k) do |*options|
82
+
83
+ options = options.first || {}
84
+ #if @has_many_cache.nil?
85
+ # @has_many_cache = {}
86
+ #end
87
+ @has_many_cache ||= {}
88
+ cache_name = "#{name}#{options.hash}"
89
+ if not @has_many_cache[cache_name]
90
+
91
+ collection_path = "/#{self.element_name.pluralize}/#{self.id}/#{name}"
92
+ resource = find_or_create_resource_for(singular)
93
+ @has_many_cache[cache_name] = OAuthActiveResource::UniqueResourceArray.new(self.connection,resource,collection_path,options)
94
+ end
95
+ return @has_many_cache[cache_name]
96
+ end
97
+ end
98
+ end
99
+
100
+ # allows you to POST/PUT an oauth authenticated multipart request
101
+ def self.send_multipart_request(method,path,files,params={})
102
+ req = Net::HTTP::Post.new(path)
103
+ if method == :put
104
+ params[:_method] = "PUT"
105
+ end
106
+ file_hash = {}
107
+ files.each do |k,v|
108
+ file_hash[k] = Net::HTTP::FileForPost.new(v)
109
+ end
110
+ req.set_multipart_data(file_hash, params)
111
+
112
+ oauth_connection.sign!(req) if not oauth_connection.nil?
113
+ res = Net::HTTP.new(site.host, site.port).start do |http|
114
+ http.request(req)
115
+ end
116
+ res
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,87 @@
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,options = {})
42
+ super()
43
+
44
+ @connection = connection
45
+ @collection_path = collection_path
46
+ @resource = resource
47
+ @options = options
48
+ reload
49
+ end
50
+
51
+ def to_json
52
+ return "[ #{self.map { |obj| obj.to_json }.join(',')} ]"
53
+ end
54
+
55
+ def to_xml
56
+ # or use __method__ here?
57
+ pt = @resource.element_name.pluralize
58
+ return "<#{pt}> #{self.map { |obj| obj.to_xml({:skip_instruct => true})}.join(' ')} </#{pt}>"
59
+ end
60
+
61
+
62
+ # DEPRECATED...
63
+ #def find(look_for)
64
+ # if not (look_for.is_a? String or look_for.is_a? Integer)
65
+ # look_for_id = look_for
66
+ # else
67
+ # look_for_id = look_for.id
68
+ # end
69
+ #
70
+ # self.each do |obj|
71
+ # obj.id == look_for_id and return obj
72
+ # end
73
+ # return nil
74
+ #end
75
+
76
+ def save
77
+ response = @connection.handle_response( @connection.put("#{@collection_path}",self.to_xml) )
78
+ self.replace( @resource.load_collection( @connection.format.decode(response.body) ) )
79
+ end
80
+
81
+ def reload
82
+ self.replace(@resource.find(:all, :from => @collection_path, :params => @options))
83
+ return self
84
+ end
85
+ end
86
+
87
+ end
@@ -0,0 +1,86 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{oauth-active-resource}
5
+ s.version = "0.4.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Johannes Wagener"]
9
+ s.date = %q{2009-09-03}
10
+ s.email = %q{johannes@wagener.cc}
11
+ s.extra_rdoc_files = [
12
+ "LICENSE",
13
+ "README.rdoc"
14
+ ]
15
+ s.files = [
16
+ ".document",
17
+ ".gitignore",
18
+ "LICENSE",
19
+ "README.rdoc",
20
+ "Rakefile",
21
+ "VERSION.yml",
22
+ "doc/classes/Collection.html",
23
+ "doc/classes/Collection.src/M000001.html",
24
+ "doc/classes/Collection.src/M000002.html",
25
+ "doc/classes/Collection.src/M000003.html",
26
+ "doc/classes/Collection.src/M000004.html",
27
+ "doc/classes/Collection.src/M000005.html",
28
+ "doc/classes/OAuthActiveResource.html",
29
+ "doc/classes/OAuthActiveResource.src/M000006.html",
30
+ "doc/classes/OAuthActiveResource.src/M000007.html",
31
+ "doc/classes/OAuthActiveResource/Connection.html",
32
+ "doc/classes/OAuthActiveResource/Connection.src/M000012.html",
33
+ "doc/classes/OAuthActiveResource/Connection.src/M000013.html",
34
+ "doc/classes/OAuthActiveResource/Resource.html",
35
+ "doc/classes/OAuthActiveResource/Resource.src/M000008.html",
36
+ "doc/classes/OAuthActiveResource/Resource.src/M000009.html",
37
+ "doc/classes/OAuthActiveResource/Resource.src/M000010.html",
38
+ "doc/classes/OAuthActiveResource/Resource.src/M000011.html",
39
+ "doc/created.rid",
40
+ "doc/files/lib/oauth_active_resource/collection_rb.html",
41
+ "doc/files/lib/oauth_active_resource/connection_rb.html",
42
+ "doc/files/lib/oauth_active_resource/resource_rb.html",
43
+ "doc/files/lib/oauth_active_resource_rb.html",
44
+ "doc/fr_class_index.html",
45
+ "doc/fr_file_index.html",
46
+ "doc/fr_method_index.html",
47
+ "doc/index.html",
48
+ "doc/rdoc-style.css",
49
+ "lib/oauth_active_resource.rb",
50
+ "lib/oauth_active_resource/connection.rb",
51
+ "lib/oauth_active_resource/fake_oauth_access_token.rb",
52
+ "lib/oauth_active_resource/resource.rb",
53
+ "lib/oauth_active_resource/unique_resource_array.rb",
54
+ "oauth-active-resource.gemspec",
55
+ "spec/oauth_active_resource_spec.rb",
56
+ "spec/spec_helper.rb"
57
+ ]
58
+ s.homepage = %q{http://github.com/jwagener/oauth-active-resource}
59
+ s.rdoc_options = ["--charset=UTF-8"]
60
+ s.require_paths = ["lib"]
61
+ s.rubygems_version = %q{1.3.4}
62
+ s.summary = %q{An OAuth enabled ActiveResource wrapper}
63
+ s.test_files = [
64
+ "spec/oauth_active_resource_spec.rb",
65
+ "spec/spec_helper.rb"
66
+ ]
67
+
68
+ if s.respond_to? :specification_version then
69
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
70
+ s.specification_version = 3
71
+
72
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
73
+ s.add_runtime_dependency(%q<pelle-oauth>, [">= 0"])
74
+ s.add_runtime_dependency(%q<activeresource>, [">= 0"])
75
+ s.add_runtime_dependency(%q<multipart>, [">= 0"])
76
+ else
77
+ s.add_dependency(%q<pelle-oauth>, [">= 0"])
78
+ s.add_dependency(%q<activeresource>, [">= 0"])
79
+ s.add_dependency(%q<multipart>, [">= 0"])
80
+ end
81
+ else
82
+ s.add_dependency(%q<pelle-oauth>, [">= 0"])
83
+ s.add_dependency(%q<activeresource>, [">= 0"])
84
+ s.add_dependency(%q<multipart>, [">= 0"])
85
+ end
86
+ end
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+
3
+ gem 'pelle-oauth', '0.3.6'
4
+ require 'oauth'
5
+
6
+ require 'spec_helper'
7
+
8
+ describe "OauthActiveResource" do
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
23
+ end
24
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec'
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+
6
+ require 'rubygems'
7
+
8
+ gem 'oauth'
9
+ require 'oauth'
10
+
11
+
12
+ require 'oauth_active_resource'
13
+
14
+
15
+
16
+ Spec::Runner.configure do |config|
17
+
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 ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oauth-active-resource
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.2
5
+ platform: ruby
6
+ authors:
7
+ - Johannes Wagener
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-03 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: pelle-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"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activeresource
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: multipart
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ description:
46
+ email: johannes@wagener.cc
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - LICENSE
53
+ - README.rdoc
54
+ files:
55
+ - .document
56
+ - .gitignore
57
+ - LICENSE
58
+ - README.rdoc
59
+ - Rakefile
60
+ - VERSION.yml
61
+ - doc/classes/Collection.html
62
+ - doc/classes/Collection.src/M000001.html
63
+ - doc/classes/Collection.src/M000002.html
64
+ - doc/classes/Collection.src/M000003.html
65
+ - doc/classes/Collection.src/M000004.html
66
+ - doc/classes/Collection.src/M000005.html
67
+ - doc/classes/OAuthActiveResource.html
68
+ - doc/classes/OAuthActiveResource.src/M000006.html
69
+ - doc/classes/OAuthActiveResource.src/M000007.html
70
+ - doc/classes/OAuthActiveResource/Connection.html
71
+ - doc/classes/OAuthActiveResource/Connection.src/M000012.html
72
+ - doc/classes/OAuthActiveResource/Connection.src/M000013.html
73
+ - doc/classes/OAuthActiveResource/Resource.html
74
+ - doc/classes/OAuthActiveResource/Resource.src/M000008.html
75
+ - doc/classes/OAuthActiveResource/Resource.src/M000009.html
76
+ - doc/classes/OAuthActiveResource/Resource.src/M000010.html
77
+ - doc/classes/OAuthActiveResource/Resource.src/M000011.html
78
+ - doc/created.rid
79
+ - doc/files/lib/oauth_active_resource/collection_rb.html
80
+ - doc/files/lib/oauth_active_resource/connection_rb.html
81
+ - doc/files/lib/oauth_active_resource/resource_rb.html
82
+ - doc/files/lib/oauth_active_resource_rb.html
83
+ - doc/fr_class_index.html
84
+ - doc/fr_file_index.html
85
+ - doc/fr_method_index.html
86
+ - doc/index.html
87
+ - doc/rdoc-style.css
88
+ - lib/oauth_active_resource.rb
89
+ - lib/oauth_active_resource/connection.rb
90
+ - lib/oauth_active_resource/fake_oauth_access_token.rb
91
+ - lib/oauth_active_resource/resource.rb
92
+ - lib/oauth_active_resource/unique_resource_array.rb
93
+ - oauth-active-resource.gemspec
94
+ - spec/oauth_active_resource_spec.rb
95
+ - spec/spec_helper.rb
96
+ has_rdoc: true
97
+ homepage: http://github.com/jwagener/oauth-active-resource
98
+ licenses: []
99
+
100
+ post_install_message:
101
+ rdoc_options:
102
+ - --charset=UTF-8
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: "0"
110
+ version:
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: "0"
116
+ version:
117
+ requirements: []
118
+
119
+ rubyforge_project:
120
+ rubygems_version: 1.3.5
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: An OAuth enabled ActiveResource wrapper
124
+ test_files:
125
+ - spec/oauth_active_resource_spec.rb
126
+ - spec/spec_helper.rb