Class: RedboothRuby::Session

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Session

Returns a new instance of Session.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/redbooth-ruby/session.rb', line 12

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]
  oauth2_base = opts[:oauth2_base] || RedboothRuby.configuration[:oauth2_base] || 'redbooth.com'
  oauth_site = "https://#{oauth2_base}/api/3"
  @oauth_urls = {
    site: "https://#{oauth2_base}/api/3",
    authorize_url: "https://#{oauth2_base}/oauth2/authorize",
    token_url: "https://#{oauth2_base}/oauth2/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_urlsObject

Returns the value of attribute oauth_urls.



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

def oauth_urls
  @oauth_urls
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



36
37
38
39
40
# File 'lib/redbooth-ruby/session.rb', line 36

def client
  options = @oauth_urls
  options[:raise_errors] = false
  @client ||= OAuth2::Client.new(consumer_key, consumer_secret, options)
end

#get_access_token_urlObject



42
43
44
45
46
47
48
49
# File 'lib/redbooth-ruby/session.rb', line 42

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



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/redbooth-ruby/session.rb', line 56

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)
    @token = new_access_token.token
    @refresh_token = new_access_token.refresh_token
    @expires_in = new_access_token.expires_in
    @access_token = nil
  end
  new_access_token
end

#valid?Boolean

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/redbooth-ruby/session.rb', line 31

def valid?
  return false unless token
  true
end