Class: Maromi::Helpers::LazyObject
- Inherits:
-
Object
- Object
- Maromi::Helpers::LazyObject
- Defined in:
- lib/maromi/helpers/lazy_object.rb
Overview
The Lazy Object is automatically attached to the Rack ENV as maromi.helper This basically allows you to interact with Maromi. For the most part, the interface is built up in a particular frameworkâs set of helpers, such as in Sinatra, though direct use of the LazyObject is permitted.
Instance Method Summary collapse
- #consumer ⇒ Object
-
#initialize(request) ⇒ LazyObject
constructor
private
A new instance of LazyObject.
-
#is_authorization_request? ⇒ Boolean
Whether or not the current request should use temporary credentials.
-
#new_consumer(params = {}) ⇒ Maromi::Consumer
A new consumer.
-
#oauth_authenticated? ⇒ Boolean
Whether or not the current request is authenticated by oauth.
- #request_for_authorization ⇒ Object
-
#require_oauth_authentication! ⇒ Object
Fires up the whole thing.
Constructor Details
#initialize(request) ⇒ LazyObject
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of LazyObject.
10 11 12 |
# File 'lib/maromi/helpers/lazy_object.rb', line 10 def initialize(request) @request = request end |
Instance Method Details
#consumer ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/maromi/helpers/lazy_object.rb', line 22 def consumer return @consumer unless @consumer.nil? if && return @consumer = Proxies::ConsumerRequest.new() elsif oauth_authenticated? return @consumer end return @consumer end |
#is_authorization_request? ⇒ Boolean
Returns whether or not the current request should use temporary credentials.
33 34 35 |
# File 'lib/maromi/helpers/lazy_object.rb', line 33 def @request.path == '/oauth/authorize' end |
#new_consumer(params = {}) ⇒ Maromi::Consumer
Returns a new consumer.
60 61 62 |
# File 'lib/maromi/helpers/lazy_object.rb', line 60 def new_consumer(params={}) consumer = Consumer.new(:secret => params[:secret] || Helpers::Token.new, :token => params[:token] || Helpers::Token.new(16), :callback_url => params[:callback], :name => params[:name]) end |
#oauth_authenticated? ⇒ Boolean
Returns whether or not the current request is authenticated by oauth.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/maromi/helpers/lazy_object.rb', line 44 def oauth_authenticated? request, consumer, = OAuth::RequestProxy.proxy(@request) if OAuth::Signature.verify(request) do |r| raise 'Bad Consumer Key' unless consumer = Consumer.get!(r.parameters['oauth_consumer_key']) raise 'Bad Token' unless = Authorization.first(:consumer => consumer, :token => r.parameters['oauth_token']) || Request.first(:consumer => consumer, :token => r.parameters['oauth_token']) [.secret, consumer.secret] end @consumer = Proxies::ConsumerAuthorization.new(, consumer) if .is_a? Authorization @consumer = Proxies::ConsumerRequest.new(, consumer) if .is_a? Request return true if .is_a? Authorization end rescue Exception => e return false end |
#request_for_authorization ⇒ Object
15 16 17 18 19 |
# File 'lib/maromi/helpers/lazy_object.rb', line 15 def @request_for_authorization ||= Request.get!(@request.params['oauth_token']) rescue DataMapper::ObjectNotFoundError @request_for_authorization = nil end |
#require_oauth_authentication! ⇒ Object
Fires up the whole thing. Makes sure that the current request is authenticated and causes Rack to return a 401 Unauthorized immediately if it is not.
39 40 41 |
# File 'lib/maromi/helpers/lazy_object.rb', line 39 def require_oauth_authentication! throw :unauthorized unless oauth_authenticated? end |