forked from kimura3104/Clockly
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.rb
More file actions
216 lines (186 loc) · 5.95 KB
/
server.rb
File metadata and controls
216 lines (186 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
require 'sinatra'
require 'sinatra/cross_origin'
require "sinatra/json"
require "json"
require "google-apis-calendar_v3"
require 'googleauth/stores/file_token_store'
require 'dotenv/load'
DB_PATH = "./db/"
Dotenv.load
enable :cross_origin
set :public_folder, 'build'
def file_load(key)
begin
File.read(DB_PATH + key)
rescue
nil
end
end
def file_store(key, value)
begin
File.write("#{DB_PATH + key}", value)
rescue
nil
end
end
def get_calendar
end
def insert_event
end
#####################################################
# プログラム自動実行部
#####################################################
def auto_exec(calendar_id_list)
end
before do
response.headers['Access-Control-Allow-Origin'] = '*'
@settings_file_path = "settings.yml"
@config = YAML.load_file(@settings_file_path) if File.exist?(@settings_file_path)
@client = Google::Apis::CalendarV3::CalendarService.new
@authorizer = Google::Auth::UserAuthorizer.new(
Google::Auth::ClientId.new(ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET']),
Google::Apis::CalendarV3::AUTH_CALENDAR,
Google::Auth::Stores::FileTokenStore.new(file: ENV['TOKEN_STORE_PATH']),
ENV['GOOGLE_CALLBACK_URL']
)
@client.client_options.application_name = "Clockly"
@client.authorization = @authorizer.get_credentials(ENV['DEFAULT_USER'])
end
get '/authorize' do
return json @authorizer.get_authorization_url
end
get '/auth/google_oauth2/callback' do
@authorizer.get_and_store_credentials_from_code(
user_id: ENV['DEFAULT_USER'],
code: params[:code]
)
redirect 'http://localhost:4567/settings'
end
post '/writable' do
data = JSON.parse(request.body.read)
writable_list = @config["writable_calendar_id"]
writable_list.delete(data['id'])
writable_list << data['id'] if !writable_list.include?(data['id']) if data['status']
@config["writable_calendar_id"] = writable_list
YAML.dump(@config, File.open(@settings_file_path, 'w'))
end
#####################################################
# カレンダ管理部
#####################################################
# カレンダの取得機能
get '/calendar_list' do
writable_calendar_list = @config["writable_calendar_id"]
calendar_list = @client.list_calendar_lists().items.map do |calendar|
{
"summary" => calendar.summary,
"id" => calendar.id,
"writable" => writable_calendar_list.include?(calendar.id)
}
end
return json calendar_list
end
get '/calendar/:id?' do
puts @client.list_events(params["id"], single_events: true)
return @client.list_events(params["id"], single_events: true).to_h.to_json
end
# カレンダの更新機能
post '/create_calendar' do
end
post '/delete_calendar' do
end
post '/insert_event' do
data = JSON.parse(request.body.read)
calendar_id = data['calendar_id']
event = data['event']
puts event['start']['date']
puts event['start']['date_time']
google_event = Google::Apis::CalendarV3::Event.new(
summary: event['summary'],
location: event['location'],
start: {
date: event['start']['date'],
date_time: event['start']['date_time']
},
end: {
date: event['end']['date'],
date_time: event['end']['date_time']
}
)
if @config["writable_calendar_id"].include?(calendar_id)
@client.insert_event(calendar_id, google_event)
end
end
post '/update_event' do
end
post '/delete_event' do
data = JSON.parse(request.body.read)
calendar_id = data['calendar_id']
event_id = data['event_id']
@client.delete_event(calendar_id, event_id)
end
#####################################################
# プログラム管理部
#####################################################
# プログラムの取得機能
get '/programs/:id?' do
programs = JSON.parse(file_load("program-repository.json"))
return json programs if !params["id"]
programs.each do |program|
return json program if program["id"] == params["id"]
end
end
# プログラムリポジトリの更新機能
post '/create_program' do
data = JSON.parse(request.body.read)
programs = []
programs = JSON.parse(file_load("program-repository.json")) if file_load("program-repository.json")
program = {
"id" => "#{data['id']}",
"name" => "#{data['name']}",
"block" => "#{data['blockXml']}",
"code" => "#{data['jsCode']}",
"rbcode" => "#{data['rbCode']}",
"calendar_id_list" => data['calendar_id_list'],
"enable_auto" => "#{data['enable_auto']}",
}
programs << program
file_store("program-repository.json", programs.to_json)
end
post '/update_program' do
data = JSON.parse(request.body.read)
updated_program = {
"id" => "#{data['id']}",
"name" => "#{data['name']}",
"block" => "#{data['blockXml']}",
"code" => "#{data['jsCode']}",
"rbcode" => "#{data['rbCode']}",
"calendar_id_list" => data['calendar_id_list'],
"enable_auto" => "#{data['enable_auto']}",
}
programs = file_load("program-repository.json")
programs = JSON.parse(programs)
programs.each_with_index do |program, i|
if program["id"] == updated_program["id"]
programs.delete_at(i);
end
end
programs << updated_program
file_store("program-repository.json", programs.to_json)
end
post '/delete_program' do
data = JSON.parse(request.body.read)
delete_program_id = data['id']
puts delete_program_id
return nil unless programs = file_load("program-repository.json")
programs = JSON.parse(programs)
programs.each_with_index do |program, i|
if program["id"] == delete_program_id
programs.delete_at(i);
end
end
file_store("program-repository.json", programs.to_json)
end
get '/*' do
content_type 'text/html'
return File.read('./build/index.html')
end