Module: SimpleMapper::Oauth

Included in:
HttpAdapter
Defined in:
lib/simple_mapper/default_plugins/oauth.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.with_oauth(controller) ⇒ Object

Ingeniousity here… ;) Duplicates the class to give it a temporary session-attached oauth scope, sets oauth to the Model-Controller-OAuth class, then makes the class use the original class for all of its instantiation. NOTE: This only really makes the class methods use OAuth. Object methods, like associations, won’t play the trick as well.



43
44
45
46
47
48
# File 'lib/simple_mapper/default_plugins/oauth.rb', line 43

def self.with_oauth(controller)
  duped = self.dup
  duped.set_oauth(controller)
  yield if block_given?
  duped
end

Instance Method Details

#oauthObject



50
51
52
# File 'lib/simple_mapper/default_plugins/oauth.rb', line 50

def oauth
  @oauth
end

#requires_oauth(consumer_key, consumer_secret, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/simple_mapper/default_plugins/oauth.rb', line 34

def requires_oauth(consumer_key, consumer_secret, options={})
  @consumer_key = consumer_key
  @consumer_secret = consumer_secret
  @oauth_options = options || {}

  # Ingeniousity here... ;)
  # Duplicates the class to give it a temporary session-attached oauth scope, sets oauth to the Model-Controller-OAuth class,
  # then makes the class use the original class for all of its instantiation.
  # NOTE: This only really makes the class methods use OAuth. Object methods, like associations, won't play the trick as well.
  def self.with_oauth(controller)
    duped = self.dup
    duped.set_oauth(controller)
    yield if block_given?
    duped
  end

  def oauth
    @oauth
  end

  def set_oauth(controller)
    @oauth = OAuthController.new(controller, self, @consumer_key, @consumer_secret, @oauth_options)
    add_callback('initialize_request') do |request|
      @oauth.authenticate! if !@oauth.authorized? && @oauth.scriptable?
      raise RuntimeError, "Must authorize OAuth before attempting to get data from the provider." unless @oauth.authorized?
      @oauth.request_signed!(request)
    end
    @oauth
  end

  true
end

#set_oauth(controller) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/simple_mapper/default_plugins/oauth.rb', line 54

def set_oauth(controller)
  @oauth = OAuthController.new(controller, self, @consumer_key, @consumer_secret, @oauth_options)
  add_callback('initialize_request') do |request|
    @oauth.authenticate! if !@oauth.authorized? && @oauth.scriptable?
    raise RuntimeError, "Must authorize OAuth before attempting to get data from the provider." unless @oauth.authorized?
    @oauth.request_signed!(request)
  end
  @oauth
end