holidays_from_google_calendar 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea98c95ad38a12d3c95ae95bce204eb9922fe556
4
- data.tar.gz: e04b7ada90c239f80455c9db4d2b81decc2ba8da
3
+ metadata.gz: e141f7e77b5d28a0f6286c5ce57a9e3140459d8e
4
+ data.tar.gz: c49f47556e87dc460485d09689637dbbb3cc0413
5
5
  SHA512:
6
- metadata.gz: d73607ef9df0b677747b7adae6a13efb66f3f31a4468a5c38d48cfa7af19f72694dbcd44ab2e3fd306f7c6150d722b621871eaf82d178ed8f631935f8de75b16
7
- data.tar.gz: 3db9c1c44dcf6b6f76df199c0527fc24e4099a21f11a07b41607085f469ecef86994874ff624ae1b77c2b84dd9b9c9e57d73ca577888342fe82469bc739fcbf4
6
+ metadata.gz: 18ca26424001c47596268618ab2816980f76ab07a8d4d89f86806eda50453a0df748a1567e62f63d07f5b88969f9bef9a11b762c65381a39d00e1c79f620ace9
7
+ data.tar.gz: a2769dc98cf24f2f85072e06a5e88f7e5eef5f802c39414ec671d3d51822c8fb250c3b065d7b1a7d716e07e72455686b95bf60a15a0d62cd26c95bad4bac4dd0
@@ -0,0 +1,71 @@
1
+ module HolidaysFromGoogleCalendar
2
+ class Cache
3
+ attr_reader :size
4
+
5
+ def initialize(enable: nil, max_size: nil)
6
+ @enable = enable
7
+ @max_size = max_size
8
+ @container = []
9
+ calculate_size
10
+ end
11
+
12
+ def enabled?
13
+ @enable
14
+ end
15
+
16
+ def cache(holidays, date_min, date_max)
17
+ pack_container(CacheUnit.new(holidays.dup, date_min, date_max))
18
+ page_out if calculate_size > @max_size
19
+ end
20
+
21
+ def retrieve(date_min, date_max)
22
+ unit = @container.find { |e| e.include?(date_min, date_max) }
23
+ return nil if unit.nil?
24
+
25
+ # For LRU (Least Recently Used)
26
+ @container.delete(unit)
27
+ @container.push(unit)
28
+
29
+ unit.retrieve(date_min, date_max)
30
+ end
31
+
32
+ private
33
+
34
+ def calculate_size
35
+ @size = unit_count + holidays_count
36
+ end
37
+
38
+ def unit_count
39
+ @container.size
40
+ end
41
+
42
+ def holidays_count
43
+ @container.map(&:size).sum
44
+ end
45
+
46
+ def pack_container(new_unit)
47
+ unnecessary_units = @container.reduce([]) do |array, unit|
48
+ # If there are overlapped units, combine them into the new unit
49
+ if new_unit.include?(unit.date_min, unit.date_max)
50
+ array.push(unit)
51
+ elsif new_unit.overlapped?(unit)
52
+ new_unit.combine(unit)
53
+ array.push(unit)
54
+ else
55
+ array # Do nothing
56
+ end
57
+ end
58
+ @container -= unnecessary_units
59
+ @container.push(new_unit)
60
+ end
61
+
62
+ def page_out
63
+ deleted_size = 0
64
+ while (@size - deleted_size) > @max_size
65
+ unit = @container.shift
66
+ deleted_size += (1 + unit.size) # Size is unit count plus holiday count
67
+ end
68
+ @size -= deleted_size
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,40 @@
1
+ module HolidaysFromGoogleCalendar
2
+ class CacheUnit
3
+ attr_reader :holidays, :date_min, :date_max
4
+
5
+ def initialize(holidays, date_min, date_max)
6
+ @holidays = holidays
7
+ @date_min = date_min
8
+ @date_max = date_max
9
+ end
10
+
11
+ def size
12
+ @holidays.size
13
+ end
14
+
15
+ def include?(date_min, date_max)
16
+ [date_min, date_max].all? { |e| @date_min <= e && e <= @date_max }
17
+ end
18
+
19
+ def retrieve(date_min, date_max)
20
+ @holidays.select { |e| date_min <= e.date && e.date <= date_max }
21
+ end
22
+
23
+ def overlapped?(other)
24
+ (date_min <= other.date_max && other.date_min <= date_max) ||
25
+ (other.date_min <= date_max && date_min <= other.date_max)
26
+ end
27
+
28
+ def combine(other)
29
+ return unless overlapped?(other)
30
+
31
+ if date_min <= other.date_max
32
+ date_min = other.date_min # rubocop:disable Lint/UselessAssignment
33
+ elsif other.date_min <= date_max
34
+ date_max = other.date_max # rubocop:disable Lint/UselessAssignment
35
+ end
36
+ @holidays =
37
+ @holidays.concat(other.holidays).uniq.sort { |a, b| a.date <=> b.date }
38
+ end
39
+ end
40
+ end
@@ -4,9 +4,26 @@ module HolidaysFromGoogleCalendar
4
4
  @nation = configuration.calendar[:nation]
5
5
  @language = configuration.calendar[:language]
6
6
  @api_key = configuration.credential[:api_key]
7
+ @cache = Cache.new(configuration.cache)
8
+
9
+ return unless configuration.preload[:enable]
10
+ preload(configuration.preload[:date_range])
7
11
  end
8
12
 
9
- def retrieve_from_google_calendar(date_min: nil, date_max: nil)
13
+ def retrieve(date_min: nil, date_max: nil)
14
+ if @cache.enabled?
15
+ cached_holidays = @cache.retrieve(date_min, date_max)
16
+ return cached_holidays if cached_holidays
17
+ end
18
+
19
+ retrieve_from_google_calendar(date_min, date_max).tap do |holidays|
20
+ @cache.cache(holidays, date_min, date_max) if @cache.enabled?
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def retrieve_from_google_calendar(date_min, date_max)
10
27
  service = Google::Apis::CalendarV3::CalendarService.new
11
28
  service.key = @api_key
12
29
 
@@ -20,8 +37,6 @@ module HolidaysFromGoogleCalendar
20
37
  pack_response_in_object(response)
21
38
  end
22
39
 
23
- private
24
-
25
40
  def calendar_id
26
41
  "#{@language}.#{@nation}#holiday@group.v.calendar.google.com"
27
42
  end
@@ -39,5 +54,12 @@ module HolidaysFromGoogleCalendar
39
54
  array.push(holiday)
40
55
  end
41
56
  end
57
+
58
+ def preload(date_range)
59
+ retrieve(
60
+ date_min: Date.current - date_range,
61
+ date_max: Date.current + date_range
62
+ )
63
+ end
42
64
  end
43
65
  end
@@ -1,15 +1,24 @@
1
1
  module HolidaysFromGoogleCalendar
2
2
  class Configuration
3
- CALENDAR_ATTRIBUTES = %i(nation language).freeze
4
- CREDENTIAL_ATTRIBUTES = %i(api_key).freeze
3
+ DEFAULT_CACHE_SIZE = 1_000
5
4
 
6
- attr_accessor :calendar, :credential
5
+ attr_accessor :calendar, :credential, :cache, :preload
7
6
 
8
7
  def initialize
9
8
  @calendar = {
10
9
  nation: "japanese",
11
10
  language: "ja"
12
11
  }
12
+
13
+ @cache = {
14
+ enable: true,
15
+ max_size: DEFAULT_CACHE_SIZE
16
+ }
17
+
18
+ @preload = {
19
+ enable: true, # Require cache enabled
20
+ date_range: 1.year
21
+ }
13
22
  end
14
23
  end
15
24
  end
@@ -1,3 +1,3 @@
1
1
  module HolidaysFromGoogleCalendar
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -1,12 +1,13 @@
1
- require "holidays_from_google_calendar/version"
2
- require "holidays_from_google_calendar/configuration"
3
- require "holidays_from_google_calendar/client"
4
- require "holidays_from_google_calendar/holiday"
5
-
6
- require "google/apis/calendar_v3"
7
-
8
1
  require "active_support"
9
2
  require "active_support/core_ext"
3
+ require "google/apis/calendar_v3"
4
+
5
+ require "holidays_from_google_calendar/cache"
6
+ require "holidays_from_google_calendar/cache_unit"
7
+ require "holidays_from_google_calendar/client"
8
+ require "holidays_from_google_calendar/configuration"
9
+ require "holidays_from_google_calendar/holiday"
10
+ require "holidays_from_google_calendar/version"
10
11
 
11
12
  module HolidaysFromGoogleCalendar
12
13
  class Holidays
@@ -17,14 +18,14 @@ module HolidaysFromGoogleCalendar
17
18
  end
18
19
 
19
20
  def in_year(date)
20
- @client.retrieve_from_google_calendar(
21
+ @client.retrieve(
21
22
  date_min: date.beginning_of_year,
22
23
  date_max: date.end_of_year + 1.day
23
24
  )
24
25
  end
25
26
 
26
27
  def in_month(date)
27
- @client.retrieve_from_google_calendar(
28
+ @client.retrieve(
28
29
  date_min: date.beginning_of_month,
29
30
  date_max: date.end_of_month + 1.day
30
31
  )
@@ -32,8 +33,7 @@ module HolidaysFromGoogleCalendar
32
33
 
33
34
  def holiday?(date)
34
35
  return true if date.wday.in?([0, 6]) # If Sunday or Saturday
35
- response = @client.retrieve_from_google_calendar(date_min: date, date_max: date + 1.day)
36
- response.items.size > 0
36
+ @client.retrieve(date_min: date, date_max: date + 1.day).size > 0
37
37
  end
38
38
  end
39
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: holidays_from_google_calendar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - necojackarc
@@ -100,6 +100,8 @@ files:
100
100
  - bin/setup
101
101
  - holidays_from_google_calendar.gemspec
102
102
  - lib/holidays_from_google_calendar.rb
103
+ - lib/holidays_from_google_calendar/cache.rb
104
+ - lib/holidays_from_google_calendar/cache_unit.rb
103
105
  - lib/holidays_from_google_calendar/client.rb
104
106
  - lib/holidays_from_google_calendar/configuration.rb
105
107
  - lib/holidays_from_google_calendar/holiday.rb