Class: DropboxApi::ConnectionBuilder
- Inherits:
-
Object
- Object
- DropboxApi::ConnectionBuilder
- Defined in:
- lib/dropbox_api/connection_builder.rb
Instance Attribute Summary collapse
-
#namespace_id ⇒ Object
Returns the value of attribute namespace_id.
Instance Method Summary collapse
- #build(url) ⇒ Object
- #can_refresh_access_token? ⇒ Boolean
-
#initialize(oauth_bearer = nil, access_token: nil, on_token_refreshed: nil) ⇒ ConnectionBuilder
constructor
A new instance of ConnectionBuilder.
- #middleware ⇒ Object
- #refresh_access_token ⇒ Object
Constructor Details
#initialize(oauth_bearer = nil, access_token: nil, on_token_refreshed: nil) ⇒ ConnectionBuilder
Returns a new instance of ConnectionBuilder.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/dropbox_api/connection_builder.rb', line 6 def initialize(oauth_bearer = nil, access_token: nil, on_token_refreshed: nil) if access_token if !access_token.is_a?(OAuth2::AccessToken) raise ArgumentError, "access_token should be an OAuth2::AccessToken" end @access_token = access_token @on_token_refreshed = on_token_refreshed elsif oauth_bearer @oauth_bearer = oauth_bearer else raise ArgumentError, "Either oauth_bearer or access_token should be set" end end |
Instance Attribute Details
#namespace_id ⇒ Object
Returns the value of attribute namespace_id.
4 5 6 |
# File 'lib/dropbox_api/connection_builder.rb', line 4 def namespace_id @namespace_id end |
Instance Method Details
#build(url) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/dropbox_api/connection_builder.rb', line 44 def build(url) Faraday.new(url) do |connection| connection.use DropboxApi::MiddleWare::PathRoot, { namespace_id: self.namespace_id } middleware.apply(connection) do connection. :Bearer, bearer yield connection end end end |
#can_refresh_access_token? ⇒ Boolean
25 26 27 |
# File 'lib/dropbox_api/connection_builder.rb', line 25 def can_refresh_access_token? @access_token && @access_token.refresh_token end |
#middleware ⇒ Object
21 22 23 |
# File 'lib/dropbox_api/connection_builder.rb', line 21 def middleware @middleware ||= MiddleWare::Stack.new end |
#refresh_access_token ⇒ Object
29 30 31 32 |
# File 'lib/dropbox_api/connection_builder.rb', line 29 def refresh_access_token @access_token = @access_token.refresh! @on_token_refreshed.call(@access_token.to_hash) if @on_token_refreshed end |