cal4near 0.1.3 → 0.2.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
  SHA256:
3
- metadata.gz: d373d36ab9313eff826c23336f0961e57da58db8ee897dea9aeabcf35fca3182
4
- data.tar.gz: '068866cddf8ea18cb5a64923d51853bc25d7648c652ff8c12dbc6da68b6c67b5'
3
+ metadata.gz: 962a769e590ce2126052259f29cb6388e0437a3bb7710d6af2352ba9390b9ba7
4
+ data.tar.gz: e6f4a4aef668039638293302e4fa8be1d2e8774fe9640dfcb4c158fae7175999
5
5
  SHA512:
6
- metadata.gz: 6be5cc5c52bd6aaa0369080d47b1b87fc968ae1904e7ce8cbf055e0ceaf3fedd80fd05f04d0870c8116cd0278e80ab130d584ced9d6b28956e534b557706c765
7
- data.tar.gz: d800efc818d1c8476521ec9615a154d7a304336f3cbf515f641c374ebd1d69f8cf943e2fa55ad54bff5f7dc458feb0c1232afd739bdf8dcfbf86153e380d291e
6
+ metadata.gz: 7c410224e01c9ada37128b56e5f6535d26fafe12099edccd8c76266e706020aa82b3f4c923950e05de7abef4e02104c67294e74dd516b5bc1ee8f60abd9e9e40
7
+ data.tar.gz: a33c7228f8b45b1d9d85427a7598ad1325c1c6ce07c8c666562c1c912e38544e11033b9e7b4c6ad7e1a09f346c74152dd5637e42be7a8283823de9a276f99e1a
data/exe/cal4 CHANGED
@@ -3,12 +3,12 @@ require "bundler/setup"
3
3
  require "cal4near"
4
4
 
5
5
  HELP_MSG =<<-MSG
6
- usage: cal4 [free|busy] [-h]
6
+ usage: cal4 [ free, f | busy, b ] [ -h ]
7
7
 
8
- free : Stdout free times from calendar api
9
- busy : Stdout busy times from calendar api [WIP]
8
+ free, f : Stdout free times from Google Calendar api
9
+ busy, b : Stdout busy times from Google Calendar api
10
10
 
11
- -h : show help
11
+ -h : Show help
12
12
  MSG
13
13
 
14
14
  WRONG_ARG_ERR_MSG =<<-MSG
@@ -22,11 +22,12 @@ if ARGV.length > 0
22
22
  return
23
23
  when 'free', 'f'
24
24
  msg = "free times"
25
- times_info = Cal4near.free_times
26
- # when 'busy', 'b'
27
- # TODO:busy_timesの実装
28
- # msg = "busy times"
29
- # times_info = Cal4near.busy_times
25
+ times_info = Cal4near.free_busy_times
26
+ is_free = true
27
+ when 'busy', 'b'
28
+ msg = "busy times"
29
+ times_info = Cal4near.free_busy_times
30
+ is_free = false
30
31
  else
31
32
  puts WRONG_ARG_ERR_MSG
32
33
  puts HELP_MSG
@@ -39,9 +40,9 @@ else
39
40
  end
40
41
 
41
42
  puts <<-MSG
42
- --------------
43
- #{msg}
44
- --------------
43
+ ----------------------------
44
+ #{msg} (9:00 - 19:00)
45
+ ----------------------------
45
46
  MSG
46
47
 
47
- Cal4near.stdout(times_info)
48
+ puts Cal4near.format(times_info, is_free)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cal4near
4
- VERSION = "0.1.3"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/cal4near.rb CHANGED
@@ -6,8 +6,6 @@ require "googleauth"
6
6
  require "googleauth/stores/file_token_store"
7
7
  require "date"
8
8
  require "fileutils"
9
- # require 'pry'
10
- # require 'pry-byebug'
11
9
 
12
10
  module Cal4near
13
11
  class Error < StandardError; end
@@ -22,7 +20,7 @@ module Cal4near
22
20
  SCOPE = Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY
23
21
 
24
22
  # 空き時間を検索する日時の範囲
25
- START_DATE = DateTime.now
23
+ START_DATE = DateTime.now.next_day(1)
26
24
  END_DATE = DateTime.now.next_day(30)
27
25
 
28
26
  # 空き時間を検索する時間の範囲
@@ -53,11 +51,12 @@ module Cal4near
53
51
  # @return [Hash]
54
52
  # @example 返り値のサンプルは以下
55
53
  # "2022-03-21"=> {"2022-03-21 09:00"=>{:free=>true}
56
- def self.free_times(
54
+ def self.free_busy_times(
57
55
  start_date = START_DATE,
58
56
  end_date = END_DATE,
59
57
  start_hour = START_HOUR,
60
- end_hour = END_HOUR
58
+ end_hour = END_HOUR,
59
+ max_date_count = 100
61
60
  )
62
61
  busy_list = []
63
62
  events(start_date, end_date).each do |event|
@@ -78,7 +77,9 @@ module Cal4near
78
77
  # puts "Free time:"
79
78
 
80
79
  result = {}
81
- (start_date.to_date..end_date.to_date).each do |date|
80
+ (start_date.to_date..end_date.to_date).each.with_index(1) do |date, i|
81
+ break if i > max_date_count
82
+
82
83
  result[date.strftime(DATE_FORMAT)] ||= {} # YYYY-MM-DD
83
84
 
84
85
  start_work_time = Time.new(date.year, date.month, date.day, start_hour, 0, 0)
@@ -107,19 +108,19 @@ module Cal4near
107
108
  result
108
109
  end
109
110
 
110
- # @param [Hash] free_times
111
- def self.stdout(times_info)
111
+ # @param [Hash] free_busy_times
112
+ def self.format(times_info, is_free = true)
112
113
  result = times_info
113
114
  wdays = %w(日 月 火 水 木 金 土)
114
115
  # 出力
115
- result.each do |date, times|
116
+ date_time_info = result.map do |date, times|
116
117
  min_time = max_time = nil
117
118
  spans = []
118
119
  times.each do |time, info|
119
120
  time = DateTime.parse(time)
120
121
  min_time ||= time
121
122
  max_time = time
122
- if info[:free]
123
+ if (is_free && info[:free]) || (!is_free && !info[:free])
123
124
  next
124
125
  else
125
126
  if min_time && max_time && min_time < max_time
@@ -134,8 +135,11 @@ module Cal4near
134
135
  end
135
136
 
136
137
  tmp_date = Date.parse(date)
137
- puts "#{tmp_date.strftime("%Y/%m/%d")}(#{wdays[tmp_date.wday]}) #{spans.join(", ")}"
138
+ spans_text = spans.empty? ? "" : " #{spans.join(", ")}"
139
+ "#{tmp_date.strftime("%Y/%m/%d")}(#{wdays[tmp_date.wday]})#{spans_text}"
138
140
  end
141
+
142
+ date_time_info.join("\n")
139
143
  end
140
144
 
141
145
  ##
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cal4near
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nakamaksk