dewey 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## 0.2.3 (October 30, 2010)
2
+
3
+ Additions:
4
+
5
+ - #delete and #delete! accept an optional :trash option to send a resource
6
+ to the trash, rather than being fully deleted.
7
+ - #get and #elete by title. Handles exact matches only.
8
+
9
+ Bugfixes:
10
+
11
+ - Not setting the :format option on #get no longers raises
12
+
1
13
  ## 0.2.2 (October 27, 2010)
2
14
 
3
15
  Changes:
data/README.md CHANGED
@@ -42,35 +42,55 @@ first operation.
42
42
  ### Putting a Document
43
43
 
44
44
  document = File.new('my_document.doc')
45
- resource = Dewey.put(document, :title => 'My First Upload') # Returns the id when successful
45
+ resource = Dewey.put(document, :title => 'My First Upload') #=> 'document:12345'
46
46
 
47
47
  ### Searching Documents
48
48
 
49
49
  Exact search
50
50
 
51
- id = Dewey.search('My Document', :exact => true) # 'document:12345'
51
+ Dewey.search('My Document', :exact => true) #=> ['document:12345']
52
52
 
53
53
  Loose search
54
54
 
55
- ids = Dewey.search('notes') # ['document:12', 'document:456']
55
+ ids = Dewey.search('notes') #=> ['document:12', 'document:456']
56
56
 
57
57
  ### Getting a Document
58
58
 
59
59
  Upload your file
60
60
 
61
61
  id = Dewey.put(File.new('my_document.doc'), 'My Second Upload')
62
-
62
+
63
63
  Get it in various formats
64
64
 
65
65
  original = Dewey.get(id) #=> Tempfile
66
66
  pdf = Dewey.get(id, :format => :pdf) #=> Tempfile
67
67
  html = Dewey.get(id, :format => :html) #=> Tempfile
68
-
68
+
69
69
  A tempfile is returned, so you'll have to move it
70
70
 
71
71
  FileUtils.mv html.path, 'path/to/destination'
72
72
 
73
+ Getting a document by title. Unmatched searches return nil
74
+
75
+ Dewey.get('My Document') #=> Tempfile
76
+ Dewey.get('No Match') #=> nil
77
+
73
78
  ### Deleting a Document
74
79
 
80
+ Deleting a document from a resource id
81
+
75
82
  id = Dewey.put(File.new('my_spreadsheet.xls'))
76
- result = Dewey.delete(id) # -> true
83
+ Dewey.delete(id) #=> true
84
+
85
+ Deleting by title. Unmatched searches return false
86
+
87
+ Dewey.delete('My Document') #=> true
88
+ Dewey.delete('No Match') #=> false
89
+
90
+ Sending to the trash rather than deleting
91
+
92
+ Dewey.delete('My Document', :trash => true) #=> true
93
+
94
+ If you would prefer an error when deletion fails
95
+
96
+ Dewey.delete!('My Document') #=> raise DeweyException
data/lib/dewey/core.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'uri'
2
2
  require 'net/https'
3
- require 'open-uri'
4
3
  require 'rexml/document'
5
4
  require 'tempfile'
6
5
 
@@ -88,22 +87,26 @@ module Dewey
88
87
 
89
88
  # Download a file. You may optionally specify a format for export.
90
89
  #
91
- # @param [String] id A resource id, for example `document:12345`
90
+ # @param [String] query A resource id or title, `document:12345` or
91
+ # `My Document` for example
92
92
  # @param [Hash] options Options for downloading the document
93
93
  # @option options [Symbol] :format The output format
94
94
  #
95
- # @return [Tempfile] The downloaded file
95
+ # @return [Tempfile, nil] The downloaded file, otherwise `nil` if the file
96
+ # couldn't be found.
96
97
  #
97
98
  # @see Dewey::Validation::DOCUMENT_EXPORT_FORMATS
98
99
  # @see Dewey::Validation::SPREADSHEET_EXPORT_FORMATS
99
100
  # @see Dewey::Validation::PRESENTATION_EXPORT_FORMATS
100
- def get(id, options = {})
101
- service, id = id.split(':')
102
- format = options[:format].to_s
101
+ def get(query, options = {})
102
+ resource_id = is_id?(query) ? query : search(query, :exact => true).first
103
103
 
104
- raise DeweyException, "Invalid format: #{format}" unless Dewey::Validation.valid_export_format?(format, service)
104
+ return nil if resource_id.nil?
105
+
106
+ service, id = resource_id.split(':')
107
+ format = options[:format].to_s
108
+ url = ''
105
109
 
106
- url = ''
107
110
  case service
108
111
  when 'document'
109
112
  url << GOOGLE_DOCUMENT_URL
@@ -113,28 +116,47 @@ module Dewey
113
116
  url << "?key=#{id}"
114
117
  end
115
118
 
116
- url << "&exportFormat=#{format}" unless format.blank?
117
-
118
- file = Tempfile.new([id, format].join('.'))
119
- file.binmode
120
-
121
- open(url, base_headers) { |data| file.write data.read }
122
-
123
- file
119
+ unless format.blank?
120
+ if Dewey::Validation.valid_export_format?(format, service)
121
+ url << "&exportFormat=#{format}" unless format.blank?
122
+ else
123
+ raise DeweyException, "Invalid format: #{format}"
124
+ end
125
+ end
126
+
127
+ response = get_request(url, base_headers)
128
+
129
+ if response.kind_of?(Net::HTTPOK)
130
+ file = Tempfile.new([id, format].join('.'))
131
+ file.binmode
132
+ file.write(response.body)
133
+ file
134
+ else
135
+ nil
136
+ end
124
137
  end
125
138
 
126
- # Deletes a document referenced by id.
139
+ # Deletes a document. The default behavior is to delete the document
140
+ # permanently, rather than trashing it.
127
141
  #
128
- # @param [String] id An id matching a document
142
+ # @param [String] query A resource id or title. If a title is provided
143
+ # a search will be performed automatically.
144
+ # @param [Hash] options Options for deleting the document
145
+ # @option options [Symbol] :trash If `true` the resource will be sent to
146
+ # the trash, rather than being permanently deleted.
129
147
  #
130
148
  # @return [Boolean] `true` if delete was successful, `false` otherwise
131
- def delete(id)
132
- headers = base_headers(false)
133
- headers['If-Match'] = '*' # We don't care if others have modified
134
-
135
- url = ''
136
- url << GOOGLE_FEED_URL
137
- url << "/#{Dewey::Utils.escape(id)}?delete=true"
149
+ def delete(query, options = {})
150
+
151
+ # We use 'If-Match' => '*' to make sure we delete regardless of others
152
+ headers = base_headers(false).merge({'If-Match' => '*'})
153
+ trash = options.delete(:trash) || false
154
+ id = (is_id?(query)) ? query : search(query, :exact => true).first
155
+
156
+ return false if id.nil?
157
+
158
+ url = "#{GOOGLE_FEED_URL}/#{Dewey::Utils.escape(id)}"
159
+ url << "?delete=true" unless trash
138
160
 
139
161
  response = delete_request(url, headers)
140
162
 
@@ -145,8 +167,8 @@ module Dewey
145
167
  # the request fails.
146
168
  #
147
169
  # @see #delete
148
- def delete!(id)
149
- delete(id) || raise(DeweyException, "Unable to delete document")
170
+ def delete!(query, options = {})
171
+ delete(query, options) || raise(DeweyException, "Unable to delete document")
150
172
  end
151
173
 
152
174
  [:search, :put, :get, :delete].each do |method|
@@ -237,5 +259,10 @@ module Dewey
237
259
  xml = REXML::Document.new(response)
238
260
  xml.elements.collect('//id') { |e| e.text.gsub('%3A', ':') }.reject(&:blank?)
239
261
  end
262
+
263
+ # Is the string an id or a search query?
264
+ def is_id?(string)
265
+ string.match(/^(doc|spr|pres).+:.+$/)
266
+ end
240
267
  end
241
268
  end
data/lib/dewey/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dewey
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 2
9
- version: 0.2.2
8
+ - 3
9
+ version: 0.2.3
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-28 00:00:00 -05:00
17
+ date: 2010-10-30 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency