dewey 0.2.4 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,13 @@
1
+ ## 0.2.6 (November 3, 2010)
2
+
3
+ * Downloaded files are automatically rewound
4
+
5
+ ## 0.2.5 (November 3, 2010)
6
+
7
+ * Sheet (gid) support for spreadsheets
8
+ * Delegate authentication, more reliable and no need for eval + alias_method
9
+ * Fix spreadsheet calls not authenticating
10
+
1
11
  ## 0.2.4 (November 2, 2010)
2
12
 
3
13
  Additions:
@@ -41,7 +41,20 @@ module Dewey
41
41
  end
42
42
 
43
43
  def token(service = nil)
44
- @authentications[service || :writely]
44
+ service = guard(service)
45
+ authenticate!(service) unless authenticated?(service)
46
+ @authentications[service]
47
+ end
48
+
49
+ private
50
+
51
+ def guard(service)
52
+ case service
53
+ when nil then :writely
54
+ when 'spreadsheet' then :wise
55
+ else
56
+ service.to_sym
57
+ end
45
58
  end
46
59
  end
47
60
  end
@@ -30,7 +30,7 @@ module Dewey
30
30
  def authenticate!
31
31
  @@authenticator.authenticate!
32
32
  end
33
-
33
+
34
34
  # Queries the document list for a particular title. Titles can be either
35
35
  # full or partial matches. Matches are returned as an array of ids.
36
36
  #
@@ -45,7 +45,7 @@ module Dewey
45
45
  headers = base_headers(false)
46
46
  url = "#{GOOGLE_FEED_URL}?title=#{title}"
47
47
  url << "&title-exact=true" if options[:exact]
48
- response = get_request(url, headers)
48
+ response = http_request(:get, url, headers)
49
49
 
50
50
  response.kind_of?(Net::HTTPOK) ? extract_ids(response.body) : []
51
51
  end
@@ -76,7 +76,7 @@ module Dewey
76
76
  # Rewind the file in the case of multiple uploads, or conversions
77
77
  file.rewind
78
78
 
79
- response = post_request(GOOGLE_FEED_URL, file.read.to_s, headers)
79
+ response = http_request(:post, GOOGLE_FEED_URL, headers, file.read.to_s)
80
80
 
81
81
  if response.kind_of?(Net::HTTPCreated)
82
82
  extract_ids(response.body).first
@@ -85,6 +85,9 @@ module Dewey
85
85
  end
86
86
  end
87
87
 
88
+ # The same as `put`, except it will raise an exception if the request fails.
89
+ #
90
+ # @see put
88
91
  def put!(file, options = {})
89
92
  put(file, options) || raise(DeweyException, "Unable to put document")
90
93
  end
@@ -131,18 +134,20 @@ module Dewey
131
134
  unless format.empty?
132
135
  if Dewey::Validation.valid_export_format?(format, service)
133
136
  url << "&exportFormat=#{format}"
134
- url << "&format=#{format}" if service == 'document'
137
+ url << "&format=#{format}" if service == 'document'
138
+ url << "&gid=#{options[:sheet]}" if service == 'spreadsheet' && options[:sheet]
135
139
  else
136
140
  raise DeweyException, "Invalid format: #{format}"
137
141
  end
138
142
  end
139
143
 
140
- response = get_request(url, base_headers)
144
+ response = http_request(:get, url, base_headers(true, service))
141
145
 
142
146
  if response.kind_of?(Net::HTTPOK)
143
147
  file = Tempfile.new([id, format].join('.'))
144
148
  file.binmode
145
149
  file.write(response.body)
150
+ file.rewind
146
151
  file
147
152
  else
148
153
  nil
@@ -171,7 +176,7 @@ module Dewey
171
176
  url = "#{GOOGLE_FEED_URL}/#{Dewey::Utils.escape(id)}"
172
177
  url << "?delete=true" unless trash
173
178
 
174
- response = delete_request(url, headers)
179
+ response = http_request(:delete, url, headers)
175
180
 
176
181
  response.kind_of?(Net::HTTPOK)
177
182
  end
@@ -184,18 +189,6 @@ module Dewey
184
189
  delete(query, options) || raise(DeweyException, "Unable to delete document")
185
190
  end
186
191
 
187
- [:search, :put, :get, :delete].each do |method|
188
- aliased = "no_auto_authenticate_#{method.to_s}".to_sym
189
- alias_method aliased, method
190
-
191
- self.class_eval(%Q{
192
- def #{method} *args
193
- authenticate! unless authenticated?
194
- #{aliased}(*args)
195
- end
196
- })
197
- end
198
-
199
192
  # Convenience method for `put`, `get`, `delete`.
200
193
  #
201
194
  # @param [File] file The file that will be converted
@@ -219,17 +212,10 @@ module Dewey
219
212
  end
220
213
 
221
214
  protected
222
-
223
- def get_request(url, headers) #:nodoc:
224
- http_request(:get, url, headers)
225
- end
226
-
227
- def post_request(url, data, headers) #:nodoc:
228
- http_request(:post, url, headers, data)
229
- end
230
215
 
231
- def delete_request(url, headers) #:nodoc:
232
- http_request(:delete, url, headers)
216
+ # Present for simplified testing
217
+ def authenticator #:nodoc:
218
+ @@authenticator
233
219
  end
234
220
 
235
221
  def http_request(method, url, headers, data = nil) #:nodoc:
@@ -255,11 +241,11 @@ module Dewey
255
241
  #
256
242
  # @param [Boolean] form If true the Content-Type will be set accordingly
257
243
  # @return [Hash] Headers hash
258
- def base_headers(form = true) #:nodoc:
244
+ def base_headers(form = true, service = nil) #:nodoc:
259
245
  base = {}
260
246
  base['GData-Version'] = '3.0'
261
247
  base['Content-Type'] = 'application/x-www-form-urlencoded' if form
262
- base['Authorization'] = "GoogleLogin auth=#{@@authenticator.token}" if authenticated?
248
+ base['Authorization'] = "GoogleLogin auth=#{authenticator.token(service)}"
263
249
 
264
250
  base
265
251
  end
@@ -1,3 +1,3 @@
1
1
  module Dewey
2
- VERSION = '0.2.4'
2
+ VERSION = '0.2.6'
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 4
9
- version: 0.2.4
8
+ - 6
9
+ version: 0.2.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Parker Selbert
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-11-02 00:00:00 -05:00
17
+ date: 2010-11-03 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency