cal4near 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 872a62fa1f557fdf85235824334bdff98fa6d2b64205f846a3d803024d0f11fc
4
+ data.tar.gz: e8ebfc5bc675c8116288f23d89c7517946aca6be1c87d9f4441e4888ea1d52fe
5
+ SHA512:
6
+ metadata.gz: 9fbcc0c20819f60918457dc4592e547e794edb49b398127a20ac8de1409c80d74be82d77dc4e4b9b241dd9575b1394ebc76bb63459c1ec864a4dd77b25f62e55
7
+ data.tar.gz: 410751d59e27aee97f4634d8043e4d152d2743b826b5c5bafe97c46df7c9ef250aeeb104226277cd16365a672c5091d3f7060e2fe56ac1c104a4b195c986bf10
@@ -0,0 +1,38 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ master ]
13
+ pull_request:
14
+ branches: [ master ]
15
+
16
+ env:
17
+ TZ: 'Asia/Tokyo'
18
+
19
+ jobs:
20
+ test:
21
+
22
+ runs-on: ubuntu-latest
23
+ strategy:
24
+ matrix:
25
+ ruby-version: ['2.6', '2.7', '3.0']
26
+
27
+ steps:
28
+ - uses: actions/checkout@v2
29
+ - name: Set up Ruby
30
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
31
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
32
+ # uses: ruby/setup-ruby@v1
33
+ uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
34
+ with:
35
+ ruby-version: ${{ matrix.ruby-version }}
36
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
37
+ - name: Run tests
38
+ run: bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ credentials.json
14
+ token.yaml
15
+ .byebug_history
16
+
17
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ source "https://rubygems.org"
3
+
4
+ # Specify your gem's dependencies in cal4near.gemspec
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 adebadayo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Cal4near
2
+
3
+ Googleカレンダーの情報をコンソールから取得するツール
4
+
5
+ (作成中)
6
+
7
+ 予定が入っていない時間を出力する
8
+
9
+ ```
10
+ $bin/cal4
11
+ 2021/10/28(木) 16:00-17:00, 18:00-19:00
12
+ 2021/10/29(金) 11:00-16:00, 17:00-19:00
13
+ 2021/10/30(土) 9:00-19:00
14
+ 2021/10/31(日) 9:00-19:00
15
+ 2021/11/01(月) 12:00-14:00, 17:00-19:00
16
+ 2021/11/02(火) 11:00-16:00
17
+ 2021/11/03(水) 12:00-19:00
18
+ 2021/11/04(木) 11:00-19:00
19
+
20
+ ```
21
+
22
+ ## Installation
23
+
24
+ Add this line to your application's Gemfile:
25
+
26
+ ```ruby
27
+ gem 'cal4near'
28
+ ```
29
+
30
+ And then execute:
31
+
32
+ $ bundle install
33
+
34
+ Or install it yourself as:
35
+
36
+ $ gem install cal4near
37
+
38
+ ## Usage
39
+
40
+ TODO: Write usage instructions here
41
+
42
+ ## Development
43
+
44
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
45
+
46
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cal4near.
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/cal4 ADDED
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+ require "bundler/setup"
3
+ require "cal4near"
4
+
5
+ HELP_MSG =<<-MSG
6
+ usage: cal4 [--free] [--busy]
7
+
8
+ --free : Stdout free times from calendar api
9
+ --busy : Stdout busy times from calendar api [WIP]
10
+ MSG
11
+
12
+ WRONG_ARG_ERR_MSG =<<-MSG
13
+ [ERR]argument is wrong.
14
+ MSG
15
+
16
+ if ARGV.length > 0
17
+ case ARGV[0]
18
+ when '-h'
19
+ puts HELP_MSG
20
+ return
21
+ when '--free', '-f'
22
+ msg = "free times"
23
+ times_info = Cal4near.free_times
24
+ # when '--busy', 'b'
25
+ # TODO:busy_timesの実装
26
+ # msg = "busy times"
27
+ # times_info = Cal4near.busy_times
28
+ else
29
+ puts WRONG_ARG_ERR_MSG
30
+ puts HELP_MSG
31
+ return
32
+ end
33
+ else
34
+ msg = "free times"
35
+ times_info = Cal4near.free_times
36
+ end
37
+
38
+ puts <<-MSG
39
+ --------------
40
+ #{msg}
41
+ --------------
42
+ MSG
43
+
44
+ Cal4near.stdout(times_info)
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "cal4near"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/cal4near.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/cal4near/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "cal4near"
7
+ spec.version = Cal4near::VERSION
8
+ spec.authors = ["nakamaksk"]
9
+ spec.email = ["adebayanen@gmail.com"]
10
+
11
+ spec.summary = "Fetch calendar information."
12
+ spec.description = "Fetch calendar information."
13
+ spec.homepage = "https://github.com/nakamaksk/cal4near"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
+
17
+ # spec.metadata["allowed_push_host"] = "'http://mygemserver.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/nakamaksk/cal4near"
21
+ spec.metadata["changelog_uri"] = "https://github.com/nakamaksk/cal4near"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "rake", "~> 13.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ spec.add_development_dependency 'google-api-client'
35
+ spec.add_development_dependency 'dotenv'
36
+ spec.add_development_dependency "pry"
37
+ spec.add_development_dependency 'pry-byebug'
38
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cal4near
4
+ VERSION = "0.1.0"
5
+ end
data/lib/cal4near.rb ADDED
@@ -0,0 +1,183 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "cal4near/version"
4
+ require "google/apis/calendar_v3"
5
+ require "googleauth"
6
+ require "googleauth/stores/file_token_store"
7
+ require "date"
8
+ require "fileutils"
9
+ require 'pry'
10
+ require 'pry-byebug'
11
+
12
+ module Cal4near
13
+ class Error < StandardError; end
14
+
15
+ OOB_URI = "urn:ietf:wg:oauth:2.0:oob".freeze
16
+ APPLICATION_NAME = "Google Calendar API Ruby Quickstart".freeze
17
+ CREDENTIALS_PATH = "credentials.json".freeze
18
+ # The file token.yaml stores the user's access and refresh tokens, and is
19
+ # created automatically when the authorization flow completes for the first
20
+ # time.
21
+ TOKEN_PATH = "token.yaml".freeze
22
+ SCOPE = Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY
23
+
24
+ # 空き時間を検索する日時の範囲
25
+ START_DATE = DateTime.now
26
+ END_DATE = DateTime.now.next_day(30)
27
+
28
+ # 空き時間を検索する時間の範囲
29
+ START_HOUR = 9
30
+ END_HOUR = 19
31
+
32
+ # カレンダーに登録されているイベントを取得
33
+ # @param [DateTime] start_date 対象期間の開始日時
34
+ # @param [DateTime] end_date 対象期間の終了日時
35
+ # @return [Array] Google::Apis::CalendarV3::Eventのリスト
36
+ def self.events(start_date, end_date)
37
+ service = Google::Apis::CalendarV3::CalendarService.new
38
+ service.client_options.application_name = APPLICATION_NAME
39
+ service.authorization = authorize
40
+ service.list_events(
41
+ "primary",
42
+ single_events: true,
43
+ order_by: "startTime",
44
+ time_min: start_date.rfc3339,
45
+ time_max: end_date.rfc3339
46
+ ).items
47
+ end
48
+
49
+ DATE_FORMAT = "%Y-%m-%d"
50
+ DATE_TIME_FORMAT = "%Y-%m-%d %H:%M"
51
+
52
+ # カレンダーの空き情報を取得
53
+ # @return [Hash]
54
+ # @example 返り値のサンプルは以下
55
+ # "2022-03-21"=> {"2022-03-21 09:00"=>{:free=>true}
56
+ def self.free_times(
57
+ start_date = START_DATE,
58
+ end_date = END_DATE,
59
+ start_hour = START_HOUR,
60
+ end_hour = END_HOUR
61
+ )
62
+ busy_list = []
63
+ events(start_date, end_date).each do |event|
64
+ # start endが両方ともdate型の場合は終日の予定
65
+ is_all_date = (event.start.date && event.end.date)
66
+
67
+ # [デバッグ用]カレンダーイベントの情報を出力する
68
+ # stdout_calendar_info(event, is_all_date)
69
+
70
+ unless is_all_date
71
+ busy_list << {
72
+ start: event.start.date_time,
73
+ end: event.end.date_time
74
+ }
75
+ end
76
+ end
77
+
78
+ # puts "Free time:"
79
+
80
+ result = {}
81
+ (start_date.to_date..end_date.to_date).each do |date|
82
+ result[date.strftime(DATE_FORMAT)] ||= {} # YYYY-MM-DD
83
+
84
+ start_work_time = Time.new(date.year, date.month, date.day, start_hour, 0, 0)
85
+ end_work_time = Time.new(date.year, date.month, date.day, end_hour, 0, 0)
86
+
87
+ step_secound = 60*60
88
+ start_work_time.to_i.step(end_work_time.to_i, step_secound).each_cons(2) do |c_time_int, n_time_int|
89
+ current_time = Time.at(c_time_int)
90
+ next_time = Time.at(n_time_int)
91
+
92
+ # 現時刻より前はスキップ
93
+ next if current_time.to_datetime < DateTime.now
94
+
95
+ date_key = date.strftime(DATE_FORMAT)
96
+ time_key = current_time.strftime(DATE_TIME_FORMAT)
97
+ result[date_key][time_key] = { free: true }
98
+
99
+ busy_list.each do |busy|
100
+ if current_time.to_datetime < busy[:end] && busy[:start] < next_time.to_datetime
101
+ result[date_key][time_key][:free] = false
102
+ break
103
+ end
104
+ end
105
+ end
106
+ end
107
+ result
108
+ end
109
+
110
+ # @param [Hash] free_times
111
+ def self.stdout(times_info)
112
+ result = times_info
113
+ wdays = %w(日 月 火 水 木 金 土)
114
+ # 出力
115
+ result.each do |date, times|
116
+ min_time = max_time = nil
117
+ spans = []
118
+ times.each do |time, info|
119
+ time = DateTime.parse(time)
120
+ min_time ||= time
121
+ max_time = time
122
+ if info[:free]
123
+ next
124
+ else
125
+ if min_time && max_time && min_time < max_time
126
+ spans << "#{min_time.strftime("%-H:%M")}-#{max_time.strftime("%-H:%M")}"
127
+ end
128
+ min_time = max_time = nil
129
+ end
130
+ end
131
+
132
+ if min_time && max_time && min_time < max_time
133
+ spans << "#{min_time.strftime("%-H:%M")}-#{max_time.strftime("%-H:%M")}"
134
+ end
135
+
136
+ tmp_date = Date.parse(date)
137
+ puts "#{tmp_date.strftime("%Y/%m/%d")}(#{wdays[tmp_date.wday]}) #{spans.join(", ")}"
138
+ end
139
+ end
140
+
141
+ ##
142
+ # Ensure valid credentials, either by restoring from the saved credentials
143
+ # files or intitiating an OAuth2 authorization. If authorization is required,
144
+ # the user's default browser will be launched to approve the request.
145
+ #
146
+ # @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
147
+ def self.authorize
148
+ client_id = Google::Auth::ClientId.from_file CREDENTIALS_PATH
149
+ token_store = Google::Auth::Stores::FileTokenStore.new file: TOKEN_PATH
150
+ authorizer = Google::Auth::UserAuthorizer.new client_id, SCOPE, token_store
151
+ user_id = "default"
152
+ credentials = authorizer.get_credentials user_id
153
+ if credentials.nil?
154
+ url = authorizer.get_authorization_url base_url: OOB_URI
155
+ puts "Open the following URL in the browser and enter the " \
156
+ "resulting code after authorization:\n" + url
157
+ code = gets
158
+ credentials = authorizer.get_and_store_credentials_from_code(
159
+ user_id: user_id, code: code, base_url: OOB_URI
160
+ )
161
+ end
162
+ credentials
163
+ end
164
+
165
+ private
166
+
167
+ def self.stdout_calendar_info(event, is_all_date)
168
+ start_date_time = event.start.date || event.start.date_time
169
+ end_date_time = event.end.date || event.end.date_time
170
+
171
+ description =
172
+ if is_all_date
173
+ "#{start_date_time.strftime("%Y/%m/%d")} 終日"
174
+ else
175
+ if start_date_time.to_date == end_date_time.to_date
176
+ "#{start_date_time.strftime("%Y/%m/%d %-H:%M")}-#{end_date_time.strftime("%-H:%M")}"
177
+ else
178
+ "#{start_date_time.strftime("%Y/%m/%d %-H:%M")}-#{end_date_time.strftime("%Y/%m/%d %-H:%M")}"
179
+ end
180
+ end
181
+ puts "- [#{description}] #{event.summary} "
182
+ end
183
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cal4near
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - nakamaksk
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-02-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '13.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '13.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: google-api-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: dotenv
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Fetch calendar information.
98
+ email:
99
+ - adebayanen@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".github/workflows/ruby.yml"
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/cal4
112
+ - bin/console
113
+ - bin/setup
114
+ - cal4near.gemspec
115
+ - lib/cal4near.rb
116
+ - lib/cal4near/version.rb
117
+ homepage: https://github.com/nakamaksk/cal4near
118
+ licenses:
119
+ - MIT
120
+ metadata:
121
+ homepage_uri: https://github.com/nakamaksk/cal4near
122
+ source_code_uri: https://github.com/nakamaksk/cal4near
123
+ changelog_uri: https://github.com/nakamaksk/cal4near
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: 2.4.0
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubygems_version: 3.3.7
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Fetch calendar information.
143
+ test_files: []