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
A new instance of DropboxSession.
-
#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
Returns a new instance of DropboxSession.
22 23 24 25 26 27 |
# File 'lib/dropbox_sdk.rb', line 22 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
216 217 218 219 220 221 222 223 224 225 |
# File 'lib/dropbox_sdk.rb', line 216 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.
153 154 155 |
# File 'lib/dropbox_sdk.rb', line 153 def access_token @access_token end |
#assert_authorized ⇒ Object
If we have an access token, then do nothing. If not, throw a RuntimeError.
185 186 187 188 189 |
# File 'lib/dropbox_sdk.rb', line 185 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.
192 193 194 |
# File 'lib/dropbox_sdk.rb', line 192 def !!@access_token end |
#clear_access_token ⇒ Object
Clears the access_token
143 144 145 |
# File 'lib/dropbox_sdk.rb', line 143 def clear_access_token @access_token = nil end |
#do_get(url, headers = nil) ⇒ Object
:nodoc:
63 64 65 66 |
# File 'lib/dropbox_sdk.rb', line 63 def do_get(url, headers=nil) # :nodoc: do_get_with_token(url, @access_token) end |
#do_http_with_body(uri, request, body) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/dropbox_sdk.rb', line 68 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:
92 93 94 95 96 |
# File 'lib/dropbox_sdk.rb', line 92 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:
98 99 100 101 102 |
# File 'lib/dropbox_sdk.rb', line 98 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
174 175 176 177 178 179 180 181 182 |
# File 'lib/dropbox_sdk.rb', line 174 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.
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/dropbox_sdk.rb', line 128 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.
122 123 124 |
# File 'lib/dropbox_sdk.rb', line 122 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:
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/dropbox_sdk.rb', line 105 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.
148 149 150 |
# File 'lib/dropbox_sdk.rb', line 148 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
201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/dropbox_sdk.rb', line 201 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
167 168 169 |
# File 'lib/dropbox_sdk.rb', line 167 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
160 161 162 |
# File 'lib/dropbox_sdk.rb', line 160 def set_request_token(key, secret) @request_token = OAuthToken.new(key, secret) end |