colleagues-calendar-command 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +2 -0
- data/bin/colleagues-calendar-command +7 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/colleagues-calendar-command.gemspec +29 -0
- data/lib/colleagues/calendar/command.rb +141 -0
- data/lib/colleagues/calendar/command/calendar.rb +62 -0
- data/lib/colleagues/calendar/command/config.rb +65 -0
- data/lib/colleagues/calendar/command/version.rb +7 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 280fbe28d09cda2e9beec8282d0151a65bcdba69
|
4
|
+
data.tar.gz: 5bf089bdba5f228cf1b768d410a79ee8fb65db30
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ec75eb99ab4a6104fe1dfd4c9b850e6ff1b0a36e733882c5557673f37162432236e85f28bcf58ea0f80c2dacf0c54849e26462c7a822ea6073cf2b71723391ed
|
7
|
+
data.tar.gz: 4de2488ff2bd1fb64e67dd7c8a946e6c2e3ca4a4ce827b0c742ae22820d861048c0193a0674cc703d8442a8708e53a8e3beaf961bfd879cadf6562cea49da833
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Kentaro Hayashi
|
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,39 @@
|
|
1
|
+
# Colleagues::Calendar::Input::Helper
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/colleagues/calendar/input/helper`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'colleagues-calendar-input-helper'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install colleagues-calendar-input-helper
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/colleagues-calendar-input-helper.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "colleagues/calendar/input/helper"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "colleagues/calendar/command/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "colleagues-calendar-command"
|
8
|
+
spec.version = Colleagues::Calendar::Command::VERSION
|
9
|
+
spec.authors = ["Kentaro Hayashi"]
|
10
|
+
spec.email = ["hayashi@clear-code.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Input helper tool for Collegues calendar}
|
13
|
+
spec.description = %q{This tool helps to input schedule with specific format.}
|
14
|
+
spec.homepage = "https://github.com/kenhys/colleagues-calendar-command"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_runtime_dependency("google_calendar")
|
25
|
+
spec.add_runtime_dependency("highline")
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require "optparse"
|
3
|
+
require "highline"
|
4
|
+
require "yaml"
|
5
|
+
require "google_calendar"
|
6
|
+
require "colleagues/calendar/command/version"
|
7
|
+
require "colleagues/calendar/command/config"
|
8
|
+
require "colleagues/calendar/command/calendar"
|
9
|
+
|
10
|
+
module Colleagues
|
11
|
+
module Calendar
|
12
|
+
module Command
|
13
|
+
|
14
|
+
class ConfigurationError < StandardError; end
|
15
|
+
|
16
|
+
class CommandLine
|
17
|
+
|
18
|
+
def initialize
|
19
|
+
@config = ConfigParser.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def select_calendar
|
23
|
+
selection = {}
|
24
|
+
@cli.choose do |menu|
|
25
|
+
menu.prompt = "誰のカレンダーに登録しますか?"
|
26
|
+
@config.calendar_ids.keys.each do |key|
|
27
|
+
menu.choice(key) do |person|
|
28
|
+
calendar_id = @config.calendar_ids[person]
|
29
|
+
selection[:person] = person
|
30
|
+
selection[:calendar_id] = calendar_id
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
selection
|
35
|
+
end
|
36
|
+
|
37
|
+
def select_schedule_type
|
38
|
+
schedule_type = ""
|
39
|
+
@cli.choose do |menu|
|
40
|
+
menu.prompt = "登録する予定の種類は?"
|
41
|
+
@config.schedule_type.each do |type, item|
|
42
|
+
menu.choice(item["label"]) do |selected|
|
43
|
+
schedule_type = selected
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
schedule_type
|
48
|
+
end
|
49
|
+
|
50
|
+
def input_schedule_date(schedule_type)
|
51
|
+
message =<<SCHEDULE
|
52
|
+
#{schedule_type}ですね。登録予定の日時は?
|
53
|
+
入力例:
|
54
|
+
11/29 (全休などの場合)
|
55
|
+
10:00 - 11:00 (当日の予定の場合)
|
56
|
+
11/29 10:00 - 11/29 11:00 (他の日の予定の場合)
|
57
|
+
|
58
|
+
SCHEDULE
|
59
|
+
input = @cli.ask(message)
|
60
|
+
begin_time = Time.now
|
61
|
+
end_time = Time.now
|
62
|
+
begin
|
63
|
+
if input.index("-")
|
64
|
+
begin_time = Time.parse(input.split("-")[0])
|
65
|
+
end_time = Time.parse(input.split("-")[1])
|
66
|
+
else
|
67
|
+
begin_time = Time.parse(input)
|
68
|
+
end_time = begin_time + (60 * 60 * 24)
|
69
|
+
end
|
70
|
+
rescue
|
71
|
+
p "FAIL"
|
72
|
+
return
|
73
|
+
end
|
74
|
+
[begin_time, end_time]
|
75
|
+
end
|
76
|
+
|
77
|
+
def run(arguments)
|
78
|
+
@cli = HighLine.new
|
79
|
+
calendar_selection = select_calendar
|
80
|
+
@calendar_id = calendar_selection[:calendar_id]
|
81
|
+
schedule_type = select_schedule_type
|
82
|
+
schedule_date = input_schedule_date(schedule_type)
|
83
|
+
register_event(schedule_type, schedule_date[0], schedule_date[1])
|
84
|
+
true
|
85
|
+
end
|
86
|
+
|
87
|
+
def register_event(schedule_type, begin_time, end_time)
|
88
|
+
@config.schedule_type.each do |key, item|
|
89
|
+
if item["label"] == schedule_type
|
90
|
+
if item["all_day"]
|
91
|
+
register_all_day_event("#{item["text"]}", begin_time, end_time)
|
92
|
+
else
|
93
|
+
register_oneshot_event("#{item["text"]}", begin_time, end_time)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def register_all_day_event(title, begin_time, end_time)
|
100
|
+
begin
|
101
|
+
@calendar = Calendar.new(@config.client_id,
|
102
|
+
@config.client_secret,
|
103
|
+
@calendar_id,
|
104
|
+
@config.refresh_token)
|
105
|
+
@calendar.create_all_day(title,
|
106
|
+
begin_time,
|
107
|
+
end_time)
|
108
|
+
unless @config.refresh_token
|
109
|
+
@config.refresh_token = @calendar.refresh_token
|
110
|
+
@config.save
|
111
|
+
end
|
112
|
+
@cli.say("登録を完了しました。")
|
113
|
+
rescue => e
|
114
|
+
puts e.message
|
115
|
+
exit 1
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def register_oneshot_event(title, begin_time, end_time)
|
120
|
+
begin
|
121
|
+
@calendar = Calendar.new(@config.client_id,
|
122
|
+
@config.client_secret,
|
123
|
+
@calendar_id,
|
124
|
+
@config.refresh_token)
|
125
|
+
@calendar.create_event(title,
|
126
|
+
begin_time,
|
127
|
+
end_time)
|
128
|
+
unless @config.refresh_token
|
129
|
+
@config.refresh_token = @calendar.refresh_token
|
130
|
+
@config.save
|
131
|
+
end
|
132
|
+
@cli.say("登録を完了しました。")
|
133
|
+
rescue => e
|
134
|
+
puts e.message
|
135
|
+
exit 1
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
module Colleagues
|
3
|
+
module Calendar
|
4
|
+
module Command
|
5
|
+
|
6
|
+
class Calendar
|
7
|
+
attr_accessor :refresh_token
|
8
|
+
|
9
|
+
def initialize(client_id, client_secret, calendar_id, refresh_token = nil)
|
10
|
+
@calendar = Google::Calendar.new(:client_id => client_id,
|
11
|
+
:client_secret => client_secret,
|
12
|
+
:calendar => calendar_id,
|
13
|
+
:redirect_url => "urn:ietf:wg:oauth:2.0:oob")
|
14
|
+
@refresh_token = refresh_token
|
15
|
+
end
|
16
|
+
|
17
|
+
def renew_refresh_token
|
18
|
+
puts "以下のURLをブラウザで開き、アクセスを承認してください。:"
|
19
|
+
puts @calendar.authorize_url
|
20
|
+
puts "リフレッシュトークンを入力してください。:"
|
21
|
+
@refresh_token = @calendar.login_with_auth_code($stdin.gets.chomp)
|
22
|
+
@refresh_token
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_all_day(title,
|
26
|
+
begin_time,
|
27
|
+
end_time)
|
28
|
+
check_refresh_token
|
29
|
+
event = @calendar.create_event do |e|
|
30
|
+
e.title = title
|
31
|
+
e.start_time = begin_time
|
32
|
+
e.end_time = end_time
|
33
|
+
e.all_day = begin_time
|
34
|
+
end
|
35
|
+
event
|
36
|
+
end
|
37
|
+
|
38
|
+
def create_event(title,
|
39
|
+
begin_time,
|
40
|
+
end_time)
|
41
|
+
check_refresh_token
|
42
|
+
event = @calendar.create_event do |e|
|
43
|
+
e.title = title
|
44
|
+
e.start_time = begin_time
|
45
|
+
e.end_time = end_time
|
46
|
+
end
|
47
|
+
event
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def check_refresh_token
|
53
|
+
unless @refresh_token
|
54
|
+
@refresh_token = renew_refresh_token
|
55
|
+
end
|
56
|
+
@calendar.login_with_refresh_token(@refresh_token)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Colleagues
|
2
|
+
module Calendar
|
3
|
+
module Command
|
4
|
+
class ConfigParser
|
5
|
+
|
6
|
+
attr_reader :client_id
|
7
|
+
attr_reader :client_secret
|
8
|
+
attr_reader :calendar_ids
|
9
|
+
attr_reader :schedule_type
|
10
|
+
attr_accessor :refresh_token
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
begin
|
14
|
+
config_path = File.expand_path("~/.colleagues-calendar-command.yaml")
|
15
|
+
unless File.exist?(config_path)
|
16
|
+
message =<<MESSAGES
|
17
|
+
Configuration file:<#{config_path}> not found.
|
18
|
+
Create #{config_path}.
|
19
|
+
|
20
|
+
Example:
|
21
|
+
---
|
22
|
+
calendar_ids:
|
23
|
+
John Smith: john@example.com
|
24
|
+
|
25
|
+
MESSAGES
|
26
|
+
raise ConfigurationError, message
|
27
|
+
end
|
28
|
+
@conf = YAML.load_file(config_path)
|
29
|
+
@calendar_ids = @conf["calendar_ids"]
|
30
|
+
@schedule_type = @conf["schedule_type"]
|
31
|
+
@refresh_token = @conf["refresh_token"]
|
32
|
+
|
33
|
+
secret_path = File.expand_path("~/.colleagues-calendar-command.secret.json")
|
34
|
+
unless File.exist?(secret_path)
|
35
|
+
message =<<MESSAGES
|
36
|
+
Client secret file:<#{secret_path}> not found.
|
37
|
+
|
38
|
+
1. Create project on https://console.developers.google.com/ for Google Calendar API
|
39
|
+
2. Download credential(OAuth client ID) file.
|
40
|
+
|
41
|
+
MESSAGES
|
42
|
+
raise ConfigurationError, message
|
43
|
+
end
|
44
|
+
open(secret_path) do |file|
|
45
|
+
@secret = JSON.load(file)
|
46
|
+
@client_id = @secret["installed"]["client_id"]
|
47
|
+
@client_secret = @secret["installed"]["client_secret"]
|
48
|
+
end
|
49
|
+
rescue => e
|
50
|
+
puts e.message
|
51
|
+
exit 1
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def save
|
56
|
+
@conf["refresh_token"] = @refresh_token
|
57
|
+
config_path = File.expand_path("~/.colleagues-calendar-command.yaml")
|
58
|
+
open(config_path, "w") do |file|
|
59
|
+
file.puts(YAML.dump(@conf))
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: colleagues-calendar-command
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kentaro Hayashi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: google_calendar
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: highline
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.15'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.15'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: This tool helps to input schedule with specific format.
|
70
|
+
email:
|
71
|
+
- hayashi@clear-code.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- bin/colleagues-calendar-command
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- colleagues-calendar-command.gemspec
|
85
|
+
- lib/colleagues/calendar/command.rb
|
86
|
+
- lib/colleagues/calendar/command/calendar.rb
|
87
|
+
- lib/colleagues/calendar/command/config.rb
|
88
|
+
- lib/colleagues/calendar/command/version.rb
|
89
|
+
homepage: https://github.com/kenhys/colleagues-calendar-command
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.6.13
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Input helper tool for Collegues calendar
|
113
|
+
test_files: []
|