forked from jsoma/flatware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
116 lines (90 loc) · 3.37 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/db/dev.sqlite3")
class Spreadsheet
include DataMapper::Resource
property :id, Serial
property :google_key, String, :unique => true, :required => true, :format => /\A[\w\-]*\z/
def base_json_path
"/feeds/worksheets/#{google_key}/public/basic?alt=json-in-script&callback=Tabletop.singleton.loadSheets"
end
def base_json_content
@base_json_content ||= open(endpoint + base_json_path).read
end
def sheet_paths
@sheet_paths ||= @sheet_ids.map{ |sheet_id|
"/feeds/list/#{google_key}/#{sheet_id}/public/values?alt=json-in-script&sq=&callback=Tabletop.singleton.loadSheet"
}
end
def sheet_ids
@sheet_ids ||= base_json_content.scan(/\/public\/basic\/([\w\-]*)/).flatten.uniq
end
def storage
@storage ||= Fog::Storage.new({:provider => 'AWS', :aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'], :aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']})
end
def directory
storage.directories.get(ENV['AWS_BUCKET']) || storage.directories.create(:key => ENV['AWS_BUCKET'], :public => true)
end
def write(path, options = {})
cached_filename = path.gsub(/[^\w\-]/,'')
content = options[:content] || open(endpoint + path).read
# File.open("#{Sinatra::Application.settings.root}/tmp/#{cached_filename}", 'w') { |f| f.write(content) }
upload(options[:filename] || cached_filename, content)
end
def upload(filename, content)
# Using an obsolete content_type because IE8 and before chokes on application/javascript and hey, Google does it.
directory.files.create(:key => filename, :body => content, :public => true, :content_type => "text/javascript")
end
def write_content
write(base_json_path, :content => base_json_content, :filename => google_key)
sheet_ids.each do |sheet_id|
path = "/feeds/list/#{google_key}/#{sheet_id}/public/values?alt=json-in-script&sq=&callback=Tabletop.singleton.loadSheet"
write(path, :filename => "#{google_key}-#{sheet_id}")
end
rescue OpenURI::HTTPError => e
# Fail silently for the time being
end
# TODO Rename this & refactor.
def write_single_content
write(base_json_path, :content => base_json_content, :filename => google_key)
path = "/feeds/list/#{google_key}/#{sheet_ids[0]}/public/values?alt=json-in-script&sq=&callback=Tabletop.singleton.loadSheet"
write(path, :filename => "#{google_key}-#{sheet_ids[0]}")
return self
end
def endpoint
"https://spreadsheets.google.com"
end
class << self
def from_key(key)
# bug in this gsub, doesn't accept urls w/ hashes on them
clean_key = key.gsub(/.*key=(.*?)\&.*/,'\1')
Spreadsheet.new.tap do |sheet|
sheet.google_key = clean_key
end
end
end
end
DataMapper.finalize.auto_upgrade!
get '/' do
erb :index
end
post '/' do
spreadsheet = Spreadsheet.from_key(params[:key])
if spreadsheet.save
@notice = "Added spreadsheet"
else
@error = "Could not add spreadsheet"
end
erb :index
end
get '/process' do
redirect '/'
end
post '/process' do
if params[:sheet]
@updated = [Spreadsheet.first(:google_key => params[:sheet]).write_single_content]
@notice = "A spreadsheet has been updated."
else
@updated = Spreadsheet.select(&:write_content)
@notice = "Updated #{@updated.length} spreadsheets"
end
erb :index
end