-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.rb
82 lines (69 loc) · 1.72 KB
/
api.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
require 'sinatra'
require 'net/http'
require 'json'
require 'rack/cors'
require 'dalli'
set :cache, Dalli::Client.new(
(ENV['MEMCACHEDCLOUD_SERVERS'] || "").split(","),
{ username: ENV["MEMCACHEDCLOUD_USERNAME"],
password: ENV["MEMCACHEDCLOUD_PASSWORD"],
failover: true
}
)
BASE_URL = "https://api.meetup.com/"
API_KEY = ENV.fetch('MEETUP_API_KEY')
use Rack::Cors do |config|
config.allow do |allow|
allow.origins '*'
allow.resource '/lunches', headers: :any
end
end
get '/lunches' do
content_type :json
lunches = settings.cache.fetch('lunches', 3600) do
lunch_hashes = Meetup.events.map { |json| Event.new(json) }.map(&:to_h)
lunch_hashes.to_json
end
end
class Meetup
def self.events(group_name: 'chadevs', status: 'upcoming')
@events = get_json_data(
'2/events',
group_urlname: group_name,
status: status,
page: 100
)
end
def self.get_json_data(resource, params)
uri = initial_uri(resource, params)
json = fetch_data(uri)
json[:results]
end
def self.initial_uri(resource, params)
uri = URI("#{BASE_URL}/#{resource}")
uri.query = URI.encode_www_form(params.merge({ key: API_KEY }))
uri
end
def self.fetch_data(uri)
res = Net::HTTP.get_response(uri)
JSON.parse(res.body, symbolize_names: true)
end
end
class Event
attr_reader :id, :name, :time, :description
def initialize(json)
@event_json = json
@id = @event_json.fetch(:id)
@name = @event_json.fetch(:name)
@time = @event_json.fetch(:time)
@description = @event_json.fetch(:description) { '' }
end
def to_h
{
id: @id,
name: @name,
time: @time,
description: @description,
}
end
end