-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
147 lines (128 loc) · 4.48 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
require 'sinatra'
require 'sinatra/json'
require 'sinatra/reloader' if development?
require 'openactive'
require 'openactive/dataset_site'
get '/' do
feed_types = [
OpenActive::DatasetSite::FeedType::SESSION_SERIES,
]
settings = OpenActive::DatasetSite::Settings.new(
open_data_feed_base_url: request.base_url + "/feed/",
dataset_site_url: request.base_url,
dataset_discussion_url: "https://github.com/simpleweb/sw-oa-ruby-test-site",
dataset_documentation_url: "https://developer.openactive.io/",
dataset_languages: ["en-GB"],
organisation_name: "Simpleweb",
organisation_url: "https://simpleweb.co.uk/",
organisation_legal_entity: "Simpleweb Ltd",
organisation_plain_text_description: "Simpleweb is a purpose driven software company that specialises in new "\
"technologies, product development, and human interaction.",
organisation_logo_url: "https://simpleweb.co.uk/wp-content/uploads/2015/07/facebook-default.png",
organisation_email: "spam@simpleweb.co.uk",
background_image_url: "https://simpleweb.co.uk/wp-content/uploads/2017/06/IMG_8994-500x500-c-default.jpg",
date_first_published: "2019-11-05", # remember, remember the fifth of November...
data_feed_types: feed_types,
)
renderer = OpenActive::DatasetSite::TemplateRenderer.new(settings)
renderer.render
end
disable :strict_paths
get '/feed/session-series' do
base_url = request.base_url+request.path
change_number = params['afterChangeNumber'].to_i || 0
per_page = 3
page_items_data = data_for_page(change_number, per_page).map do |raw_item|
if raw_item.has_key?('data')
raw_item['data'] = build_session_series(raw_item['data'])
end
raw_item
end
page_items = page_items_data.map do |raw_item|
args = {
Id: raw_item["id"],
State: raw_item["state"],
Kind: raw_item["kind"],
Modified: raw_item["modified"],
}
args[:Data] = raw_item["data"] if args[:State] == "updated"
OpenActive::Rpde::RpdeItem.new(**args)
end
# page = RpdeBody::createFromNextChangeNumber($baseUrl, $changeNumber, pageItems);
page = OpenActive::Rpde::RpdeBody.create_from_next_change_number(
base_url, change_number, page_items)
json page
end
private
def build_session_series(data)
address = OpenActive::Models::PostalAddress.new(
street_address: data['location']['address']['streetAddress'],
address_locality: data['location']['address']['addressLocality'],
address_region: data['location']['address']['addressRegion'],
postal_code: data['location']['address']['postalCode'],
address_country: data['location']['address']['addressCountry'],
)
location = OpenActive::Models::Place.new(
id: data['location']['id'],
name: data['location']['name'],
address: address,
)
activities = data['activity'].map do |activity_data|
OpenActive::Models::Concept.new(
id: activity_data['id'],
in_scheme: activity_data['inScheme'],
pref_label: activity_data['prefLabel']
)
end
event_schedule = data['eventSchedule'].map do |event_schedule_data|
OpenActive::Models::PartialSchedule.new(
repeat_frequency: event_schedule_data['repeatFrequency'],
start_time: event_schedule_data['startTime'],
end_time: event_schedule_data['endTime'],
by_day: event_schedule_data['byDay'],
# by_day: event_schedule_data['byDay'].map { |d| DayOfWeek.find_by_value(d) },
)
end
organizer = OpenActive::Models::Organization.new(
id: data['organizer']['id'],
name: data['organizer']['name'],
)
offers = data['offers'].map do |offer_data|
puts offer_data
OpenActive::Models::Offer.new(
id: offer_data['id'],
price: offer_data['price'],
price_currency: offer_data['price_currency'],
)
end
OpenActive::Models::SessionSeries.new(
name: data['name'],
start_date: data['startDate'],
end_date: data['endDate'],
duration: data['duration'],
location: location,
activity: activities,
event_schedule: event_schedule,
organizer: organizer,
offers: offers,
)
end
def data_for_page(change_number, per_page)
all_data
.select { |item| item["modified"] > change_number }
.sort do |item1, item2|
if item1["modified"] == item2["modified"]
item1["id"] <=> item2["id"]
else
item1["modified"] <=> item2["modified"]
end
end
.take(per_page)
end
def all_data
JSON.parse(
File.read(
File.join(__dir__, 'session-series-feed-items.json')
)
)
end