Module: Devbootcamp::OAuth

Defined in:
lib/devbootcamp/oauth.rb

Class Method Summary collapse

Class Method Details

.authorize!(oauth_code, options = {}) ⇒ Object



56
57
58
59
60
61
# File 'lib/devbootcamp/oauth.rb', line 56

def authorize!(oauth_code, options = {})
  @token = client.auth_code.get_token(oauth_code,
    :redirect_uri => options.fetch(:redirect_uri, callback_url)
  )
  authorized?
end

.authorize_url(params = {}) ⇒ Object



24
25
26
27
28
# File 'lib/devbootcamp/oauth.rb', line 24

def authorize_url(params={})
  params[:redirect_uri] ||= callback_url
  raise "redirect_uri cannot be blank" if params[:redirect_uri].blank?
  client.auth_code.authorize_url(params)
end

.authorized?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/devbootcamp/oauth.rb', line 52

def authorized?
  !!token
end

.clientObject



14
15
16
17
18
# File 'lib/devbootcamp/oauth.rb', line 14

def client
  @client ||= OAuth2::Client.new(
    application_id, secret, site: site
  )
end

.refresh!Object



63
64
65
# File 'lib/devbootcamp/oauth.rb', line 63

def refresh!
  self.token = token.refresh! if token && token.refresh_token
end

.site_uriObject



20
21
22
# File 'lib/devbootcamp/oauth.rb', line 20

def site_uri
  URI(client.site)
end

.token_as_hashObject



39
40
41
42
43
44
45
# File 'lib/devbootcamp/oauth.rb', line 39

def token_as_hash
  {
    token: token.token,
    refresh_token: token.refresh_token,
    expires_at: token.expires_at,
  }
end

.token_from_hash(hash) ⇒ Object



47
48
49
50
# File 'lib/devbootcamp/oauth.rb', line 47

def token_from_hash(hash)
  return unless hash
  @token = OAuth2::AccessToken.new(client, hash[:token], hash)
end

.unauthorize_url(params = {}) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/devbootcamp/oauth.rb', line 30

def unauthorize_url(params={})
  params[:redirect_uri] ||= callback_url
  raise "redirect_uri cannot be blank" if params[:redirect_uri].blank?
  site_uri.tap do |uri|
    uri.path = '/unauthenticate'
    uri.query = params.to_param
  end.to_s
end