Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/jwagener/oauth-active-resource
Browse files Browse the repository at this point in the history
Conflicts:
	lib/oauth_active_resource/resource.rb
  • Loading branch information
Thor Kell committed Aug 5, 2010
2 parents 0232d69 + ae56979 commit b3705a8
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 79 deletions.
12 changes: 6 additions & 6 deletions Rakefile
Expand Up @@ -4,14 +4,14 @@ require 'rake'
begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "oauth-active-resource"
gem.summary = "An OAuth enabled ActiveResource wrapper"
gem.email = "johannes@wagener.cc"
gem.name = "oauth-active-resource"
gem.summary = "An OAuth enabled ActiveResource wrapper"
gem.email = "johannes@wagener.cc"
gem.homepage = "http://github.com/jwagener/oauth-active-resource"
gem.authors = ["Johannes Wagener"]
gem.authors = [ "Johannes Wagener", "Hannes Tyden" ]
gem.add_dependency "oauth", ">= 0.3.6"
gem.add_dependency "activeresource"
gem.add_dependency "multipart"
gem.add_dependency "activeresource"
gem.add_dependency "multipart"
end
rescue LoadError
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
Expand Down
2 changes: 1 addition & 1 deletion VERSION.yml
@@ -1,4 +1,4 @@
---
:major: 0
:minor: 4
:patch: 4
:patch: 5
29 changes: 14 additions & 15 deletions lib/oauth_active_resource.rb
@@ -1,19 +1,20 @@
require 'rubygems'

gem 'activeresource'
require 'activeresource'
require 'active_resource'

gem 'oauth'
require 'oauth'

require 'digest/md5'

module OAuthActiveResource

# TODO check if klass has ancestor OAuthActiveResource
def self.register(add_to_module, model_module, options = {})

oauth_connection = options[:access_token]

if oauth_connection.nil?
oauth_connection = FakeOAuthAccessToken.new
oauth_connection = FakeOAuthAccessToken.new
end

site = options[:site]
Expand All @@ -22,7 +23,7 @@ def self.register(add_to_module, model_module, options = {})
model_module.constants.each do |klass|
# TODO check if klass.is_a OAuthActiveResource
sub = Class.new(model_module.const_get(klass)) do
self.site = site
self.site = site
@oauth_connection = oauth_connection
end
const_set(klass, sub)
Expand All @@ -37,30 +38,28 @@ def self.method_missing(name,*args)
def self.destroy
name = self.model_name.split('::').last
self.parent.send :remove_const, name
end
end
end

# Obscure (=Hash) token+secret, b/c it should stay one
if oauth_connection.nil?
dynamic_module_name = "AnonymousConsumer"
else
hash = Digest::MD5.hexdigest("#{oauth_connection.token}#{oauth_connection.secret}")
hash = Digest::MD5.hexdigest("#{oauth_connection.token}#{oauth_connection.secret}")
dynamic_module_name = "OAuthConsumer#{hash}"
end

if add_to_module.const_defined? dynamic_module_name
mod = add_to_module.const_get dynamic_module_name
mod = add_to_module.const_get dynamic_module_name
else
add_to_module.const_set(dynamic_module_name, mod)
add_to_module.const_set(dynamic_module_name, mod)
end

return mod
end

end


require 'oauth_active_resource/connection'
require 'oauth_active_resource/resource'
require 'oauth_active_resource/unique_resource_array'
require 'oauth_active_resource/fake_oauth_access_token'
require File.expand_path('oauth_active_resource/connection', File.dirname(__FILE__))
require File.expand_path('oauth_active_resource/resource', File.dirname(__FILE__))
require File.expand_path('oauth_active_resource/unique_resource_array', File.dirname(__FILE__))
require File.expand_path('oauth_active_resource/fake_oauth_access_token', File.dirname(__FILE__))
17 changes: 8 additions & 9 deletions lib/oauth_active_resource/connection.rb
@@ -1,7 +1,6 @@
module OAuthActiveResource

class Connection < ActiveResource::Connection
def initialize(oauth_connection, *args)
def initialize(oauth_connection, *args)
@oauth_connection = oauth_connection
super(*args)
end
Expand All @@ -19,25 +18,25 @@ def handle_response(response)
# ugly code to insert the error_message into response
error_message = "#{format.decode response.body}"
if not error_message.nil? or error_message == ""
exc.response.instance_eval do ||
exc.response.instance_eval do ||
@message = error_message
end
end
end
ensure
raise exc
end
end

private
def request(method, path, *arguments)
private
def request(method, path, *arguments)
if @oauth_connection == nil
super(method, path, *arguments)
else
response = @oauth_connection.request(method, "#{site.scheme}://#{site.host}:#{site.port}#{path}", *arguments)
response = @oauth_connection.request(method, "#{site.scheme}://#{site.host}:#{site.port}#{path}", *arguments)
handle_response(response)
end
rescue Timeout::Error => e
rescue Timeout::Error => e
raise TimeoutError.new(e.message)
end
end
end
end
17 changes: 5 additions & 12 deletions lib/oauth_active_resource/fake_oauth_access_token.rb
@@ -1,29 +1,24 @@
module OAuthActiveResource

# just simulates the request and sign! methods of a oauth access token
class FakeOAuthAccessToken < OAuth::Consumer

class FakeOAuthAccessToken < ::OAuth::Consumer
attr_accessor :token, :secret
def initialize()
@key = nil
token = 'Anonymous'
secret = 'Anonymous'
token = 'Anonymous'
secret = 'Anonymous'

# ensure that keys are symbols
@options = @@default_options
@options = @@default_options
end

def request(http_method, path, token = nil, request_options = {}, *arguments)
if path !~ /^\//
@http = create_http(path)
_uri = URI.parse(path)
path = "#{_uri.path}#{_uri.query ? "?#{_uri.query}" : ""}"

end

rsp = http.request(create_http_request(http_method, path, token, request_options, *arguments))

rsp
http.request(create_http_request(http_method, path, token, request_options, *arguments))
end

def get(path, headers = {})
Expand All @@ -49,7 +44,5 @@ def delete(path, headers = {})
def sign!(*args)
# do nothing
end

end
end

34 changes: 17 additions & 17 deletions lib/oauth_active_resource/resource.rb
Expand Up @@ -12,14 +12,14 @@ class << self

def self.connection(refresh = false)
@connection = Connection.new(oauth_connection, site,format) if @connection.nil? || refresh
@connection.timeout = timeout if timeout
return @connection
@connection.timeout = timeout if timeout
return @connection
end

#TODO remove when soundcloud api is fixed
# if self has no id, try extracting from uri
def load(*args)
super(*args)
super(*args)
self.id = self.uri.split('/').last if self.id.nil? and defined? self.uri
end

Expand All @@ -31,24 +31,24 @@ def ==(other)

def self.load_collection(*args)
instantiate_collection(*args)
end
end

#
# belongs_to :user
# => will look for a user-id tag and load this user
#
#
def self.belongs_to(*args)
args.each do |k|
args.each do |k|
name = k.to_s
define_method(k) do
define_method(k) do
if @belongs_to_cache.nil?
@belongs_to_cache = {}
end
if not @belongs_to_cache[name]
resource = find_or_create_resource_for(name)
@belongs_to_cache[name] = resource.find(self.send("#{name}_id"))
end
return @belongs_to_cache[name]
return @belongs_to_cache[name]
end
end
end
Expand All @@ -59,29 +59,29 @@ def self.belongs_to(*args)
# User 123 (http://example.com/users/123/) has many friends
# The list of friends can be accessed by http://example.com/users/123/friends
# Our class definition:
#
#
# class User < Resource
# has_many :friends
# end
#
#
# user = User.find(123)
# user.friends.each do |friend|
# p friend.name
# end
#
# # adding a friend
#
# # adding a friend
# stranger = User.find(987)
# user.friends << stranger
# user.friends.save
# => sends a PUT with collection of friends to http://example.com/users/123/friends ## OUTDATED!!?

def self.has_many(*args)
args.each do |k|
args.each do |k|
name = k.to_s
singular = name.singularize
define_method(k) do |*options|
define_method(k) do |*options|

options = options.first || {}
options = options.first || {}
#if @has_many_cache.nil?
# @has_many_cache = {}
#end
Expand All @@ -93,13 +93,13 @@ def self.has_many(*args)
resource = find_or_create_resource_for(singular)
@has_many_cache[cache_name] = OAuthActiveResource::UniqueResourceArray.new(self.connection,resource,collection_path,options)
end
return @has_many_cache[cache_name]
return @has_many_cache[cache_name]
end
end
end


# ignore is added because the multipart gem is adding an extra new line
# ignore is added because the multipart gem is adding an extra new line
# to the last parameter which will break parsing of track[sharing]
def self.multipart_bug_fix(params)
ordered_params = ActiveSupport::OrderedHash.new
Expand Down
30 changes: 14 additions & 16 deletions lib/oauth_active_resource/unique_resource_array.rb
Expand Up @@ -38,8 +38,8 @@ def []=(*args)

# see has_many in Resource
class UniqueResourceArray < UniqueArray
def initialize(connection, resource, collection_path,options = {})
super()
def initialize(connection, resource, collection_path,options = {})
super()

@connection = connection
@collection_path = collection_path
Expand All @@ -58,20 +58,19 @@ def to_xml
return "<#{pt}> #{self.map { |obj| obj.to_xml({:skip_instruct => true})}.join(' ')} </#{pt}>"
end


# DEPRECATED...
#def find(look_for)
# if not (look_for.is_a? String or look_for.is_a? Integer)
# look_for_id = look_for
# else
# look_for_id = look_for.id
# end
#
# self.each do |obj|
# obj.id == look_for_id and return obj
# end
# return nil
#end
# def find(look_for)
# if not (look_for.is_a? String or look_for.is_a? Integer)
# look_for_id = look_for
# else
# look_for_id = look_for.id
# end
#
# self.each do |obj|
# obj.id == look_for_id and return obj
# end
# return nil
# end

def save
response = @connection.handle_response( @connection.put("#{@collection_path}",self.to_xml) )
Expand All @@ -83,5 +82,4 @@ def reload
return self
end
end

end
9 changes: 6 additions & 3 deletions oauth-active-resource.gemspec
@@ -1,12 +1,15 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
# -*- encoding: utf-8 -*-

Gem::Specification.new do |s|
s.name = %q{oauth-active-resource}
s.version = "0.4.4"
s.version = "0.4.5"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Johannes Wagener"]
s.date = %q{2009-11-19}
s.authors = ["Johannes Wagener", "Hannes Tyden"]
s.date = %q{2010-05-26}
s.email = %q{johannes@wagener.cc}
s.extra_rdoc_files = [
"LICENSE",
Expand Down

0 comments on commit b3705a8

Please sign in to comment.