dewey 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,15 @@
1
+ ## 0.2.1 (October 26, 2010)
2
+
3
+ Additions:
4
+
5
+ - delete! method. Raises an exception rather than returning false when
6
+ operations fail.
7
+ - search method. For retrieving one or more document:ids from a title query.
8
+
9
+ Bugfixes:
10
+
11
+ - Actually delete files rather than sending them to the trash
12
+
1
13
  ## 0.2.0 (October 20, 2010)
2
14
 
3
15
  Additions:
@@ -49,4 +61,4 @@ Bugfixes:
49
61
  Bugfixes:
50
62
 
51
63
  - Mandate ~ 1.3 version of rspec
52
- - Clear up YAML load issues
64
+ - Clear up YAML load issues
data/README.md CHANGED
@@ -39,25 +39,38 @@ Be sure to set up authorization before attempting any file operations! You don't
39
39
  need to explictely call authorize though, as it will attempt to do that on the
40
40
  first operation.
41
41
 
42
- Putting a document:
42
+ ### Putting a Document
43
43
 
44
44
  document = File.new('my_document.doc')
45
45
  resource = Dewey.put(document, 'My First Upload') # Returns the id when successful
46
46
 
47
- Getting a document:
47
+ ### Searching Documents
48
+
49
+ Exact search
50
+
51
+ id = Dewey.search('My Document', :exact => true) # 'document:12345'
52
+
53
+ Loose search
54
+
55
+ ids = Dewey.search('notes') # ['document:12', 'document:456']
56
+
57
+ ### Getting a Document
58
+
59
+ Upload your file
48
60
 
49
- # Upload your file
50
61
  id = Dewey.put(File.new('my_document.doc'), 'My Second Upload')
51
62
 
52
- # Get it in various formats
63
+ Get it in various formats
64
+
53
65
  original = Dewey.get(id) # -> Tempfile
54
66
  pdf = Dewey.get(id, :pdf) # -> Tempfile
55
67
  html = Dewey.get(id, :html) # -> Tempfile
56
68
 
57
- # A tempfile is returned, so you'll have to move it
69
+ A tempfile is returned, so you'll have to move it
70
+
58
71
  FileUtils.mv html.path, 'path/to/destination'
59
72
 
60
- Deleting a document:
73
+ ### Deleting a Document
61
74
 
62
75
  id = Dewey.put(File.new('my_spreadsheet.xls'))
63
76
  result = Dewey.delete(id) # -> true
@@ -6,6 +6,11 @@ require 'tempfile'
6
6
 
7
7
  module Dewey
8
8
  class << self
9
+
10
+ # Set the authentication strategy. You can currently only use ClientAuth,
11
+ # but you must provide +email+ and +password+ options. You must set up
12
+ # authentication before using any file operations.
13
+ #
9
14
  def authentication(strategy, options)
10
15
  case strategy
11
16
  when :client
@@ -13,18 +18,42 @@ module Dewey
13
18
  end
14
19
  end
15
20
 
21
+ # Report whether Dewey has both an authentication strategy selected, and has
22
+ # been authenticated. Delegates to the chosen authenticator.
23
+ #
16
24
  def authenticated?
17
25
  !@@authenticator.nil? && @@authenticator.authenticated?
18
26
  end
19
27
 
28
+ # Tell the authentication strategy to authenticate. Not necessary though, as
29
+ # any file operation will authenticate automatically.
30
+ #
20
31
  def authenticate!
21
32
  @@authenticator.authenticate!
22
33
  end
23
-
34
+
35
+ def search(query, options = {})
36
+ authenticate! unless authenticated?
37
+
38
+ title = query.gsub(/\s+/, '+')
39
+ headers = base_headers(false)
40
+ url = "#{GOOGLE_FEED_URL}?title=#{title}"
41
+ url << "&title-exact=true" if options[:exact]
42
+ response = get_request(url, headers)
43
+
44
+ case response
45
+ when Net::HTTPOK
46
+ extract_ids(response.body)
47
+ else
48
+ nil
49
+ end
50
+ end
51
+
24
52
  # Upload a file to the account. A successful upload will return the resource
25
53
  # id, which is useful for downloading the file without doing a title search.
26
54
  # * file - A File reference
27
55
  # * title - An alternative title, to be used instead of the filename
56
+ #
28
57
  def put(file, title = nil)
29
58
  authenticate! unless authenticated?
30
59
 
@@ -49,7 +78,7 @@ module Dewey
49
78
 
50
79
  case response
51
80
  when Net::HTTPCreated
52
- extract_rid(response.body)
81
+ extract_ids(response.body)
53
82
  else
54
83
  false
55
84
  end
@@ -58,6 +87,7 @@ module Dewey
58
87
  # Download, or export more accurately, a file to a specified format
59
88
  # * rid - A resource id, for example +document:12345+
60
89
  # * format - The output format, see *_EXPORT_FORMATS for possiblibilites
90
+ #
61
91
  def get(rid, format = nil)
62
92
  authenticate! unless authenticated?
63
93
 
@@ -79,15 +109,19 @@ module Dewey
79
109
  file
80
110
  end
81
111
 
82
- # Deletes a document referenced either by resource id or by name.
112
+ # Deletes a document referenced either by resource id.
83
113
  # * id - A resource id or exact file name matching a document in the account
114
+ #
84
115
  def delete(id)
85
116
  authenticate! unless authenticated?
86
117
 
87
- headers = base_headers
118
+ headers = base_headers(false)
88
119
  headers['If-Match'] = '*' # We don't care if others have modified
89
120
 
90
- url = GOOGLE_FEED_URL + "/#{Dewey::Utils.escape(id)}?delete=true"
121
+ url = ''
122
+ url << GOOGLE_FEED_URL
123
+ url << "/#{Dewey::Utils.escape(id)}?delete=true"
124
+
91
125
  response = delete_request(url, headers)
92
126
 
93
127
  case response
@@ -97,13 +131,20 @@ module Dewey
97
131
  false
98
132
  end
99
133
  end
134
+
135
+ # The same as delete, except that it will raise +Dewey::DeweyException+ if
136
+ # the request fails.
137
+ #
138
+ def delete!(id)
139
+ delete(id) || raise(DeweyException, "Unable to delete document")
140
+ end
100
141
 
101
142
  # Convenience method for +put+, +get+, +delete+. Returns a Tempfile
102
143
  # with in the provided type. Note that if you omit the format option it will
103
144
  # simply upload the file and return it.
104
145
  # * file - The file that will be converted
105
- # * options - Takes :title and :format. See +upload+ for title, and +download+
106
- # for format.
146
+ # * options - Takes :title and :format. See +upload+ for title, and +download+ for format.
147
+ #
107
148
  def convert(file, options = {})
108
149
  rid = put(file, options[:title])
109
150
  con = get(rid, options[:format])
@@ -113,6 +154,12 @@ module Dewey
113
154
  con
114
155
  end
115
156
 
157
+ protected
158
+
159
+ def get_request(url, headers) #:nodoc:
160
+ http_request(:get, url, headers)
161
+ end
162
+
116
163
  def post_request(url, data, headers) #:nodoc:
117
164
  http_request(:post, url, headers, data)
118
165
  end
@@ -125,33 +172,41 @@ module Dewey
125
172
  url = URI.parse(url) if url.kind_of? String
126
173
 
127
174
  connection = (url.scheme == 'https') ? Net::HTTPS.new(url.host, url.port) : Net::HTTP.new(url.host, url.port)
128
-
175
+ full_path = url.path
176
+ full_path << "?#{url.query}" unless url.query.nil?
177
+
129
178
  case method
179
+ when :get
180
+ connection.get(full_path, headers)
130
181
  when :post
131
- connection.post(url.path, data, headers)
182
+ connection.post(full_path, data, headers)
132
183
  when :delete
133
- connection.delete(url.path, headers)
184
+ connection.delete(full_path, headers)
134
185
  else
135
- raise DeweyException, "Invalid request type. Valid options are :post and :delete"
186
+ raise DeweyException, "Invalid request type. Valid options are :get, :post and :delete"
136
187
  end
137
188
  end
138
189
 
139
- def base_headers #:nodoc:
190
+ def base_headers(put_or_post = true) #:nodoc:
140
191
  base = {}
141
192
  base['GData-Version'] = '3.0'
142
- base['Content-Type'] = 'application/x-www-form-urlencoded'
193
+ base['Content-Type'] = 'application/x-www-form-urlencoded' if put_or_post
143
194
  base['Authorization'] = "GoogleLogin auth=#{@@authenticator.token}" if authenticated?
144
195
 
145
196
  base
146
197
  end
147
198
 
148
- def extract_rid(response) #:nodoc:
199
+ def extract_ids(response) #:nodoc:
149
200
  xml = REXML::Document.new(response)
150
-
151
- begin
152
- "#{$1}:#{$2}" if xml.elements['//id'].text =~ /.*(document|spreadsheet|presentation)%3A([0-9a-zA-Z_-]+)$/
153
- rescue
154
- raise DeweyException, "id could not be extracted from: #{response}"
201
+ ids = xml.elements.
202
+ collect('//id') { |e| "#{$1}:#{$2}" if e.text =~ /.*(document|spreadsheet|presentation)(?:%3A|:)([0-9a-zA-Z_-]+)$/ }.
203
+ reject(&:nil?)
204
+
205
+ case ids.length
206
+ when 0 then nil
207
+ when 1 then ids.first
208
+ else
209
+ ids
155
210
  end
156
211
  end
157
212
  end
@@ -1,3 +1,3 @@
1
1
  module Dewey
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
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-10-23 00:00:00 -05:00
17
+ date: 2010-10-26 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency