-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.rb
64 lines (54 loc) · 1.32 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
require 'sinatra'
require 'sinatra/cross_origin'
require 'sinatra/jsonp'
require 'dalli'
require 'json'
require 'twitter'
require 'newrelic_rpm'
require_relative './lib/lyrics'
require_relative './lib/twitter_searcher'
require_relative './lib/tweet_cache'
# CORS related configs
enable :cross_origin
set :allow_methods, [:get, :options]
set :allow_credentials, false
set :max_age, "1728000"
set :twitter_searcher, TwitterSearcher.new
set :tweet_cache, TweetCache.new(
::Dalli::Client.new(ENV['MEMCACHIER_SERVERS'].split(','),
username: ENV['MEMCACHIER_USERNAME'],
password: ENV['MEMCACHIER_PASSWORD'],
namespace: 'tweetflight')
)
configure :development do
# Fix thin logging
$stdout.sync = true
require 'sinatra/reloader'
set :allow_origin, '*'
end
configure :production do
set :allow_origin, 'http://tweetflight.wearebrightly.com'
end
helpers do
def twitter_searcher
settings.twitter_searcher
end
def tweet_cache
settings.tweet_cache
end
end
before do
response.headers["Access-Control-Allow-Headers"] = "x-requested-with"
end
get '/tweets.json' do
content_type :json
results = Lyrics.all.collect {|lyric|
{
id: lyric.id,
line: lyric.line,
time: lyric.time,
tweet: tweet_cache.get(lyric)
}
}
jsonp results
end