-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
56 lines (48 loc) · 1.56 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
require_relative './shared_config'
require 'json'
require 'sinatra'
require 'newrelic_rpm'
def allow_cors
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Methods"] = "GET, OPTIONS"
end
options '/tweets.json' do
allow_cors
halt 200
end
options '/diybiohi.json' do
allow_cors
halt 200
end
options '/diybiohi-archive.json' do
allow_cors
halt 200
end
# json endpoint for all tweets
get '/tweets.json' do
allow_cors
content_type "application/json"
tweets_collection.find.sort({tweet_id: -1}).to_a.to_json
end
# json endpoint for only #diybiohi tweets.
# TODO put these in their own collection
# TODO need to only return tweets from current postcard period
# TODO regex pattern is not efficient
# TODO need to de-dupe tweets or look for re-tweets?
# Also see
# http://docs.mongodb.org/ecosystem/tutorial/model-data-for-ruby-on-rails/
# https://github.com/mongodb/mongo-ruby-driver/wiki/Tutorial
get '/diybiohi.json' do
allow_cors
content_type "application/json"
# tweets_collection.find("text" => {"$regex" => '.*diybiohi.*'}).sort(:time => :desc).to_a.to_json
tweets_collection.find({ "text" => { "$regex" => '.*diybiohi.*'}, "time" => {"$gte" => "2013-11-01T00:00:00Z"} }).sort(:time => :desc).to_a.to_json
end
get '/diybiohi-archive.json' do
allow_cors
content_type "application/json"
tweets_collection.find({ "text" => { "$regex" => '.*diybiohi.*'}, "time" => {"$lt" => "2013-11-01T00:00:00Z"} }).sort(:time => :desc).to_a.to_json
end
get '/' do
send_file File.join(settings.public_folder, 'index.html')
end