Class: Candl::EventLoaderModel
- Inherits:
-
Object
- Object
- Candl::EventLoaderModel
- Defined in:
- lib/candl/event_loader_model.rb
Class Method Summary collapse
-
.build_google_request_path(google_calendar_base_path, calendar_id, api_key, from, to) ⇒ Object
build request path to calendar host (google calendar).
- .Event(dtstart, dtend, summary, description, location, uid) ⇒ Object
-
.get_agenda_events(google_calendar_base_path, calendar_id, api_key, from, to) ⇒ Object
load events prepared for agenda view.
-
.get_month_events(google_calendar_base_path, calendar_id, api_key, from, to) ⇒ Object
load events for month view.
-
.parse_calendar(google_calendar_base_path, calendar_id, api_key, from, to) ⇒ Object
parses json response form calendar host (google calendar).
-
.spread_multiday_events(events, from, to) ⇒ Object
inserts new event starts for events that are multiple day’s long so in the agenda one can see them filling multiple day’s.
Class Method Details
.build_google_request_path(google_calendar_base_path, calendar_id, api_key, from, to) ⇒ Object
build request path to calendar host (google calendar)
33 34 35 |
# File 'lib/candl/event_loader_model.rb', line 33 def self.build_google_request_path(google_calendar_base_path, calendar_id, api_key, from, to) google_test_path = "#{google_calendar_base_path}#{calendar_id}/events?key=#{api_key}&singleEvents=true&orderBy=startTime&timeMin=#{CGI.escape(from.to_s)}&timeMax=#{CGI.escape(to.to_s)}" end |
.Event(dtstart, dtend, summary, description, location, uid) ⇒ Object
7 8 9 |
# File 'lib/candl/event_loader_model.rb', line 7 def self.Event(dtstart, dtend, summary, description, location, uid) Event.new(:dtstart, :dtend, :summary, :description, :location, :uid) end |
.get_agenda_events(google_calendar_base_path, calendar_id, api_key, from, to) ⇒ Object
load events prepared for agenda view
12 13 14 15 16 17 18 19 |
# File 'lib/candl/event_loader_model.rb', line 12 def self.get_agenda_events(google_calendar_base_path, calendar_id, api_key, from, to) events = parse_calendar(google_calendar_base_path, calendar_id, api_key, from, to) spreaded_events = spread_multiday_events(events, from, to) sorted_events = (events + spreaded_events.to_a).sort_by do |el| [el.dtstart, el.summary] end end |
.get_month_events(google_calendar_base_path, calendar_id, api_key, from, to) ⇒ Object
load events for month view
22 23 24 25 26 27 28 |
# File 'lib/candl/event_loader_model.rb', line 22 def self.get_month_events(google_calendar_base_path, calendar_id, api_key, from, to) events = parse_calendar(google_calendar_base_path, calendar_id, api_key, from, to) sorted_events = (events).sort_by do |el| [el.dtstart, el.summary] end end |
.parse_calendar(google_calendar_base_path, calendar_id, api_key, from, to) ⇒ Object
parses json response form calendar host (google calendar)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/candl/event_loader_model.rb', line 38 def self.parse_calendar(google_calendar_base_path, calendar_id, api_key, from, to) google_test_path = build_google_request_path(google_calendar_base_path, calendar_id, api_key, from.to_datetime, to.to_datetime) requested_events = JSON.parse(Net::HTTP.get(URI.parse(google_test_path))) if requested_events["items"] != nil restructured_events = requested_events["items"].map{ |e| e["start"]["dateTime"] != nil ? Event.new(DateTime.parse(e["start"]["dateTime"]), DateTime.parse(e["end"]["dateTime"]), e["summary"], e["description"], e["location"], e["id"]) : Event.new(Date.parse(e["start"]["date"]), Date.parse(e["end"]["date"]), e["summary"], e["description"], e["location"], e["id"]) } else raise "Calendar event request failed and responded with:\n #{requested_events}" end restructured_events.to_a end |
.spread_multiday_events(events, from, to) ⇒ Object
inserts new event starts for events that are multiple day’s long so in the agenda one can see them filling multiple day’s
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/candl/event_loader_model.rb', line 54 def self.spread_multiday_events(events, from, to) unspreaded_events = events.select{ |event| (event.dtend - event.dtstart).to_i > 0 } spreaded_events = unspreaded_events.map do |event| ([from, (event.dtstart + 1.day)].max .. [(event.dtend - 1.day), to].min).to_a.map do |date| Event.new.tap do |e| e.dtstart = date.to_date e.dtend = event.dtend.to_date e.summary = event.summary e.location = event.location e.description = event.description e.uid = event.uid end end end.flatten! end |