oare 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .rvmrc
2
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'http://rubygems.org'
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,25 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ oare (0.1.0)
5
+ activeresource (~> 3.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ activemodel (3.0.7)
11
+ activesupport (= 3.0.7)
12
+ builder (~> 2.1.2)
13
+ i18n (~> 0.5.0)
14
+ activeresource (3.0.7)
15
+ activemodel (= 3.0.7)
16
+ activesupport (= 3.0.7)
17
+ activesupport (3.0.7)
18
+ builder (2.1.2)
19
+ i18n (0.5.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ oare!
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Nelvin Driz, Marjun Pagalan
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,8 @@
1
+ Oauth Active Resource Extension
2
+ ===
3
+
4
+ Example
5
+ =======
6
+
7
+
8
+ Copyright (c) 2011 Nelvin Driz, Marjun Pagalan, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'rake/gempackagetask'
5
+ require 'rake/testtask'
6
+ require 'rake/rdoctask'
7
+
8
+
9
+ desc 'Default: run unit tests.'
10
+ task :default => :test
11
+
12
+ desc 'Test the ore plugin.'
13
+ Rake::TestTask.new(:test) do |t|
14
+ t.libs << 'lib'
15
+ t.libs << 'test'
16
+ t.pattern = 'test/**/*_test.rb'
17
+ t.verbose = true
18
+ end
19
+
20
+ desc 'Generate documentation for the ore plugin.'
21
+ Rake::RDocTask.new(:rdoc) do |rdoc|
22
+ rdoc.rdoc_dir = 'rdoc'
23
+ rdoc.title = 'Ore'
24
+ rdoc.options << '--line-numbers' << '--inline-source'
25
+ rdoc.rdoc_files.include('README')
26
+ rdoc.rdoc_files.include('lib/**/*.rb')
27
+ end
28
+
29
+ spec = Gem::Specification.load(Dir['*.gemspec'].first)
30
+ package = Rake::GemPackageTask.new(spec)
31
+ package.define
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'ore'
@@ -0,0 +1,79 @@
1
+ module Oare
2
+ module Resource
3
+ def has_many(association_id, options = {})
4
+ @associations ||= []
5
+ class_name = options[:class_name] || association_id.to_s.singularize.camelize
6
+ @associations << class_name.constantize
7
+ end
8
+
9
+ def connection(refresh = true)
10
+ @associations.each do |model_constant|
11
+ model_constant.access_token = self.access_token
12
+ end if @associations
13
+
14
+ @connection = Oare::Connection.new(self.access_token) if @connection.nil? || refresh
15
+ @connection.timeout = timeout if timeout
16
+ @connection
17
+ end
18
+
19
+ end
20
+
21
+ module Gateway
22
+
23
+ def method_missing(method, *args, &block)
24
+ collection = ::ActiveResource::Base.oauth_enabled_classes
25
+ if collection.keys.include? method
26
+ constant = collection[method].join('::').constantize
27
+ constant.access_token = self.access_token
28
+ #Rails.logger.info constant.access_token.inspect
29
+ #Rails.logger.info ::ActiveResource::Base.access_token.inspect
30
+ #sleep 20
31
+ constant
32
+ else
33
+ super(method, *args, &block)
34
+ end
35
+ end
36
+
37
+ def access_token
38
+ ## This method assumes that the Gateway Model has the fields/methods for:
39
+ # oauth_token : string,
40
+ # oauth_token_secret : string,
41
+ # consumer : consumer token
42
+ #
43
+
44
+ OAuth::AccessToken.new(consumer, oauth_token, oauth_token_secret)
45
+ end
46
+
47
+ end
48
+ end
49
+
50
+ # Monkey Patch Active Resource and Action Controller
51
+ class ActiveResource::Base
52
+ # --------------------------------------------------------------
53
+ # Class Methods
54
+ # --------------------------------------------------------------
55
+ cattr_accessor :oauth_enabled_classes
56
+
57
+ class << self
58
+ attr_accessor :access_token
59
+
60
+ def requires_oauth
61
+ # Extend ActiveResource with this Module
62
+ #
63
+ extend Oare::Resource
64
+ self.oauth_enabled_classes ||= {}
65
+ class_name = self.to_s.split('::')
66
+ key = class_name[-1].underscore.to_sym
67
+ unless self.oauth_enabled_classes.keys.include? key
68
+ self.oauth_enabled_classes.merge!(key => class_name)
69
+ end
70
+ end
71
+
72
+ def access_token=(token)
73
+ @access_token = token
74
+ self.site = token.consumer.site
75
+ end
76
+
77
+ end
78
+ end
79
+
@@ -0,0 +1,42 @@
1
+ class NilAccessToken < Exception; end
2
+
3
+ class Oare::Connection < ActiveResource::Connection
4
+ attr_reader :access_token
5
+
6
+ def initialize(token, format = ActiveResource::Formats::XmlFormat)
7
+ @access_token = token
8
+ @user = @password = nil
9
+ @uri_parser = URI.const_defined?(:Parser) ? URI::Parser.new : URI
10
+ self.site = access_token.consumer.site
11
+ self.format = format
12
+ end
13
+
14
+ def get_without_decoding(path, headers = {})
15
+ request(:get, path, build_request_headers(headers, :get))
16
+ end
17
+
18
+ def handle_response(response)
19
+ return super(response)
20
+ rescue ActiveResource::ClientError => exc
21
+ begin
22
+ error_message = "#{format.decode response.body}"
23
+ if not error_message.nil? or error_message == ""
24
+ exc.response.instance_eval do ||
25
+ @message = error_message
26
+ end
27
+ end
28
+ ensure
29
+ raise exc
30
+ end
31
+ end
32
+
33
+ private
34
+ def request(method, path, *arguments)
35
+ raise NilAccessToken if access_token.nil?
36
+ response = access_token.request(method, path, *arguments)
37
+ handle_response(response)
38
+ rescue Timeout::Error => e
39
+ raise TimeoutError.new(e.message)
40
+ end
41
+
42
+ end
data/lib/oare.rb ADDED
@@ -0,0 +1,7 @@
1
+ module Oare
2
+ end
3
+
4
+ if defined?(Rails) && Rails::VERSION::MAJOR == 3
5
+ require File.expand_path('active_resource_override/connection', File.dirname(__FILE__))
6
+ require File.expand_path('active_resource_override/base' , File.dirname(__FILE__))
7
+ end
data/oare.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # encouding: utf -8
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ ## Basic Information
6
+ #
7
+ s.name = 'oare'
8
+ s.version = '0.1.1'
9
+ s.platform = Gem::Platform::RUBY
10
+ s.required_ruby_version = '~> 1.9'
11
+ s.required_rubygems_version = '>= 1.3.6'
12
+ s.authors = ['Nelvin Driz', 'Marjun Pagalan']
13
+ s.email = ['ndriz@exist.com', 'mpagalan@exist.com']
14
+ s.licenses = ['MIT']
15
+ s.summary = 'Oauth Active Resource Extension'
16
+ s.description = %q{
17
+ Allows Oauth usage in Active Resource
18
+ }
19
+
20
+ ## Rdoc Settings
21
+ #
22
+ s.has_rdoc = true
23
+ s.extra_rdoc_files = 'README.rdoc'
24
+ s.rdoc_options = %w{--main README.rdoc}
25
+
26
+ ## External Name in RubyForge
27
+ #
28
+ s.rubyforge_project = 'oare'
29
+
30
+ ## Gem Files
31
+ #
32
+ s.files = `git ls-files`.split("\n")
33
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
34
+ s.require_paths = ['lib']
35
+
36
+ ## Dependenciew
37
+ #
38
+ s.add_dependency 'activeresource', '~> 3.0'
39
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oare
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.1
6
+ platform: ruby
7
+ authors:
8
+ - Nelvin Driz
9
+ - Marjun Pagalan
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2011-05-17 00:00:00 +08:00
15
+ default_executable:
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: activeresource
19
+ prerelease: false
20
+ requirement: &id001 !ruby/object:Gem::Requirement
21
+ none: false
22
+ requirements:
23
+ - - ~>
24
+ - !ruby/object:Gem::Version
25
+ version: "3.0"
26
+ type: :runtime
27
+ version_requirements: *id001
28
+ description: "\n Allows Oauth usage in Active Resource\n "
29
+ email:
30
+ - ndriz@exist.com
31
+ - mpagalan@exist.com
32
+ executables: []
33
+
34
+ extensions: []
35
+
36
+ extra_rdoc_files:
37
+ - README.rdoc
38
+ files:
39
+ - .gitignore
40
+ - Gemfile
41
+ - Gemfile.lock
42
+ - MIT-LICENSE
43
+ - README.rdoc
44
+ - Rakefile
45
+ - init.rb
46
+ - lib/active_resource_override/base.rb
47
+ - lib/active_resource_override/connection.rb
48
+ - lib/oare.rb
49
+ - oare.gemspec
50
+ has_rdoc: true
51
+ homepage:
52
+ licenses:
53
+ - MIT
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --main
57
+ - README.rdoc
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: "1.9"
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 1.3.6
72
+ requirements: []
73
+
74
+ rubyforge_project: oare
75
+ rubygems_version: 1.5.1
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Oauth Active Resource Extension
79
+ test_files: []
80
+