dropbox-sdk 1.6.1 → 1.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/CHANGELOG +4 -0
  2. data/lib/dropbox_sdk.rb +120 -121
  3. metadata +2 -2
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 1.6.2 (2013-10-10)
2
+ * Put params in body for POST requests (they were being put in the URL).
3
+ Fixes bug with /delta and really long cursors.
4
+
1
5
  1.6.1 (2013-07-08)
2
6
  * Fixed syntax error (Ruby 1.8.7 didn't mind, but Ruby 1.9 did).
3
7
 
@@ -14,10 +14,24 @@ module Dropbox # :nodoc:
14
14
  WEB_SERVER = "www.dropbox.com"
15
15
 
16
16
  API_VERSION = 1
17
- SDK_VERSION = "1.6.1"
17
+ SDK_VERSION = "1.6.2"
18
18
 
19
19
  TRUSTED_CERT_FILE = File.join(File.dirname(__FILE__), 'trusted-certs.crt')
20
20
 
21
+ def self.clean_params(params)
22
+ r = {}
23
+ params.each do |k,v|
24
+ r[k] = v.to_s if not v.nil?
25
+ end
26
+ r
27
+ end
28
+
29
+ def self.make_query_string(params)
30
+ clean_params(params).collect {|k,v|
31
+ CGI.escape(k) + "=" + CGI.escape(v)
32
+ }.join("&")
33
+ end
34
+
21
35
  def self.do_http(uri, request) # :nodoc:
22
36
  http = Net::HTTP.new(uri.host, uri.port)
23
37
 
@@ -81,6 +95,28 @@ end
81
95
 
82
96
  class DropboxSessionBase # :nodoc:
83
97
 
98
+ attr_writer :locale
99
+
100
+ def initialize(locale)
101
+ @locale = locale
102
+ end
103
+
104
+ private
105
+
106
+ def build_url(path, content_server)
107
+ port = 443
108
+ host = content_server ? Dropbox::API_CONTENT_SERVER : Dropbox::API_SERVER
109
+ full_path = "/#{Dropbox::API_VERSION}#{path}"
110
+ return URI::HTTPS.build({:host => host, :path => full_path})
111
+ end
112
+
113
+ def build_url_with_params(path, params, content_server) # :nodoc:
114
+ target = build_url(path, content_server)
115
+ params['locale'] = @locale
116
+ target.query = Dropbox::make_query_string(params)
117
+ return target
118
+ end
119
+
84
120
  protected
85
121
 
86
122
  def do_http(uri, request) # :nodoc:
@@ -90,19 +126,17 @@ class DropboxSessionBase # :nodoc:
90
126
 
91
127
  public
92
128
 
93
- def do_get(url, headers=nil) # :nodoc:
129
+ def do_get(path, params=nil, headers=nil, content_server=false) # :nodoc:
130
+ params ||= {}
94
131
  assert_authorized
95
- uri = URI.parse(url)
96
- request = Net::HTTP::Get.new(uri.request_uri)
97
- do_http(uri, request)
132
+ uri = build_url_with_params(path, params, content_server)
133
+ do_http(uri, Net::HTTP::Get.new(uri.request_uri))
98
134
  end
99
135
 
100
136
  def do_http_with_body(uri, request, body)
101
137
  if body != nil
102
138
  if body.is_a?(Hash)
103
- form_data = {}
104
- body.each {|k,v| form_data[k.to_s] = v if !v.nil?}
105
- request.set_form_data(form_data)
139
+ request.set_form_data(Dropbox::clean_params(body))
106
140
  elsif body.respond_to?(:read)
107
141
  if body.respond_to?(:length)
108
142
  request["Content-Length"] = body.length.to_s
@@ -121,15 +155,18 @@ class DropboxSessionBase # :nodoc:
121
155
  do_http(uri, request)
122
156
  end
123
157
 
124
- def do_post(url, headers=nil, body=nil) # :nodoc:
158
+ def do_post(path, params=nil, headers=nil, content_server=false) # :nodoc:
159
+ params ||= {}
125
160
  assert_authorized
126
- uri = URI.parse(url)
127
- do_http_with_body(uri, Net::HTTP::Post.new(uri.request_uri, headers), body)
161
+ uri = build_url(path, content_server)
162
+ params['locale'] = @locale
163
+ do_http_with_body(uri, Net::HTTP::Post.new(uri.request_uri, headers), params)
128
164
  end
129
165
 
130
- def do_put(url, headers=nil, body=nil) # :nodoc:
166
+ def do_put(path, params=nil, headers=nil, body=nil, content_server=false) # :nodoc:
167
+ params ||= {}
131
168
  assert_authorized
132
- uri = URI.parse(url)
169
+ uri = build_url_with_params(path, params, content_server)
133
170
  do_http_with_body(uri, Net::HTTP::Put.new(uri.request_uri, headers), body)
134
171
  end
135
172
  end
@@ -141,7 +178,8 @@ class DropboxSession < DropboxSessionBase # :nodoc:
141
178
 
142
179
  # * consumer_key - Your Dropbox application's "app key".
143
180
  # * consumer_secret - Your Dropbox application's "app secret".
144
- def initialize(consumer_key, consumer_secret)
181
+ def initialize(consumer_key, consumer_secret, locale=nil)
182
+ super(locale)
145
183
  @consumer_key = consumer_key
146
184
  @consumer_secret = consumer_secret
147
185
  @request_token = nil
@@ -304,7 +342,9 @@ end
304
342
 
305
343
 
306
344
  class DropboxOAuth2Session < DropboxSessionBase # :nodoc:
307
- def initialize(oauth2_access_token)
345
+
346
+ def initialize(oauth2_access_token, locale=nil)
347
+ super(locale)
308
348
  if not oauth2_access_token.is_a?(String)
309
349
  raise "bad type for oauth2_access_token (expecting String)"
310
350
  end
@@ -340,25 +380,19 @@ class DropboxOAuth2FlowBase # :nodoc:
340
380
  end
341
381
 
342
382
  def _get_authorize_url(redirect_uri, state)
343
- params = {"client_id" => @consumer_key, "response_type" => "code"}
344
- if not redirect_uri.nil?
345
- params["redirect_uri"] = redirect_uri
346
- end
347
- if not state.nil?
348
- params["state"] = state
349
- end
350
- if not @locale.nil?
351
- params['locale'] = @locale
352
- end
383
+ params = {
384
+ "client_id" => @consumer_key,
385
+ "response_type" => "code",
386
+ "redirect_uri" => redirect_uri,
387
+ "state" => state,
388
+ "locale" => @locale,
389
+ }
353
390
 
354
391
  host = Dropbox::WEB_SERVER
355
392
  path = "/#{Dropbox::API_VERSION}/oauth2/authorize"
356
393
 
357
394
  target = URI::Generic.new("https", nil, host, nil, nil, path, nil, nil, nil)
358
-
359
- target.query = params.collect {|k,v|
360
- CGI.escape(k) + "=" + CGI.escape(v)
361
- }.join("&")
395
+ target.query = Dropbox::make_query_string(params)
362
396
 
363
397
  target.to_s
364
398
  end
@@ -378,18 +412,11 @@ class DropboxOAuth2FlowBase # :nodoc:
378
412
  params = {
379
413
  "grant_type" => "authorization_code",
380
414
  "code" => code,
415
+ "redirect_uri" => original_redirect_uri,
416
+ "locale" => @locale,
381
417
  }
382
418
 
383
- if not @locale.nil?
384
- params['locale'] = @locale
385
- end
386
- if not original_redirect_uri.nil?
387
- params['redirect_uri'] = original_redirect_uri
388
- end
389
-
390
- form_data = {}
391
- params.each {|k,v| form_data[k.to_s] = v if !v.nil?}
392
- request.set_form_data(form_data)
419
+ request.set_form_data(Dropbox::clean_params(params))
393
420
 
394
421
  response = Dropbox::do_http(uri, request)
395
422
 
@@ -653,10 +680,13 @@ class DropboxClient
653
680
  # * +locale+: The user's current locale (used to localize error messages).
654
681
  def initialize(oauth2_access_token, root="auto", locale=nil)
655
682
  if oauth2_access_token.is_a?(String)
656
- @session = DropboxOAuth2Session.new(oauth2_access_token)
683
+ @session = DropboxOAuth2Session.new(oauth2_access_token, locale)
657
684
  elsif oauth2_access_token.is_a?(DropboxSession)
658
685
  @session = oauth2_access_token
659
686
  @session.get_access_token
687
+ if not locale.nil?
688
+ @session.locale = locale
689
+ end
660
690
  else
661
691
  raise ArgumentError.new("oauth2_access_token doesn't have a valid type")
662
692
  end
@@ -671,8 +701,6 @@ class DropboxClient
671
701
  #sandbox is the URL root component that indicates this
672
702
  @root = "sandbox"
673
703
  end
674
-
675
- @locale = locale
676
704
  end
677
705
 
678
706
  # Returns some information about the current user's Dropbox account (the "current user"
@@ -681,7 +709,7 @@ class DropboxClient
681
709
  # For a detailed description of what this call returns, visit:
682
710
  # https://www.dropbox.com/developers/reference/api#account-info
683
711
  def account_info()
684
- response = @session.do_get build_url("/account/info")
712
+ response = @session.do_get "/account/info"
685
713
  Dropbox::parse_response(response)
686
714
  end
687
715
 
@@ -720,16 +748,14 @@ class DropboxClient
720
748
  # The file will not overwrite any pre-existing file.
721
749
  def put_file(to_path, file_obj, overwrite=false, parent_rev=nil)
722
750
  path = "/files_put/#{@root}#{format_path(to_path)}"
723
-
724
751
  params = {
725
- 'overwrite' => overwrite.to_s
752
+ 'overwrite' => overwrite.to_s,
753
+ 'parent_rev' => parent_rev,
726
754
  }
727
755
 
728
- params['parent_rev'] = parent_rev unless parent_rev.nil?
729
-
730
- response = @session.do_put(build_url(path, params, content_server=true),
731
- {"Content-Type" => "application/octet-stream"},
732
- file_obj)
756
+ headers = {"Content-Type" => "application/octet-stream"}
757
+ content_server = true
758
+ response = @session.do_put path, params, headers, file_obj, content_server
733
759
 
734
760
  Dropbox::parse_response(response)
735
761
  end
@@ -819,20 +845,24 @@ class DropboxClient
819
845
  end
820
846
 
821
847
  def commit_chunked_upload(to_path, upload_id, overwrite=false, parent_rev=nil) #:nodoc
848
+ path = "/commit_chunked_upload/#{@root}#{format_path(to_path)}"
822
849
  params = {'overwrite' => overwrite.to_s,
823
850
  'upload_id' => upload_id,
824
- 'parent_rev' => parent_rev.to_s,
851
+ 'parent_rev' => parent_rev,
825
852
  }
826
- @session.do_post(build_url("/commit_chunked_upload/#{@root}#{format_path(to_path)}", params, content_server=true))
853
+ headers = nil
854
+ content_server = true
855
+ @session.do_post path, params, headers, content_server
827
856
  end
828
857
 
829
858
  def partial_chunked_upload(data, upload_id=nil, offset=nil) #:nodoc
830
- params = {}
831
- params['upload_id'] = upload_id.to_s if upload_id
832
- params['offset'] = offset.to_s if offset
833
- @session.do_put(build_url('/chunked_upload', params, content_server=true),
834
- {'Content-Type' => "application/octet-stream"},
835
- data)
859
+ params = {
860
+ 'upload_id' => upload_id,
861
+ 'offset' => offset,
862
+ }
863
+ headers = {'Content-Type' => "application/octet-stream"}
864
+ content_server = true
865
+ @session.do_put '/chunked_upload', params, headers, data, content_server
836
866
  end
837
867
 
838
868
  # Download a file
@@ -873,11 +903,13 @@ class DropboxClient
873
903
  # Returns:
874
904
  # * The HTTPResponse for the file download request.
875
905
  def get_file_impl(from_path, rev=nil) # :nodoc:
876
- params = {}
877
- params['rev'] = rev.to_s if rev
878
-
879
906
  path = "/files/#{@root}#{format_path(from_path)}"
880
- @session.do_get build_url(path, params, content_server=true)
907
+ params = {
908
+ 'rev' => rev,
909
+ }
910
+ headers = nil
911
+ content_server = true
912
+ @session.do_get path, params, headers, content_server
881
913
  end
882
914
  private :get_file_impl
883
915
 
@@ -920,7 +952,7 @@ class DropboxClient
920
952
  "from_path" => format_path(from_path, false),
921
953
  "to_path" => format_path(to_path, false),
922
954
  }
923
- response = @session.do_post build_url("/fileops/copy", params)
955
+ response = @session.do_post "/fileops/copy", params
924
956
  Dropbox::parse_response(response)
925
957
  end
926
958
 
@@ -938,7 +970,7 @@ class DropboxClient
938
970
  "root" => @root,
939
971
  "path" => format_path(path, false),
940
972
  }
941
- response = @session.do_post build_url("/fileops/create_folder", params)
973
+ response = @session.do_post "/fileops/create_folder", params
942
974
 
943
975
  Dropbox::parse_response(response)
944
976
  end
@@ -957,7 +989,7 @@ class DropboxClient
957
989
  "root" => @root,
958
990
  "path" => format_path(path, false),
959
991
  }
960
- response = @session.do_post build_url("/fileops/delete", params)
992
+ response = @session.do_post "/fileops/delete", params
961
993
  Dropbox::parse_response(response)
962
994
  end
963
995
 
@@ -978,7 +1010,7 @@ class DropboxClient
978
1010
  "from_path" => format_path(from_path, false),
979
1011
  "to_path" => format_path(to_path, false),
980
1012
  }
981
- response = @session.do_post build_url("/fileops/move", params)
1013
+ response = @session.do_post "/fileops/move", params
982
1014
  Dropbox::parse_response(response)
983
1015
  end
984
1016
 
@@ -1009,15 +1041,14 @@ class DropboxClient
1009
1041
  params = {
1010
1042
  "file_limit" => file_limit.to_s,
1011
1043
  "list" => list.to_s,
1012
- "include_deleted" => include_deleted.to_s
1044
+ "include_deleted" => include_deleted.to_s,
1045
+ "hash" => hash,
1046
+ "rev" => rev,
1013
1047
  }
1014
1048
 
1015
- params["hash"] = hash if hash
1016
- params["rev"] = rev if rev
1017
-
1018
- response = @session.do_get build_url("/metadata/#{@root}#{format_path(path)}", params=params)
1049
+ response = @session.do_get "/metadata/#{@root}#{format_path(path)}", params
1019
1050
  if response.kind_of? Net::HTTPRedirection
1020
- raise DropboxNotModified.new("metadata not modified")
1051
+ raise DropboxNotModified.new("metadata not modified")
1021
1052
  end
1022
1053
  Dropbox::parse_response(response)
1023
1054
  end
@@ -1043,7 +1074,7 @@ class DropboxClient
1043
1074
  'include_deleted' => include_deleted.to_s
1044
1075
  }
1045
1076
 
1046
- response = @session.do_get build_url("/search/#{@root}#{format_path(path)}", params)
1077
+ response = @session.do_get "/search/#{@root}#{format_path(path)}", params
1047
1078
  Dropbox::parse_response(response)
1048
1079
  end
1049
1080
 
@@ -1061,14 +1092,12 @@ class DropboxClient
1061
1092
  # For a detailed description of what this call returns, visit:
1062
1093
  # https://www.dropbox.com/developers/reference/api#revisions
1063
1094
  def revisions(path, rev_limit=1000)
1064
-
1065
1095
  params = {
1066
1096
  'rev_limit' => rev_limit.to_s
1067
1097
  }
1068
1098
 
1069
- response = @session.do_get build_url("/revisions/#{@root}#{format_path(path)}", params)
1099
+ response = @session.do_get "/revisions/#{@root}#{format_path(path)}", params
1070
1100
  Dropbox::parse_response(response)
1071
-
1072
1101
  end
1073
1102
 
1074
1103
  # Restore a file to a previous revision.
@@ -1086,7 +1115,7 @@ class DropboxClient
1086
1115
  'rev' => rev.to_s
1087
1116
  }
1088
1117
 
1089
- response = @session.do_post build_url("/restore/#{@root}#{format_path(path)}", params)
1118
+ response = @session.do_post "/restore/#{@root}#{format_path(path)}", params
1090
1119
  Dropbox::parse_response(response)
1091
1120
  end
1092
1121
 
@@ -1101,9 +1130,9 @@ class DropboxClient
1101
1130
  #
1102
1131
  # Returns:
1103
1132
  # * A Hash object that looks like the following:
1104
- # {'url': 'https://dl.dropbox.com/0/view/wvxv1fw6on24qw7/file.mov', 'expires': 'Thu, 16 Sep 2011 01:01:25 +0000'}
1133
+ # {'url': 'https://dl.dropboxusercontent.com/1/view/abcdefghijk/example', 'expires': 'Thu, 16 Sep 2011 01:01:25 +0000'}
1105
1134
  def media(path)
1106
- response = @session.do_get build_url("/media/#{@root}#{format_path(path)}")
1135
+ response = @session.do_get "/media/#{@root}#{format_path(path)}"
1107
1136
  Dropbox::parse_response(response)
1108
1137
  end
1109
1138
 
@@ -1118,11 +1147,11 @@ class DropboxClient
1118
1147
  #
1119
1148
  # Returns:
1120
1149
  # * A Hash object that looks like the following example:
1121
- # {'url': 'http://www.dropbox.com/s/m/a2mbDa2', 'expires': 'Thu, 16 Sep 2011 01:01:25 +0000'}
1150
+ # {'url': 'https://db.tt/c0mFuu1Y', 'expires': 'Tue, 01 Jan 2030 00:00:00 +0000'}
1122
1151
  # For a detailed description of what this call returns, visit:
1123
1152
  # https://www.dropbox.com/developers/reference/api#shares
1124
1153
  def shares(path)
1125
- response = @session.do_get build_url("/shares/#{@root}#{format_path(path)}")
1154
+ response = @session.do_get "/shares/#{@root}#{format_path(path)}"
1126
1155
  Dropbox::parse_response(response)
1127
1156
  end
1128
1157
 
@@ -1203,12 +1232,11 @@ class DropboxClient
1203
1232
  # way. To facilitate this, the _path_ strings above are lower-cased versions of
1204
1233
  # the actual path. The _metadata_ dicts have the original, case-preserved path.
1205
1234
  def delta(cursor=nil)
1206
- params = {}
1207
- if cursor
1208
- params['cursor'] = cursor
1209
- end
1235
+ params = {
1236
+ 'cursor' => cursor,
1237
+ }
1210
1238
 
1211
- response = @session.do_post build_url("/delta", params)
1239
+ response = @session.do_post "/delta", params
1212
1240
  Dropbox::parse_response(response)
1213
1241
  end
1214
1242
 
@@ -1222,15 +1250,13 @@ class DropboxClient
1222
1250
  # Returns:
1223
1251
  # * The HTTPResponse for the thumbnail request.
1224
1252
  def thumbnail_impl(from_path, size='large') # :nodoc:
1225
- from_path = format_path(from_path, true)
1226
-
1253
+ path = "/thumbnails/#{@root}#{format_path(from_path, true)}"
1227
1254
  params = {
1228
1255
  "size" => size
1229
1256
  }
1230
-
1231
- url = build_url("/thumbnails/#{@root}#{from_path}", params, content_server=true)
1232
-
1233
- @session.do_get url
1257
+ headers = nil
1258
+ content_server = true
1259
+ @session.do_get path, params, headers, content_server
1234
1260
  end
1235
1261
  private :thumbnail_impl
1236
1262
 
@@ -1246,9 +1272,7 @@ class DropboxClient
1246
1272
  # {"expires"=>"Fri, 31 Jan 2042 21:01:05 +0000", "copy_ref"=>"z1X6ATl6aWtzOGq0c3g5Ng"}
1247
1273
  def create_copy_ref(path)
1248
1274
  path = "/copy_ref/#{@root}#{format_path(path)}"
1249
-
1250
- response = @session.do_get(build_url(path, {}))
1251
-
1275
+ response = @session.do_get path
1252
1276
  Dropbox::parse_response(response)
1253
1277
  end
1254
1278
 
@@ -1262,39 +1286,14 @@ class DropboxClient
1262
1286
  # Returns:
1263
1287
  # * A hash with the metadata of the new file.
1264
1288
  def add_copy_ref(to_path, copy_ref)
1265
- path = "/fileops/copy"
1266
-
1267
1289
  params = {'from_copy_ref' => copy_ref,
1268
1290
  'to_path' => "#{to_path}",
1269
1291
  'root' => @root}
1270
1292
 
1271
- response = @session.do_post(build_url(path, params))
1272
-
1293
+ response = @session.do_post "/fileops/copy", params
1273
1294
  Dropbox::parse_response(response)
1274
1295
  end
1275
1296
 
1276
- def build_url(url, params=nil, content_server=false) # :nodoc:
1277
- port = 443
1278
- host = content_server ? Dropbox::API_CONTENT_SERVER : Dropbox::API_SERVER
1279
- versioned_url = "/#{Dropbox::API_VERSION}#{url}"
1280
-
1281
- target = URI::Generic.new("https", nil, host, port, nil, versioned_url, nil, nil, nil)
1282
-
1283
- #add a locale param if we have one
1284
- #initialize a params object is we don't have one
1285
- if @locale
1286
- (params ||= {})['locale']=@locale
1287
- end
1288
-
1289
- if params
1290
- target.query = params.collect {|k,v|
1291
- CGI.escape(k) + "=" + CGI.escape(v)
1292
- }.join("&")
1293
- end
1294
-
1295
- target.to_s
1296
- end
1297
-
1298
1297
  #From the oauth spec plus "/". Slash should not be ecsaped
1299
1298
  RESERVED_CHARACTERS = /[^a-zA-Z0-9\-\.\_\~\/]/ # :nodoc:
1300
1299
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dropbox-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.1
4
+ version: 1.6.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-08 00:00:00.000000000 Z
12
+ date: 2013-10-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json