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 +4 -4
- data/lib/holidays_from_google_calendar.rb +37 -44
- data/lib/holidays_from_google_calendar/client.rb +31 -0
- data/lib/holidays_from_google_calendar/configuration.rb +13 -0
- data/lib/holidays_from_google_calendar/holiday.rb +10 -0
- data/lib/holidays_from_google_calendar/version.rb +1 -1
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ddd84d5bf6b28a28dcbfbd7cdad2574e50d1d17f
|
4
|
+
data.tar.gz: 3a9f0c5359768ce0ca1f136090c343b7e036e482
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
-
|
49
|
-
|
50
|
-
|
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
|
-
|
53
|
-
Time.parse(date.iso8601).iso8601
|
54
|
-
end
|
42
|
+
private
|
55
43
|
|
56
|
-
|
57
|
-
|
58
|
-
|
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
|
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.
|
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:
|