Class: Bitly::V3::OAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/bitly/v3/oauth.rb

Overview

OAuth consumer for authentication

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(consumer_token, consumer_secret) ⇒ OAuth


6
7
8
# File 'lib/bitly/v3/oauth.rb', line 6

def initialize(consumer_token, consumer_secret)
  @consumer_token, @consumer_secret = consumer_token, consumer_secret
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token


5
6
7
# File 'lib/bitly/v3/oauth.rb', line 5

def access_token
  @access_token
end

Instance Method Details

#authorize_url(redirect_url, state = nil) ⇒ Object

Get the url to redirect a user to, pass the url you want the user to be redirected back to.


22
23
24
25
26
# File 'lib/bitly/v3/oauth.rb', line 22

def authorize_url(redirect_url, state=nil)
  params = {:redirect_uri => redirect_url}
  params[:state] = state if state
  client.auth_code.authorize_url(params).gsub(/api-ssl\./,'')
end

#clientObject

Get the OAuth 2 client


11
12
13
14
15
16
17
18
# File 'lib/bitly/v3/oauth.rb', line 11

def client
  @client ||= ::OAuth2::Client.new(
    @consumer_token,
    @consumer_secret,
    :site => 'https://api-ssl.bitly.com',
    :token_url => '/oauth/access_token'
  )
end

#get_access_token_from_code(code, redirect_url) ⇒ Object

Get the access token. You must pass the exact same redirect_url passed to the authorize_url method


30
31
32
# File 'lib/bitly/v3/oauth.rb', line 30

def get_access_token_from_code(code,redirect_url)
  @access_token ||= client.auth_code.get_token(code, :redirect_uri => redirect_url, :parse => :query)
end

#get_access_token_from_token(token, params = {}) ⇒ Object

If you already have a user token, this method gets the access token


35
36
37
38
# File 'lib/bitly/v3/oauth.rb', line 35

def get_access_token_from_token(token, params={})
  params = params.inject({}) { |options, (key, value)| options[key.to_s] = value; options }
  @access_token ||= ::OAuth2::AccessToken.new(client, token, params)
end