Permalink
Browse files

Properly implemented inheritance in mongoid models.

Added OpenTransactToken and PicomoneyToken
  • Loading branch information...
1 parent e488772 commit bfedccf8f5a4c751dabdc634465dc0aa1bf1794b @pelle committed Nov 30, 2010
View
@@ -5,3 +5,4 @@ pkg
.svn
*.gem
.bundle
+.swp
@@ -21,17 +21,17 @@ class ConsumerToken
def self.find_or_create_from_access_token(user,access_token)
if user
- user.consumer_tokens.first(:conditions=>{:type=>self.to_s,:token=>access_token.token}) ||
- user.consumer_tokens.create!(:type=>self.to_s,:token=>access_token.token, :secret=>access_token.secret)
+ user.consumer_tokens.first(:conditions=>{:_type=>self.to_s,:token=>access_token.token}) ||
+ user.consumer_tokens.create!(:_type=>self.to_s,:token=>access_token.token, :secret=>access_token.secret)
else
# Is there a better way of doing this in mongoid?
# Please submit a patch
- user = User.first(:conditions=>{:type=>self.to_s,"consumer_tokens.token"=>access_token.token})
+ user = User.first(:conditions=>{:_type=>self.to_s,"consumer_tokens.token"=>access_token.token})
if user
user.consumer_tokens.detect{|t| t.token==access_token.token && t.is_a?(self)}
else
user = User.create
- user.consumer_tokens.create!(:type=>self.to_s,:token=>access_token.token, :secret=>access_token.secret)
+ user.consumer_tokens.create!(:_type=>self.to_s,:token=>access_token.token, :secret=>access_token.secret)
end
end
end
@@ -79,7 +79,7 @@ def load_consumer
# Override this in you controller to deny user or redirect to login screen.
def deny_access!
- head 403
+ head 401
end
end
@@ -10,6 +10,7 @@
class_name=value[:class_name]||"#{key.to_s.classify}Token"
unless Object.const_defined?(class_name.to_sym)
if File.exists?(File.join(File.dirname(__FILE__), "services","#{key.to_s}_token.rb"))
+ Rails.logger.info File.join(File.dirname(__FILE__), "services","#{key.to_s}_token")
require File.join(File.dirname(__FILE__), "services","#{key.to_s}_token")
else
begin
@@ -0,0 +1,15 @@
+require 'opentransact'
+class OpenTransactToken < ConsumerToken
+
+# def self.server
+# @consumer||=OpenTransact::Server.new credentials
+# end
+
+# def self.consumer
+# @consumer||=server.consumer
+# end
+
+ def client
+ @client ||= OpenTransact::Client.new self.class.credentials.merge( {:token=>token, :secret=>secret})
+ end
+end
@@ -0,0 +1,17 @@
+require File.join(File.dirname(__FILE__),'opentransact_token')
+
+class PicomoneyToken < OpenTransactToken
+
+ def self.credentials
+ @credentials||={
+ :site=>"https://picomoney.com",
+ :consumer_key => super.delete(:key),
+ :consumer_secret => super.delete(:secret)
+ }.merge(super)
+ end
+
+ def about_user
+ client.get("/about_user")
+ end
+
+end
@@ -26,6 +26,7 @@ def consumer
end
def get_request_token(callback_url)
+ Rails.logger.info "OAUTH_CONSUMER #{consumer.inspect}"
consumer.get_request_token(:oauth_callback=>callback_url)
end
@@ -62,11 +63,11 @@ module InstanceMethods
# Main client for interfacing with remote service. Override this to use
# preexisting library eg. Twitter gem.
def client
- @client||=OAuth::AccessToken.new self.class.consumer,token,secret
+ @client||=OAuth::AccessToken.new slelf.class.consumer,token,secret
end
def simple_client
- @simple_client||=SimpleClient.new OAuth::AccessToken.new( self.class.consumer,token,secret)
+ @simple_client||=SimpleClient.new client
end
# Override this to return user data from service

0 comments on commit bfedccf

Please sign in to comment.