Class: Mogli::Authenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/mogli/authenticator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, secret, callback_url) ⇒ Authenticator

Returns a new instance of Authenticator.



8
9
10
11
12
# File 'lib/mogli/authenticator.rb', line 8

def initialize(client_id,secret,callback_url)
  @client_id = client_id
  @secret = secret
  @callback_url = callback_url
end

Instance Attribute Details

#callback_urlObject (readonly)

Returns the value of attribute callback_url.



6
7
8
# File 'lib/mogli/authenticator.rb', line 6

def callback_url
  @callback_url
end

#client_idObject (readonly)

Returns the value of attribute client_id.



6
7
8
# File 'lib/mogli/authenticator.rb', line 6

def client_id
  @client_id
end

#secretObject (readonly)

Returns the value of attribute secret.



6
7
8
# File 'lib/mogli/authenticator.rb', line 6

def secret
  @secret
end

Instance Method Details

#access_token_url(code) ⇒ Object



19
20
21
# File 'lib/mogli/authenticator.rb', line 19

def access_token_url(code)
  "https://graph.facebook.com/oauth/access_token?client_id=#{client_id}&redirect_uri=#{CGI.escape(callback_url)}&client_secret=#{secret}&code=#{CGI.escape(code)}"
end

#authorize_url(options = {}) ⇒ Object



14
15
16
17
# File 'lib/mogli/authenticator.rb', line 14

def authorize_url(options = {})
  options_part = "&" + options.map {|k,v| "#{k}=#{v.kind_of?(Array) ? v.join(',') : v}" }.join('&') unless options.empty?
  "https://graph.facebook.com/oauth/authorize?client_id=#{client_id}&redirect_uri=#{CGI.escape(callback_url)}#{options_part}"
end

#get_access_token_for_applicationObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mogli/authenticator.rb', line 34

def get_access_token_for_application
    client = Mogli::Client.new
    request = client.class.post(client.api_path('oauth/access_token'),
      :body=> {
        :grant_type => 'client_credentials',
        :client_id => client_id,
        :client_secret => secret
      }
    )
    request.parsed_response.split('=').last
end

#get_access_token_for_session_key(session_keys) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/mogli/authenticator.rb', line 23

def get_access_token_for_session_key(session_keys)
  keystr = session_keys.is_a?(Array) ?
             session_keys.join(',') : session_keys
  client = Mogli::Client.new
  client.post("oauth/exchange_sessions", nil,
              {:type => 'client_cred',
               :client_id => client_id,
               :client_secret => secret,
               :sessions => keystr})
end