-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
64 lines (50 loc) · 1.3 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 'dotenv'
Dotenv.load
require 'sinatra'
require 'sinatra/json'
require 'newrelic_rpm'
require 'dalli'
require 'rack/cors'
require './scraper'
class ApiApp < Sinatra::Base
set :cache, Dalli::Client.new(
ENV.fetch("MEMCACHIER_SERVERS", 'localhost').split(","),
{:username => ENV["MEMCACHIER_USERNAME"],
:password => ENV["MEMCACHIER_PASSWORD"],
:failover => true,
:socket_timeout => 1.5,
:socket_failure_delay => 0.2
})
use Rack::Cors do
allow do
origins '*'
resource '/calendar.json', :headers => :any, :methods => :get
end
end
get '/' do
redirect to('/calendar.json?keyword=memphis+ruby')
end
get '/calendar.json' do
keyword = params[:keyword]
json({
"_pull_requests_appreciated" => "https://github.com/MemphisRuby/memphis_ruby_api",
"meetups" => scraper.by_keyword(keyword),
})
end
# Wildcard route that redirects to the matching meetups next event url.
get '/:meetup' do
keyword = params[:meetup]
if result = scraper.by_keyword(keyword).first
redirect to(result["event_url"])
else
redirect to('http://www.meetup.com/memphis-technology-user-groups/')
end
end
def scraper
Scraper.new(cache: cache)
end
def cache
settings.cache
end
run! if app_file == $0
end