Class: Maromi::Helpers::LazyObject

Inherits:
Object
  • Object
show all
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

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

#consumerObject



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 is_authorization_request? && request_for_authorization
    return @consumer = Proxies::ConsumerRequest.new(request_for_authorization)
  elsif oauth_authenticated?
    return @consumer
  end
  return @consumer
end

#is_authorization_request?Boolean

Returns whether or not the current request should use temporary credentials.

Returns:

  • (Boolean)

    whether or not the current request should use temporary credentials



33
34
35
# File 'lib/maromi/helpers/lazy_object.rb', line 33

def is_authorization_request?
  @request.path == '/oauth/authorize'
end

#new_consumer(params = {}) ⇒ Maromi::Consumer

Returns a new consumer.

Returns:



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.

Returns:

  • (Boolean)

    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, authorization = 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 = Authorization.first(:consumer => consumer, :token => r.parameters['oauth_token']) || Request.first(:consumer => consumer, :token => r.parameters['oauth_token'])
      [authorization.secret, consumer.secret]
    end
    @consumer = Proxies::ConsumerAuthorization.new(authorization, consumer) if authorization.is_a? Authorization
    @consumer = Proxies::ConsumerRequest.new(authorization, consumer) if authorization.is_a? Request
    return true if authorization.is_a? Authorization
  end
rescue Exception => e
  return false
end

#request_for_authorizationObject



15
16
17
18
19
# File 'lib/maromi/helpers/lazy_object.rb', line 15

def request_for_authorization
  @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