holidays_from_google_calendar 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f7ee8d89b1e78190b0aaab5b0f4ada3c80ca3f44
4
- data.tar.gz: 9a8cb530a7cea9a3227fbe8860d9272f402cc7cd
3
+ metadata.gz: ddd84d5bf6b28a28dcbfbd7cdad2574e50d1d17f
4
+ data.tar.gz: 3a9f0c5359768ce0ca1f136090c343b7e036e482
5
5
  SHA512:
6
- metadata.gz: e31a52324f26ddb22915a9bed9f13bc8a9a57ffac561e1dfb36c952738e61a98ccdc15db0fbcfcad24a6b914300fc1fded12be075f4d3cbc13578a5215be452c
7
- data.tar.gz: e4cce20d7cb978354f324729827ea5b4deead9bb96d0dc823a5a31047a74eead0588a60693249293127abf331d497a9429585c1ffb64733abe750e3cfb278237
6
+ metadata.gz: 43dca74914287410f8715ce941a2efb1f5ede23a06b5ca25b7d35e7db6ff8a3132d9204e83177c3a0420da78803a0425e49220a52ad65d13c6b192de4c7178a5
7
+ data.tar.gz: 78dd94477bb25dd7ab5530d7dc2f6aa4e8234d3c82f354dbcb64a576d9f944b9320f29ed0bc56a30ae23225246f82bd24fcb1909680613fcde0216614538ef61
@@ -1,4 +1,7 @@
1
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"
2
5
 
3
6
  require "google/apis/calendar_v3"
4
7
 
@@ -7,55 +10,45 @@ require "active_support/core_ext"
7
10
 
8
11
  module HolidaysFromGoogleCalendar
9
12
  class Holidays
10
- Holiday = Struct.new(:name, :date)
11
-
12
- def initialize(nation: "usa", language: "en", api_key: nil)
13
- @nation = nation
14
- @language = language
15
- @api_key = api_key
16
- end
17
-
18
- def in_year(date)
19
- response = retrieve_from_google_calendar(
20
- date_min: date.beginning_of_year,
21
- date_max: date.end_of_year
22
- )
23
- pack_response_in_struct(response)
24
- end
13
+ class << self
14
+ def configure
15
+ @configuration = Configuration.new
16
+ yield @configuration
17
+ @client = Client.new(@configuration.to_h)
18
+ end
25
19
 
26
- def in_month(date)
27
- response = retrieve_from_google_calendar(
28
- date_min: date.beginning_of_month,
29
- date_max: date.end_of_month
30
- )
31
- pack_response_in_struct(response)
32
- end
20
+ def in_year(date)
21
+ response = @client.retrieve_from_google_calendar(
22
+ date_min: date.beginning_of_year,
23
+ date_max: date.end_of_year + 1.day
24
+ )
25
+ pack_response_in_struct(response)
26
+ end
33
27
 
34
- private
35
-
36
- def retrieve_from_google_calendar(date_min: nil, date_max: nil)
37
- service = Google::Apis::CalendarV3::CalendarService.new
38
- service.key = @api_key
39
- service.list_events(
40
- calendar_id,
41
- single_events: true,
42
- order_by: "startTime",
43
- time_min: date_to_time(date_min),
44
- time_max: date_to_time(date_max)
45
- )
46
- end
28
+ def in_month(date)
29
+ response = @client.retrieve_from_google_calendar(
30
+ date_min: date.beginning_of_month,
31
+ date_max: date.end_of_month + 1.day
32
+ )
33
+ pack_response_in_struct(response)
34
+ end
47
35
 
48
- def calendar_id
49
- "#{@language}.#{@nation}#holiday@group.v.calendar.google.com"
50
- end
36
+ def holiday?(date)
37
+ return true if date.wday.in?([0, 6]) # If Sunday or Saturday
38
+ response = @client.retrieve_from_google_calendar(date_min: date, date_max: date + 1.day)
39
+ response.items.size > 0
40
+ end
51
41
 
52
- def date_to_time(date)
53
- Time.parse(date.iso8601).iso8601
54
- end
42
+ private
55
43
 
56
- def pack_response_in_struct(response)
57
- response.items.reduce([]) do |array, item|
58
- array.push(Holiday.new(item.summary, Date.parse(item.start.date)))
44
+ def pack_response_in_struct(response)
45
+ response.items.reduce([]) do |array, item|
46
+ holiday = Holiday.new(
47
+ name: item.summary,
48
+ date: Date.parse(item.start.date)
49
+ )
50
+ array.push(holiday)
51
+ end
59
52
  end
60
53
  end
61
54
  end
@@ -0,0 +1,31 @@
1
+ module HolidaysFromGoogleCalendar
2
+ class Client
3
+ def initialize(nation: "usa", language: "en", api_key: nil)
4
+ @nation = nation
5
+ @language = language
6
+ @api_key = api_key
7
+ end
8
+
9
+ def retrieve_from_google_calendar(date_min: nil, date_max: nil)
10
+ service = Google::Apis::CalendarV3::CalendarService.new
11
+ service.key = @api_key
12
+ service.list_events(
13
+ calendar_id,
14
+ single_events: true,
15
+ order_by: "startTime",
16
+ time_min: date_to_time(date_min),
17
+ time_max: date_to_time(date_max)
18
+ )
19
+ end
20
+
21
+ private
22
+
23
+ def calendar_id
24
+ "#{@language}.#{@nation}#holiday@group.v.calendar.google.com"
25
+ end
26
+
27
+ def date_to_time(date)
28
+ Time.parse(date.iso8601).iso8601
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,13 @@
1
+ module HolidaysFromGoogleCalendar
2
+ class Configuration
3
+ ATTRIBUTES = %i(nation language api_key)
4
+
5
+ attr_accessor(*ATTRIBUTES)
6
+
7
+ def to_h
8
+ ATTRIBUTES.reduce({}) do |hash, attribute|
9
+ hash.merge(attribute => public_send(attribute))
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module HolidaysFromGoogleCalendar
2
+ class Holiday
3
+ attr_reader :name, :date
4
+
5
+ def initialize(name: nil, date: nil)
6
+ @name = name
7
+ @date = date
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module HolidaysFromGoogleCalendar
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  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.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - necojackarc
@@ -99,6 +99,9 @@ files:
99
99
  - bin/setup
100
100
  - holidays_from_google_calendar.gemspec
101
101
  - lib/holidays_from_google_calendar.rb
102
+ - lib/holidays_from_google_calendar/client.rb
103
+ - lib/holidays_from_google_calendar/configuration.rb
104
+ - lib/holidays_from_google_calendar/holiday.rb
102
105
  - lib/holidays_from_google_calendar/version.rb
103
106
  homepage: https://github.com/necojackarc/holidays_from_google_calendar
104
107
  licenses: