Sha256: 7b958fd4b7f1a5bbe4283ca659a0120c97ea895d19b461e8db92bd79c59ebde8
Contents?: true
Size: 1.71 KB
Versions: 2
Compression:
Stored size: 1.71 KB
Contents
module HolidaysFromGoogleCalendar class Client def initialize(configuration) @nation = configuration.calendar[:nation] @language = configuration.calendar[:language] @api_key = configuration.credential[:api_key] @cache = Cache.new(configuration.cache) return unless configuration.preload[:enable] preload(configuration.preload[:date_range]) end def retrieve(date_min: nil, date_max: nil) if @cache.enabled? cached_holidays = @cache.retrieve(date_min, date_max) return cached_holidays if cached_holidays end retrieve_from_google_calendar(date_min, date_max).tap do |holidays| @cache.cache(holidays, date_min, date_max) if @cache.enabled? end end private def retrieve_from_google_calendar(date_min, date_max) service = Google::Apis::CalendarV3::CalendarService.new service.key = @api_key response = service.list_events( calendar_id, single_events: true, order_by: "startTime", time_min: date_to_time(date_min), time_max: date_to_time(date_max) ) pack_response_in_object(response) end def calendar_id "#{@language}.#{@nation}#holiday@group.v.calendar.google.com" end def date_to_time(date) Time.parse(date.iso8601).iso8601 end def pack_response_in_object(response) response.items.reduce([]) do |array, item| holiday = Holiday.new( name: item.summary, date: Date.parse(item.start.date) ) array.push(holiday) end end def preload(date_range) retrieve( date_min: Date.current - date_range, date_max: Date.current + date_range ) end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
holidays_from_google_calendar-0.4.1 | lib/holidays_from_google_calendar/client.rb |
holidays_from_google_calendar-0.4.0 | lib/holidays_from_google_calendar/client.rb |