jwagener-oauth-active-resource 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 jwagener
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.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ = oauth-active-resource
2
+
3
+ Description goes here.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 jwagener. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "oauth-active-resource"
8
+ gem.summary = %Q{TODO}
9
+ gem.email = "johannes@wagener.cc"
10
+ gem.homepage = "http://github.com/jwagener/oauth-active-resource"
11
+ gem.authors = ["jwagener"]
12
+
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17
+ end
18
+
19
+ require 'spec/rake/spectask'
20
+ Spec::Rake::SpecTask.new(:spec) do |spec|
21
+ spec.libs << 'lib' << 'spec'
22
+ spec.spec_files = FileList['spec/**/*_spec.rb']
23
+ end
24
+
25
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.pattern = 'spec/**/*_spec.rb'
28
+ spec.rcov = true
29
+ end
30
+
31
+
32
+ task :default => :spec
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ if File.exist?('VERSION.yml')
37
+ config = YAML.load(File.read('VERSION.yml'))
38
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
39
+ else
40
+ version = ""
41
+ end
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "oauth-active-resource #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
48
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 0
@@ -0,0 +1,28 @@
1
+ apimodule 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
@@ -0,0 +1,26 @@
1
+ module OAuthActiveResource
2
+
3
+ class Connection < ActiveResource::Connection
4
+ def initialize(oauth_connection, *args)
5
+ @oauth_connection = oauth_connection
6
+ super(*args)
7
+ end
8
+
9
+ # an alternative for the get method, which doesnt tries to decode the response
10
+ def get_without_decoding(path, headers = {})
11
+ request(:get, path, build_request_headers(headers, :get))
12
+ end
13
+
14
+ private
15
+ def request(method, path, *arguments)
16
+ if @oauth_connection == nil
17
+ super(method, path, *arguments)
18
+ else
19
+ result = @oauth_connection.send(method, "#{site.scheme}://#{site.host}:#{site.port}#{path}", *arguments)
20
+ handle_response(result)
21
+ end
22
+ rescue Timeout::Error => e
23
+ raise TimeoutError.new(e.message)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,87 @@
1
+ require 'rubygems'
2
+ gem 'multipart'
3
+ require 'multipart'
4
+
5
+ require 'uri'
6
+
7
+ module OAuthActiveResource
8
+ class Resource < ActiveResource::Base
9
+ @oauth_connection = nil # Defaults to be anonymous
10
+
11
+ class << self
12
+ attr_accessor :oauth_connection
13
+ end
14
+
15
+ def self.connection(refresh = false)
16
+ @connection = Connection.new(oauth_connection, site,format) if @connection.nil? || refresh
17
+ @connection.timeout = timeout if timeout
18
+ return @connection
19
+ end
20
+
21
+ #TODO remove when soundcloud api is fixed
22
+ # if self has no id, try extracting from uri
23
+ def load(*args)
24
+ super(*args)
25
+ self.id = self.uri.split('/').last if self.id.nil? and defined? self.uri
26
+ end
27
+
28
+ # has_many allows resources with sub-resources which arent nested to be accessable.
29
+ #
30
+ # Example:
31
+ # User 123 (http://example.com/users/123/) has many friends
32
+ # The list of friends can be accessed by http://example.com/users/123/friends
33
+ # Our class definition:
34
+ #
35
+ # class User < Resource
36
+ # has_many :friends
37
+ # end
38
+ #
39
+ # user = User.find(123)
40
+ # user.friends.each do |friend|
41
+ # p friend.name
42
+ # end
43
+ #
44
+ # # adding a friend
45
+ # stranger = User.find(987)
46
+ # user.friends << stranger
47
+ # user.friends.save
48
+ # => sends a PUT with collection of friends to http://example.com/users/123/friends
49
+
50
+ def self.has_many(*args)
51
+ args.each do |k|
52
+ name = k.to_s
53
+ singular = name.singularize
54
+ define_method(k) do
55
+ if @has_many_cache.nil?
56
+ @has_many_cache = {}
57
+ end
58
+ if not @has_many_cache[name]
59
+ uri = "/#{self.element_name.pluralize}/#{self.id}/#{name}"
60
+ resource = find_or_create_resource_for(singular)
61
+ @has_many_cache[name] = OAuthActiveResource::Collection.new(self.connection,resource,uri)
62
+ end
63
+ return @has_many_cache[name]
64
+ end
65
+ end
66
+ end
67
+
68
+
69
+ # allows you to POST/PUT an oauth authenticated multipart request
70
+ def self.send_multipart_request(method,path,file_param_name,file,params={})
71
+ req = Net::HTTP::Post.new(path)
72
+ if method == :put
73
+ params[:_method] = "PUT"
74
+ end
75
+ post_file = Net::HTTP::FileForPost.new(file)
76
+ req.set_multipart_data({file_param_name => post_file},params)
77
+
78
+ oauth_connection.sign!(req)
79
+ uri = URI.parse oauth_connection.consumer.site
80
+
81
+ res = Net::HTTP.new(uri.host,uri.port).start do |http|
82
+ http.request(req)
83
+ end
84
+ res
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,48 @@
1
+ require 'activeresource'
2
+ require 'digest/md5'
3
+
4
+ module OAuthActiveResource
5
+
6
+ # TODO check if klass has ancestor OAuthActiveResource
7
+ def self.register(add_to_module, model_module, options = {})
8
+
9
+ oauth_connection = options[:access_token]
10
+ site = options[:site]
11
+
12
+ mod = Module.new do
13
+ model_module.constants.each do |klass|
14
+ # TODO check if klass.is_a OAuthActiveResource
15
+ sub = Class.new(model_module.const_get(klass)) do
16
+ self.site = site
17
+ @oauth_connection = oauth_connection
18
+ end
19
+ const_set(klass, sub)
20
+ end
21
+
22
+ def self.method_missing(name,*args)
23
+ self.const_get(name)
24
+ rescue
25
+ super(name,*args)
26
+ end
27
+
28
+ end
29
+
30
+ # Obscure (=Hash) token+secret, b/c it should stay one
31
+ if oauth_connection.nil?
32
+ dynamic_module_name = "AnonymousConsumer"
33
+ else
34
+ hash = Digest::MD5.hexdigest("#{oauth_connection.token}#{oauth_connection.secret}")
35
+ dynamic_module_name = "OAuthConsumer#{hash}"
36
+ end
37
+
38
+ add_to_module.const_set(dynamic_module_name, mod)
39
+ return mod
40
+ end
41
+
42
+ end
43
+
44
+
45
+ require 'oauth_active_resource/connection'
46
+ require 'oauth_active_resource/resource'
47
+ require 'oauth_active_resource/collection'
48
+
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe "OauthActiveResource" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec'
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require 'oauth_active_resource'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jwagener-oauth-active-resource
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - jwagener
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-13 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: johannes@wagener.cc
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - LICENSE
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION.yml
30
+ - lib/oauth_active_resource.rb
31
+ - lib/oauth_active_resource/connection.rb
32
+ - lib/oauth_active_resource/resource.rb
33
+ - lib/oauth_active_resource/collection.rb
34
+ - spec/oauth_active_resource_spec.rb
35
+ - spec/spec_helper.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/jwagener/oauth-active-resource
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --charset=UTF-8
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: TODO
62
+ test_files:
63
+ - spec/oauth_active_resource_spec.rb
64
+ - spec/spec_helper.rb