-
Notifications
You must be signed in to change notification settings - Fork 4
/
meetup.rb
89 lines (76 loc) · 2.47 KB
/
meetup.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
#require 'open-uri'
# driver or module for importing events from www.meetup,com
module Meetup
class << self
attr_accessor :log
# returns an array of unsaved Event objects
def get_events
@log ||= Rails.logger
@log.info "importing Meetup events"
events_data = get_events_json
events = events_data.inject([]) do |events, event_data|
adapter = Adapter.new(event_data)
events << Event.build_from(adapter)
end
@log.info "got #{events.size} Meetup events"
events
end
def get_events_json
response = open self.meetup_api_url
data = JSON.parse(response.read)
data['results']
rescue URI::InvalidURIError
@log.fatal "invalid meetup api url: #{self.meetup_api_url}"
rescue OpenURI::HTTPError
@log.fatal "unable to retreive meetup data (http error) from url #{self.meetup_api_url}"
end
def meetup_api_url
conf = Appsterdam::Application.meetup_api_options
"#{conf[:events_url]}?key=#{conf[:key]}&group_urlname=#{conf[:group_name]}"
end
end
class Adapter
# Mapping from our Event model's attributes to meetup attributes (hash keys).
# Extra mappings can be defined here. For more complex ones, you can define
# an instance method
ATTR_MAPPING = {
:name => 'name',
:description => 'description',
:url => 'url',
:lon => {'venue' => 'lon'},
:lat => {'venue' => 'lat'},
:fee => {'fee' => 'amount'},
:fee_description => {'fee' => 'description'}
}
def initialize(data)
@data = data
end
def starts_at
seconds = @data['time'] / 1000.0
Time.at(seconds)
end
# for now lets just lump venue data together as 'location'. Other APIs
# don't provide this break down (like iCal) and we don't really need such
# detailed information
def location
venue = @data['venue']
return unless venue
['name','address_1','address_2','address_3','zip','city'].collect do |key|
venue[key]
end.compact.join(", ")
end
# generate instance methods from mappings
ATTR_MAPPING.each do |native, api|
if api.is_a? Hash
key = api.keys.first
define_method native do
(@data[key] || {})[api[key]]
end
else
define_method native do
@data[api]
end
end
end
end
end