dewey 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +6 -0
- data/README.md +13 -3
- data/TODO.md +4 -3
- data/lib/dewey.rb +38 -61
- data/lib/dewey/https.rb +3 -2
- data/lib/dewey/mime.rb +2 -7
- data/lib/dewey/utils.rb +1 -1
- data/lib/dewey/validation.rb +38 -0
- metadata +4 -3
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,11 +1,21 @@
|
|
1
|
+
::::::::: :::::::::: ::: ::: :::::::::: ::: :::
|
2
|
+
:+: :+: :+: :+: :+: :+: :+: :+:
|
3
|
+
+:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+
|
4
|
+
+#+ +:+ +#++:++# +#+ +:+ +#+ +#++:++# +#++:
|
5
|
+
+#+ +#+ +#+ +#+ +#+#+ +#+ +#+ +#+
|
6
|
+
#+# #+# #+# #+#+# #+#+# #+# #+#
|
7
|
+
######### ########## ### ### ########## ###
|
8
|
+
|
1
9
|
Dewey allows you to simply upload, download and delete files from your Google
|
2
|
-
Docs account.
|
10
|
+
Docs account.
|
11
|
+
|
12
|
+
Let Google do all of the hard work of converting your documents!
|
3
13
|
|
4
|
-
|
14
|
+
## Note
|
5
15
|
|
6
16
|
Dewey is in alpha. It is not recommended you use this in production code.
|
7
17
|
|
8
|
-
##
|
18
|
+
## Getting Started
|
9
19
|
|
10
20
|
First, create a new Dewey::Document instance using your google docs account and
|
11
21
|
password.
|
data/TODO.md
CHANGED
data/lib/dewey.rb
CHANGED
@@ -6,23 +6,17 @@ require 'tempfile'
|
|
6
6
|
require 'dewey/https'
|
7
7
|
require 'dewey/mime'
|
8
8
|
require 'dewey/utils'
|
9
|
+
require 'dewey/validation'
|
9
10
|
|
10
11
|
module Dewey
|
11
|
-
GOOGLE_BASE_URL = "https://www.google.com"
|
12
12
|
GOOGLE_DOCS_URL = "https://docs.google.com"
|
13
13
|
GOOGLE_SPRD_URL = "https://spreadsheets.google.com"
|
14
|
-
GOOGLE_LOGIN_URL =
|
15
|
-
GOOGLE_FEED_URL = GOOGLE_DOCS_URL + "/feeds/default/private/full"
|
14
|
+
GOOGLE_LOGIN_URL = "https://www.google.com/accounts/ClientLogin"
|
16
15
|
|
16
|
+
GOOGLE_FEED_URL = GOOGLE_DOCS_URL + "/feeds/default/private/full"
|
17
17
|
GOOGLE_DOCUMENT_URL = GOOGLE_DOCS_URL + "/feeds/download/documents/Export"
|
18
18
|
GOOGLE_SPREADSHEET_URL = GOOGLE_SPRD_URL + "/feeds/download/spreadsheets/Export"
|
19
19
|
|
20
|
-
GOOGLE_SERVICES = { :document => 'writely', :spreadsheet => 'wise' }
|
21
|
-
|
22
|
-
DOCUMENT_EXPORT_FORMATS = %w(txt odt pdf html rtf doc png zip)
|
23
|
-
PRESENTATION_EXPORT_FORMATS = %w(swf pdf png ppt)
|
24
|
-
SPREADSHEET_EXPORT_FORMATS = %W(xls csv pdf ods tsv html)
|
25
|
-
|
26
20
|
class DeweyException < Exception
|
27
21
|
end
|
28
22
|
|
@@ -30,37 +24,7 @@ module Dewey
|
|
30
24
|
# This base class handles authentication and requests
|
31
25
|
#
|
32
26
|
class Document
|
33
|
-
attr_accessor :account, :password, :
|
34
|
-
|
35
|
-
# Determine wether or not a format is available for download.
|
36
|
-
#
|
37
|
-
# * format - The file format to check, i.e. 'txt', 'doc', 'pdf'
|
38
|
-
# * service - The service that would be used. Must be document, presentation,
|
39
|
-
# or spreadsheet.
|
40
|
-
def self.valid_upload_format?(format, service = :document)
|
41
|
-
case service
|
42
|
-
when :document then Dewey::DOCUMENT_MIMETYPES.has_key?(format.to_sym)
|
43
|
-
when :presentation then Dewey::PRESENTATION_MIMETYPES.has_key?(format.to_sym)
|
44
|
-
when :spreadsheet then Dewey::SPREADSHEET_MIMETYPES.has_key?(format.to_sym)
|
45
|
-
else
|
46
|
-
raise DeweyException, "Unknown service: #{service}"
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
# Determine whether or not a format is available for export.
|
51
|
-
#
|
52
|
-
# * format - The file format to check, i.e. 'txt', 'doc', 'pdf'
|
53
|
-
# * service - The service that would be used. Must be document, presentation,
|
54
|
-
# or spreadsheet.
|
55
|
-
def self.valid_export_format?(format, service = :document)
|
56
|
-
case service
|
57
|
-
when :document then DOCUMENT_EXPORT_FORMATS.include?(format)
|
58
|
-
when :presentation then PRESENTATION_EXPORT_FORMATS.include?(format)
|
59
|
-
when :spreadsheet then SPREADSHEET_EXPORT_FORMATS.include?(format)
|
60
|
-
else
|
61
|
-
raise DeweyException, "Unknown service: #{service}"
|
62
|
-
end
|
63
|
-
end
|
27
|
+
attr_accessor :account, :password, :token
|
64
28
|
|
65
29
|
# Create a new Doc object
|
66
30
|
# Options specified in +opts+ consist of:
|
@@ -68,12 +32,9 @@ module Dewey
|
|
68
32
|
# * :account - The Google Doc's account that will be used for authentication.
|
69
33
|
# This will most typically be a gmail account, i.e. +example@gmail.com+
|
70
34
|
# * :password - The password for the Google Doc's account.
|
71
|
-
# * :service - The default service to connect to, either :document or :spreadsheet
|
72
35
|
def initialize(options = {})
|
73
36
|
@account = options[:account]
|
74
37
|
@password = options[:password]
|
75
|
-
@service = options[:service] || :document
|
76
|
-
@cached = {}
|
77
38
|
end
|
78
39
|
|
79
40
|
# Returns true if this instance has been authorized
|
@@ -91,7 +52,7 @@ module Dewey
|
|
91
52
|
|
92
53
|
url = URI.parse(GOOGLE_LOGIN_URL)
|
93
54
|
params = { 'accountType' => 'HOSTED_OR_GOOGLE', 'Email' => @account,
|
94
|
-
'Passwd' => @password, 'service'=>
|
55
|
+
'Passwd' => @password, 'service'=> 'writely' }
|
95
56
|
|
96
57
|
response = Net::HTTPS.post_form(url, params)
|
97
58
|
|
@@ -105,8 +66,14 @@ module Dewey
|
|
105
66
|
raise DeweyException, "Unexpected response: #{response}"
|
106
67
|
end
|
107
68
|
end
|
108
|
-
|
109
|
-
|
69
|
+
|
70
|
+
# Upload a file to the account. A successful upload will return the resource
|
71
|
+
# id, which is useful for downloading the file without doing a title search.
|
72
|
+
# * file - A File reference
|
73
|
+
# * title - An alternative title, to be used instead of the filename
|
74
|
+
def put(file, title = nil)
|
75
|
+
authorize! unless authorized?
|
76
|
+
|
110
77
|
extension = File.extname(file.path).sub('.', '')
|
111
78
|
basename = File.basename(file.path, ".#{extension}")
|
112
79
|
mimetype = Dewey::Mime.mime_type(file)
|
@@ -114,12 +81,12 @@ module Dewey
|
|
114
81
|
|
115
82
|
title ||= basename
|
116
83
|
|
117
|
-
raise DeweyException, "Invalid file type: #{extension}" unless Dewey::
|
84
|
+
raise DeweyException, "Invalid file type: #{extension}" unless Dewey::Validation.valid_upload_format?(extension, service)
|
118
85
|
|
119
86
|
headers = base_headers
|
120
87
|
headers['Content-Length'] = File.size?(file).to_s
|
121
|
-
headers['Content-Type'] = mimetype
|
122
88
|
headers['Slug'] = Dewey::Utils.escape(title)
|
89
|
+
headers['Content-Type'] = mimetype unless mimetype =~ /Can't expand summary_info/
|
123
90
|
|
124
91
|
# Rewind the file in the case of multiple uploads, or conversions
|
125
92
|
file.rewind
|
@@ -128,25 +95,31 @@ module Dewey
|
|
128
95
|
|
129
96
|
case response
|
130
97
|
when Net::HTTPCreated
|
131
|
-
|
98
|
+
extract_rid(response.body)
|
132
99
|
else
|
133
100
|
nil
|
134
101
|
end
|
135
102
|
end
|
136
103
|
|
104
|
+
alias :upload :put
|
105
|
+
|
137
106
|
# Download, or export more accurately, a file to a specified format
|
138
|
-
# *
|
107
|
+
# * rid - A resource id, for example +document:12345+
|
139
108
|
# * format - The output format, see *_EXPORT_FORMATS for possiblibilites
|
140
|
-
def
|
141
|
-
|
109
|
+
def get(rid, format = nil)
|
110
|
+
authorize! unless authorized?
|
142
111
|
|
143
|
-
|
112
|
+
spreadsheet = !! rid.match(/^spreadsheet/)
|
113
|
+
id = rid.sub(/[a-z]+:/, '')
|
114
|
+
|
115
|
+
url = ''
|
116
|
+
url << (spreadsheet ? GOOGLE_SPREADSHEET_URL : GOOGLE_DOCUMENT_URL)
|
144
117
|
url << (spreadsheet ? "?key=#{id}" : "?docID=#{id}")
|
145
118
|
url << "&exportFormat=#{format.to_s}" unless format.nil?
|
146
119
|
|
147
120
|
headers = base_headers
|
148
121
|
|
149
|
-
file = Tempfile.new([
|
122
|
+
file = Tempfile.new([rid, format].join('.'))
|
150
123
|
file.binmode
|
151
124
|
|
152
125
|
open(url, headers) { |data| file.write data.read }
|
@@ -154,8 +127,13 @@ module Dewey
|
|
154
127
|
file
|
155
128
|
end
|
156
129
|
|
130
|
+
alias :download :get
|
131
|
+
|
157
132
|
# Deletes a document referenced either by resource id or by name.
|
133
|
+
# * id - A resource id or exact file name matching a document in the account
|
158
134
|
def delete(id)
|
135
|
+
authorize! unless authorized?
|
136
|
+
|
159
137
|
headers = base_headers
|
160
138
|
headers['If-Match'] = '*' # We don't care if others have modified
|
161
139
|
|
@@ -196,9 +174,8 @@ module Dewey
|
|
196
174
|
|
197
175
|
def http_request(method, url, headers, data = nil) #:nodoc:
|
198
176
|
url = URI.parse(url) if url.kind_of? String
|
199
|
-
|
200
|
-
connection = Net::HTTP.new(url.host, url.port)
|
201
|
-
connection.use_ssl = (url.scheme == 'https')
|
177
|
+
|
178
|
+
connection = (url.scheme == 'https') ? Net::HTTPS.new(url.host, url.port) : Net::HTTP.new(url.host, url.port)
|
202
179
|
|
203
180
|
case method
|
204
181
|
when :post
|
@@ -219,13 +196,13 @@ module Dewey
|
|
219
196
|
base
|
220
197
|
end
|
221
198
|
|
222
|
-
def extract_rid(
|
223
|
-
xml = REXML::Document.new(
|
199
|
+
def extract_rid(response) #:nodoc:
|
200
|
+
xml = REXML::Document.new(response)
|
224
201
|
|
225
202
|
begin
|
226
|
-
"#{$1}:#{$2}" if xml.elements['//id'].text =~ /.+(document|spreadsheet|presentation)%3A([0-9a-zA-
|
203
|
+
"#{$1}:#{$2}" if xml.elements['//id'].text =~ /.+(document|spreadsheet|presentation)%3A([0-9a-zA-Z_-]+)$/
|
227
204
|
rescue
|
228
|
-
raise DeweyException, "id could not be extracted from: #{
|
205
|
+
raise DeweyException, "id could not be extracted from: #{response}"
|
229
206
|
end
|
230
207
|
end
|
231
208
|
end
|
data/lib/dewey/https.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
require 'net/http'
|
2
2
|
|
3
|
-
|
4
|
-
module Net
|
3
|
+
module Net # :nodoc:
|
5
4
|
class HTTPS < Net::HTTP
|
6
5
|
def initialize(address, port = 443)
|
7
6
|
super(address, port)
|
8
7
|
self.use_ssl = true
|
8
|
+
@ssl_context = OpenSSL::SSL::SSLContext.new
|
9
|
+
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
9
10
|
end
|
10
11
|
end
|
11
12
|
end
|
data/lib/dewey/mime.rb
CHANGED
@@ -1,8 +1,3 @@
|
|
1
|
-
# (c) Copyright 2010 Parker Selbert <parker@sorentwo.com>
|
2
|
-
#
|
3
|
-
# Dewey is freely distributable under the terms of an MIT-style license.
|
4
|
-
# See LICENSE or http://www.opensource.org/licenses/mit-license.php
|
5
|
-
|
6
1
|
module Dewey
|
7
2
|
|
8
3
|
DOCUMENT_MIMETYPES = {
|
@@ -63,8 +58,8 @@ module Dewey
|
|
63
58
|
# cast them to one that Google Docs will accept.
|
64
59
|
def self.coerce(mime_type)
|
65
60
|
case mime_type
|
66
|
-
when /vnd.ms-office/
|
67
|
-
when /x-docx/
|
61
|
+
when /vnd.ms-office/ then 'application/msword'
|
62
|
+
when /x-docx/ then 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
68
63
|
else mime_type
|
69
64
|
end
|
70
65
|
end
|
data/lib/dewey/utils.rb
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
module Dewey
|
2
|
+
|
3
|
+
DOCUMENT_EXPORT_FORMATS = %w(txt odt pdf html rtf doc png zip)
|
4
|
+
PRESENTATION_EXPORT_FORMATS = %w(swf pdf png ppt)
|
5
|
+
SPREADSHEET_EXPORT_FORMATS = %W(xls csv pdf ods tsv html)
|
6
|
+
|
7
|
+
class Validation
|
8
|
+
# Determine wether or not a format is available for download.
|
9
|
+
#
|
10
|
+
# * format - The file format to check, i.e. 'txt', 'doc', 'pdf'
|
11
|
+
# * service - The service that would be used. Must be document, presentation,
|
12
|
+
# or spreadsheet.
|
13
|
+
def self.valid_upload_format?(format, service = :document)
|
14
|
+
case service
|
15
|
+
when :document then Dewey::DOCUMENT_MIMETYPES.has_key?(format.to_sym)
|
16
|
+
when :presentation then Dewey::PRESENTATION_MIMETYPES.has_key?(format.to_sym)
|
17
|
+
when :spreadsheet then Dewey::SPREADSHEET_MIMETYPES.has_key?(format.to_sym)
|
18
|
+
else
|
19
|
+
raise DeweyException, "Unknown service: #{service}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Determine whether or not a format is available for export.
|
24
|
+
#
|
25
|
+
# * format - The file format to check, i.e. 'txt', 'doc', 'pdf'
|
26
|
+
# * service - The service that would be used. Must be document, presentation,
|
27
|
+
# or spreadsheet.
|
28
|
+
def self.valid_export_format?(format, service = :document)
|
29
|
+
case service
|
30
|
+
when :document then Dewey::DOCUMENT_EXPORT_FORMATS.include?(format)
|
31
|
+
when :presentation then Dewey::PRESENTATION_EXPORT_FORMATS.include?(format)
|
32
|
+
when :spreadsheet then Dewey::SPREADSHEET_EXPORT_FORMATS.include?(format)
|
33
|
+
else
|
34
|
+
raise DeweyException, "Unknown service: #{service}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 4
|
9
|
+
version: 0.1.4
|
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-
|
17
|
+
date: 2010-10-19 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -43,6 +43,7 @@ files:
|
|
43
43
|
- lib/dewey/https.rb
|
44
44
|
- lib/dewey/mime.rb
|
45
45
|
- lib/dewey/utils.rb
|
46
|
+
- lib/dewey/validation.rb
|
46
47
|
- lib/dewey.rb
|
47
48
|
- README.md
|
48
49
|
- CHANGELOG.md
|