Class: RedboothRuby::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/redbooth-ruby/session.rb

Constant Summary collapse

OAUTH_URLS =
{
  site: 'https://redbooth.com/api/3',
  authorize_url: 'https://redbooth.com/oauth2/authorize',
  token_url: 'https://redbooth.com/oauth2/token'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Session

Returns a new instance of Session.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/redbooth-ruby/session.rb', line 17

def initialize(opts = {})
  @token = opts[:token]
  @refresh_token = opts[:refresh_token]
  @expires_in = opts[:expires_in]
  @auto_refresh_token = opts[:auto_refresh_token] || true
  @on_token_refresh = opts[:on_token_refresh]
  @consumer_key = opts[:consumer_key] || RedboothRuby.configuration[:consumer_key]
  @consumer_secret = opts[:consumer_secret] || RedboothRuby.configuration[:consumer_secret]
  @oauth_verifier = opts[:oauth_verifier]
  @oauth_token = opts[:oauth_token]
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



6
7
8
# File 'lib/redbooth-ruby/session.rb', line 6

def access_token
  @access_token
end

#auto_refresh_tokenObject

Returns the value of attribute auto_refresh_token.



7
8
9
# File 'lib/redbooth-ruby/session.rb', line 7

def auto_refresh_token
  @auto_refresh_token
end

#consumer_keyObject

Returns the value of attribute consumer_key.



8
9
10
# File 'lib/redbooth-ruby/session.rb', line 8

def consumer_key
  @consumer_key
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



8
9
10
# File 'lib/redbooth-ruby/session.rb', line 8

def consumer_secret
  @consumer_secret
end

#expires_inObject

Returns the value of attribute expires_in.



7
8
9
# File 'lib/redbooth-ruby/session.rb', line 7

def expires_in
  @expires_in
end

#oauth_tokenObject

Returns the value of attribute oauth_token.



9
10
11
# File 'lib/redbooth-ruby/session.rb', line 9

def oauth_token
  @oauth_token
end

#oauth_verifierObject

Returns the value of attribute oauth_verifier.



9
10
11
# File 'lib/redbooth-ruby/session.rb', line 9

def oauth_verifier
  @oauth_verifier
end

#on_token_refresh(&block) ⇒ Object

Returns the value of attribute on_token_refresh.



7
8
9
# File 'lib/redbooth-ruby/session.rb', line 7

def on_token_refresh
  @on_token_refresh
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



7
8
9
# File 'lib/redbooth-ruby/session.rb', line 7

def refresh_token
  @refresh_token
end

#tokenObject

Returns the value of attribute token.



6
7
8
# File 'lib/redbooth-ruby/session.rb', line 6

def token
  @token
end

Instance Method Details

#clientObject



34
35
36
# File 'lib/redbooth-ruby/session.rb', line 34

def client
  @client ||= OAuth2::Client.new(consumer_key, consumer_secret, OAUTH_URLS)
end

#get_access_token_urlObject



38
39
40
41
42
43
44
45
# File 'lib/redbooth-ruby/session.rb', line 38

def get_access_token_url
  uri = URI.parse(OAUTH_URLS[:token_url])
  params = URI.decode_www_form(uri.query.to_s)
  params << ['oauth_verifier', oauth_verifier] if oauth_verifier
  params << ['oauth_token', oauth_token] if oauth_token
  uri.query = URI.encode_www_form(params) if params.size > 0
  uri.to_s
end

#refresh_access_token!Object



52
53
54
55
56
57
58
59
# File 'lib/redbooth-ruby/session.rb', line 52

def refresh_access_token!
  new_access_token = access_token.refresh!
  if new_access_token
    on_token_refresh.call(access_token, new_access_token) if on_token_refresh.is_a?(Proc)
    @access_token = new_access_token
  end
  new_access_token
end

#valid?Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/redbooth-ruby/session.rb', line 29

def valid?
  return false unless token
  true
end