Class: DropboxSession
- Inherits:
-
Object
- Object
- DropboxSession
- Defined in:
- lib/dropbox_sdk.rb
Overview
DropboxSession is responsible for holding OAuth information. It knows how to take your consumer key and secret and request an access token, an authorize url, and get an access token. You just need to pass it to DropboxClient after its been authorized.
Class Method Summary collapse
-
.deserialize(ser) ⇒ Object
Takes a serialized DropboxSession YAML String and returns a new DropboxSession object.
Instance Method Summary collapse
-
#access_token ⇒ Object
Returns the access token, or nil if one hasn’t been acquired yet.
-
#assert_authorized ⇒ Object
If we have an access token, then do nothing.
-
#authorized? ⇒ Boolean
Returns true if this Session has been authorized and has an access_token.
-
#clear_access_token ⇒ Object
Clears the access_token.
-
#do_get(url, headers = nil) ⇒ Object
:nodoc:.
- #do_http_with_body(uri, request, body) ⇒ Object
-
#do_post(url, headers = nil, body = nil) ⇒ Object
:nodoc:.
-
#do_put(url, headers = nil, body = nil) ⇒ Object
:nodoc:.
-
#get_access_token ⇒ Object
Returns the access token.
-
#get_authorize_url(callback = nil) ⇒ Object
This returns a URL that your user must visit to grant permissions to this application.
-
#get_request_token ⇒ Object
This returns a request token.
-
#get_token(url_end, input_token, error_message_prefix) ⇒ Object
: nodoc:.
-
#initialize(consumer_key, consumer_secret) ⇒ DropboxSession
constructor
-
consumer_key - Your Dropbox application’s “app key”.
-
-
#request_token ⇒ Object
Returns the request token, or nil if one hasn’t been acquired yet.
-
#serialize ⇒ Object
serialize the DropboxSession.
-
#set_access_token(key, secret) ⇒ Object
Given a saved access token and secret, you set this Session to use that token and secret * token - this is the access token * secret - this is the access token secret.
-
#set_request_token(key, secret) ⇒ Object
Given a saved request token and secret, set this location’s token and secret * token - this is the request token * secret - this is the request token secret.
Constructor Details
#initialize(consumer_key, consumer_secret) ⇒ DropboxSession
-
consumer_key - Your Dropbox application’s “app key”.
-
consumer_secret - Your Dropbox application’s “app secret”.
26 27 28 29 30 31 |
# File 'lib/dropbox_sdk.rb', line 26 def initialize(consumer_key, consumer_secret) @consumer_key = consumer_key @consumer_secret = consumer_secret @request_token = nil @access_token = nil end |
Class Method Details
.deserialize(ser) ⇒ Object
Takes a serialized DropboxSession YAML String and returns a new DropboxSession object
233 234 235 236 237 238 239 240 241 242 |
# File 'lib/dropbox_sdk.rb', line 233 def self.deserialize(ser) ser = YAML::load(ser) session = DropboxSession.new(ser.pop, ser.pop) session.set_request_token(ser.pop, ser.pop) if ser.length > 0 session.set_access_token(ser.pop, ser.pop) end session end |
Instance Method Details
#access_token ⇒ Object
Returns the access token, or nil if one hasn’t been acquired yet.
170 171 172 |
# File 'lib/dropbox_sdk.rb', line 170 def access_token @access_token end |
#assert_authorized ⇒ Object
If we have an access token, then do nothing. If not, throw a RuntimeError.
202 203 204 205 206 |
# File 'lib/dropbox_sdk.rb', line 202 def unless raise RuntimeError.new('Session does not yet have a request token') end end |
#authorized? ⇒ Boolean
Returns true if this Session has been authorized and has an access_token.
209 210 211 |
# File 'lib/dropbox_sdk.rb', line 209 def !!@access_token end |
#clear_access_token ⇒ Object
Clears the access_token
160 161 162 |
# File 'lib/dropbox_sdk.rb', line 160 def clear_access_token @access_token = nil end |
#do_get(url, headers = nil) ⇒ Object
:nodoc:
80 81 82 83 |
# File 'lib/dropbox_sdk.rb', line 80 def do_get(url, headers=nil) # :nodoc: do_get_with_token(url, @access_token) end |
#do_http_with_body(uri, request, body) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/dropbox_sdk.rb', line 85 def do_http_with_body(uri, request, body) if body != nil if body.is_a?(Hash) form_data = {} body.each {|k,v| form_data[k.to_s] = v if !v.nil?} request.set_form_data(form_data) elsif body.respond_to?(:read) if body.respond_to?(:length) request["Content-Length"] = body.length.to_s elsif body.respond_to?(:stat) && body.stat.respond_to?(:size) request["Content-Length"] = body.stat.size.to_s else raise ArgumentError, "Don't know how to handle 'body' (responds to 'read' but not to 'length' or 'stat.size')." end request.body_stream = body else s = body.to_s request["Content-Length"] = s.length request.body = s end end do_http(uri, @access_token, request) end |
#do_post(url, headers = nil, body = nil) ⇒ Object
:nodoc:
109 110 111 112 113 |
# File 'lib/dropbox_sdk.rb', line 109 def do_post(url, headers=nil, body=nil) # :nodoc: uri = URI.parse(url) do_http_with_body(uri, Net::HTTP::Post.new(uri.request_uri, headers), body) end |
#do_put(url, headers = nil, body = nil) ⇒ Object
:nodoc:
115 116 117 118 119 |
# File 'lib/dropbox_sdk.rb', line 115 def do_put(url, headers=nil, body=nil) # :nodoc: uri = URI.parse(url) do_http_with_body(uri, Net::HTTP::Put.new(uri.request_uri, headers), body) end |
#get_access_token ⇒ Object
Returns the access token. If this DropboxSession doesn’t yet have an access_token, it requests one using the request_token generate from your app’s token and secret. This request will fail unless your user has gone to the authorize_url and approved your request
191 192 193 194 195 196 197 198 199 |
# File 'lib/dropbox_sdk.rb', line 191 def get_access_token return @access_token if if @request_token.nil? raise DropboxAuthError.new("No request token. You must set this or get an authorize url first.") end @access_token = get_token("/access_token", @request_token, "Couldn't get access token.") end |
#get_authorize_url(callback = nil) ⇒ Object
This returns a URL that your user must visit to grant permissions to this application.
145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/dropbox_sdk.rb', line 145 def (callback=nil) get_request_token() url = "/#{Dropbox::API_VERSION}/oauth/authorize?oauth_token=#{URI.escape(@request_token.key)}" if callback url += "&oauth_callback=#{URI.escape(callback)}" end if @locale url += "&locale=#{URI.escape(@locale)}" end "https://#{Dropbox::WEB_SERVER}#{url}" end |
#get_request_token ⇒ Object
This returns a request token. Requests one from the dropbox server using the provided application key and secret if nessecary.
139 140 141 |
# File 'lib/dropbox_sdk.rb', line 139 def get_request_token() @request_token ||= get_token("/request_token", nil, "Error getting request token. Is your app key and secret correctly set?") end |
#get_token(url_end, input_token, error_message_prefix) ⇒ Object
: nodoc:
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/dropbox_sdk.rb', line 122 def get_token(url_end, input_token, ) #: nodoc: response = do_get_with_token("https://#{Dropbox::API_SERVER}:443/#{Dropbox::API_VERSION}/oauth#{url_end}", input_token) if not response.kind_of?(Net::HTTPSuccess) # it must be a 200 raise DropboxAuthError.new("#{} Server returned #{response.code}: #{response.}.", response) end parts = CGI.parse(response.body) if !parts.has_key? "oauth_token" and parts["oauth_token"].length != 1 raise DropboxAuthError.new("Invalid response from #{url_end}: missing \"oauth_token\" parameter: #{response.body}", response) end if !parts.has_key? "oauth_token_secret" and parts["oauth_token_secret"].length != 1 raise DropboxAuthError.new("Invalid response from #{url_end}: missing \"oauth_token\" parameter: #{response.body}", response) end OAuthToken.new(parts["oauth_token"][0], parts["oauth_token_secret"][0]) end |
#request_token ⇒ Object
Returns the request token, or nil if one hasn’t been acquired yet.
165 166 167 |
# File 'lib/dropbox_sdk.rb', line 165 def request_token @request_token end |
#serialize ⇒ Object
serialize the DropboxSession. At DropboxSession’s state is capture in three key/secret pairs. Consumer, request, and access. Serialize returns these in a YAML string, generated from a converted array of the form:
- consumer_key, consumer_secret, request_token.token, request_token.secret, access_token.token, access_token.secret
-
access_token is only included if it already exists in the DropboxSesssion
218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/dropbox_sdk.rb', line 218 def serialize toreturn = [] if @access_token toreturn.push @access_token.secret, @access_token.key end get_request_token toreturn.push @request_token.secret, @request_token.key toreturn.push @consumer_secret, @consumer_key toreturn.to_yaml end |
#set_access_token(key, secret) ⇒ Object
Given a saved access token and secret, you set this Session to use that token and secret
-
token - this is the access token
-
secret - this is the access token secret
184 185 186 |
# File 'lib/dropbox_sdk.rb', line 184 def set_access_token(key, secret) @access_token = OAuthToken.new(key, secret) end |
#set_request_token(key, secret) ⇒ Object
Given a saved request token and secret, set this location’s token and secret
-
token - this is the request token
-
secret - this is the request token secret
177 178 179 |
# File 'lib/dropbox_sdk.rb', line 177 def set_request_token(key, secret) @request_token = OAuthToken.new(key, secret) end |