class HolidaysFromGoogleCalendar::Client
Public Class Methods
new(configuration)
click to toggle source
# File lib/holidays_from_google_calendar/client.rb, line 3 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
Public Instance Methods
retrieve(date_min: nil, date_max: nil)
click to toggle source
# File lib/holidays_from_google_calendar/client.rb, line 13 def retrieve(date_min: nil, date_max: nil) date_min = Date.parse(date_min.iso8601) date_max = Date.parse(date_max.iso8601) 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 Instance Methods
calendar_id()
click to toggle source
# File lib/holidays_from_google_calendar/client.rb, line 43 def calendar_id "#{@language}.#{@nation}#holiday@group.v.calendar.google.com" end
date_to_time(date)
click to toggle source
# File lib/holidays_from_google_calendar/client.rb, line 47 def date_to_time(date) Time.parse(date.iso8601).iso8601 end
pack_response_in_object(response, date_min, date_max)
click to toggle source
# File lib/holidays_from_google_calendar/client.rb, line 51 def pack_response_in_object(response, date_min, date_max) response.items.reduce([]) do |array, item| date = item.start.date holiday = Holiday.new( name: item.summary, date: date.is_a?(Date) ? date : Date.parse(date) ) if date_min <= holiday.date && holiday.date <= date_max array.push(holiday) else array # Do nothing end end end
preload(date_range)
click to toggle source
# File lib/holidays_from_google_calendar/client.rb, line 68 def preload(date_range) retrieve( date_min: Date.current - date_range, date_max: Date.current + date_range ) end
retrieve_from_google_calendar(date_min, date_max)
click to toggle source
# File lib/holidays_from_google_calendar/client.rb, line 29 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 + 1.day) ) pack_response_in_object(response, date_min, date_max) end