ragoon 0.7.0 → 0.8.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
  SHA1:
3
- metadata.gz: fbd57a4e5bfe0f3990157e545b0d687d1317cfe1
4
- data.tar.gz: d70d933b70be5da6ddb0e73ab00b1797f51722c1
3
+ metadata.gz: 37f399478c7454f731afd71d4999580936ea1b53
4
+ data.tar.gz: 12cdf219b8815923988dc421e4de9a8f0b88c795
5
5
  SHA512:
6
- metadata.gz: e02b0db90f76d94ef930431d51a8700b2070e2d91c3b997b90c87ee8b57a0e31810e83f78fa89c908b7e50dc467f76a2c2a135e4daebeaf097c17fd7cfdc735e
7
- data.tar.gz: 4b78c29e494335076b8a8d22cdf5fbbca9bd5e54720f99921b73b46a4acf399db8a1686303f5966ae76afb60c96cdf7743f547521a45085f66bebc508c902cc1
6
+ metadata.gz: fdb2a9f426a97151430fb75635bcf7f8526c87633d333b92c72313939e9fcf113513702afc52d51c7ec7490d989411f3fef9e2e52aab56ac08c71f5dcc60c4a0
7
+ data.tar.gz: 24a2d356115daa93f0a9c4d5f739b25485bf41fead815d3327a333a617555d37938ff7280358138f09305077af45cb8c19410586b9301b1ec0e20492e9b6c4ed
data/Gemfile CHANGED
@@ -1,3 +1,9 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
+
5
+ group :test do
6
+ gem 'webmock'
7
+ gem 'vcr'
8
+ end
9
+
@@ -35,4 +35,19 @@ class Ragoon::Services
35
35
  "#{endpoint.scheme}://#{endpoint.host}#{endpoint.path}"
36
36
  end
37
37
 
38
+ def parse_time(str)
39
+ return nil if str.nil?
40
+ Time.parse(str).localtime
41
+ end
42
+
43
+ def to_date_str(time)
44
+ return nil if time.nil?
45
+ time.localtime.strftime('%F')
46
+ end
47
+
48
+ def to_datetime_str(time)
49
+ return nil if time.nil?
50
+ time.utc.strftime('%FT%TZ')
51
+ end
52
+
38
53
  end
@@ -1,4 +1,6 @@
1
1
  class Ragoon::Services::Schedule < Ragoon::Services
2
+ XML_NS = 'http://schemas.cybozu.co.jp/schedule/2008'
3
+
2
4
  def schedule_get_events(options = {})
3
5
  action_name = 'ScheduleGetEvents'
4
6
 
@@ -7,19 +9,29 @@ class Ragoon::Services::Schedule < Ragoon::Services
7
9
  body_node = Ragoon::XML.create_node(action_name)
8
10
  parameter_node = Ragoon::XML.create_node(
9
11
  'parameters',
10
- start: options[:start].strftime('%FT%T'),
11
- end: options[:end].strftime('%FT%T'),
12
- start_for_daily: options[:start].localtime.strftime('%F'),
13
- end_for_daily: options[:end].localtime.strftime('%F'),
12
+ start: to_datetime_str(options[:start]),
13
+ end: to_datetime_str(options[:end]),
14
+ start_for_daily: to_date_str(options[:start]),
15
+ end_for_daily: to_date_str(options[:end]),
14
16
  )
15
17
  body_node.add_child(parameter_node)
16
18
 
17
19
  client.request(action_name, body_node)
18
20
 
19
- events = client.result_set.xpath('//schedule_event').
20
- find_all { |ev| ev[:event_type] != 'banner' }.map do |event|
21
- parse_event(event)
22
- end
21
+ events_by_type = client.result_set.xpath('//schedule_event')
22
+ .group_by {|ev| ev[:event_type] }
23
+
24
+ # events_by_type['banner'] does not include results
25
+ [
26
+ (events_by_type['normal'] || []).map {|event|
27
+ parse_event(event)
28
+ },
29
+ (events_by_type['repeat'] || []).map {|event|
30
+ expand_repeat_event_periods(event, options[:start], options[:end]).map {|period|
31
+ parse_event(event, period)
32
+ }
33
+ }
34
+ ].flatten.sort_by { |e| e[:start_at] }
23
35
  end
24
36
 
25
37
  def schedule_add_event(options = {})
@@ -46,10 +58,7 @@ class Ragoon::Services::Schedule < Ragoon::Services
46
58
  )
47
59
  parameter_node.add_child(schedule_event_node)
48
60
 
49
- members_node = Ragoon::XML.create_node(
50
- 'members',
51
- xmlns: 'http://schemas.cybozu.co.jp/schedule/2008'
52
- )
61
+ members_node = Ragoon::XML.create_node('members', xmlns: XML_NS)
53
62
  schedule_event_node.add_child(members_node)
54
63
  if options[:users]
55
64
  options[:users].each do |user|
@@ -60,22 +69,19 @@ class Ragoon::Services::Schedule < Ragoon::Services
60
69
  end
61
70
  end
62
71
 
63
- when_node = Ragoon::XML.create_node(
64
- 'when',
65
- xmlns: 'http://schemas.cybozu.co.jp/schedule/2008'
66
- )
72
+ when_node = Ragoon::XML.create_node('when', xmlns: XML_NS)
67
73
  date_node =
68
74
  if options[:allday]
69
75
  Ragoon::XML.create_node(
70
76
  'date',
71
- start: options[:start_at].strftime('%F'),
72
- end: options[:end_at].strftime('%F')
77
+ start: to_date_str(options[:start_at]),
78
+ end: to_date_str(options[:end_at])
73
79
  )
74
80
  else
75
81
  Ragoon::XML.create_node(
76
82
  'datetime',
77
- start: options[:start_at].utc.strftime('%FT%T'),
78
- end: options[:end_at].utc.strftime('%FT%T')
83
+ start: to_datetime_str(options[:start_at]),
84
+ end: to_datetime_str(options[:end_at])
79
85
  )
80
86
  end
81
87
  when_node.add_child(date_node)
@@ -89,8 +95,9 @@ class Ragoon::Services::Schedule < Ragoon::Services
89
95
  }.first
90
96
  end
91
97
 
92
- def parse_event(event)
93
- period = start_and_end(event)
98
+ def parse_event(event, period = nil)
99
+ period = start_and_end(event) if period.nil?
100
+
94
101
  {
95
102
  id: event[:id],
96
103
  url: event_url(event[:id]),
@@ -102,6 +109,7 @@ class Ragoon::Services::Schedule < Ragoon::Services
102
109
  users: users_info(event),
103
110
  private: !(event[:public_type] == 'public'),
104
111
  allday: event[:allday] == 'true',
112
+ event_type: event[:event_type],
105
113
  }
106
114
  end
107
115
 
@@ -110,14 +118,14 @@ class Ragoon::Services::Schedule < Ragoon::Services
110
118
  end
111
119
 
112
120
  def facility_names(event)
113
- event.xpath('ev:members', ev: "http://schemas.cybozu.co.jp/schedule/2008").
114
- children.map { |c| c.xpath('ev:facility', ev: "http://schemas.cybozu.co.jp/schedule/2008").first }.
121
+ event.xpath('ev:members', ev: XML_NS).
122
+ children.map { |c| c.xpath('ev:facility', ev: XML_NS).first }.
115
123
  compact.map { |n| n[:name] }
116
124
  end
117
125
 
118
126
  def users_info(event)
119
- event.xpath('ev:members', ev: "http://schemas.cybozu.co.jp/schedule/2008").
120
- children.map { |c| c.xpath('ev:user', ev: "http://schemas.cybozu.co.jp/schedule/2008").first }.
127
+ event.xpath('ev:members', ev: XML_NS).
128
+ children.map { |c| c.xpath('ev:user', ev: XML_NS).first }.
121
129
  compact.map do |n|
122
130
  {
123
131
  id: n[:id].to_i,
@@ -129,28 +137,21 @@ class Ragoon::Services::Schedule < Ragoon::Services
129
137
  def start_and_end(event)
130
138
  start_at = nil
131
139
  end_at = nil
140
+ timezone = event[:timezone]
141
+ end_timezone = event[:end_timezone] || event[:timezone]
132
142
 
133
143
  unless event[:allday] == 'true'
134
- case event[:event_type]
135
- when 'normal'
136
- period = event.children.xpath('ev:datetime', ev: "http://schemas.cybozu.co.jp/schedule/2008").first
137
- unless period.nil?
138
- start_at = parse_event_time(period[:start])
139
- unless event[:start_only] == 'true'
140
- end_at = parse_event_time(period[:end])
141
- end
142
- end
143
- when 'repeat'
144
- repeat_info = event.xpath('ev:repeat_info', ev: 'http://schemas.cybozu.co.jp/schedule/2008').first
145
- unless repeat_info.nil?
146
- period = repeat_info.xpath('ev:condition', ev: 'http://schemas.cybozu.co.jp/schedule/2008').first
147
- unless period.nil?
148
- start_at = parse_event_time(period[:start_time])
149
- unless event[:start_only] == 'true'
150
- end_at = parse_event_time(period[:end_time])
151
- end
152
- end
153
- end
144
+ period = event.xpath('ev:when/ev:datetime', ev: XML_NS).first
145
+ unless period.nil?
146
+ start_at = parse_event_time(period[:start], timezone)
147
+ end_at = parse_event_time(period[:end], end_timezone)
148
+ end
149
+
150
+ else
151
+ period = event.xpath('ev:when/ev:date', ev: XML_NS).first
152
+ unless period.nil?
153
+ start_at = parse_event_time(period[:start], timezone)
154
+ end_at = parse_event_time(period[:end] + " 23:59:59", end_timezone)
154
155
  end
155
156
  end
156
157
 
@@ -160,8 +161,16 @@ class Ragoon::Services::Schedule < Ragoon::Services
160
161
  }
161
162
  end
162
163
 
163
- def parse_event_time(time)
164
- Time.parse(time).localtime.to_s
164
+ def parse_event_time(time, timezone = ENV['TZ'], now = Time.now)
165
+ return nil if time.nil?
166
+
167
+ begin
168
+ old_timezone = ENV['TZ']
169
+ ENV['TZ'] = timezone
170
+ Time.parse(time, now).localtime.to_s
171
+ ensure
172
+ ENV['TZ'] = old_timezone
173
+ end
165
174
  end
166
175
 
167
176
  def default_options(action_name)
@@ -172,4 +181,72 @@ class Ragoon::Services::Schedule < Ragoon::Services
172
181
  {}
173
182
  end
174
183
  end
184
+
185
+ private
186
+
187
+ def expand_repeat_event_periods(event, start_at, end_at)
188
+ timezone = event[:timezone]
189
+ end_timezone = event[:end_timezone] || event[:timezone]
190
+
191
+ periods = []
192
+
193
+ event.xpath('ev:repeat_info/ev:condition', ev: XML_NS).each do |cond|
194
+ exclusive_dates = event.xpath('ev:repeat_info/ev:exclusive_datetimes/ev:exclusive_datetime',
195
+ ev: XML_NS).map do |exclusive_date|
196
+ Date.parse(exclusive_date[:start])
197
+ end
198
+
199
+ dates = expand_dates(start_at.to_date, end_at.to_date, cond, exclusive_dates)
200
+
201
+ periods = dates.map do |date|
202
+ {
203
+ start_at: parse_event_time(cond[:start_time], timezone, date.to_time),
204
+ end_at: parse_event_time(cond[:end_time], end_timezone, date.to_time)
205
+ }
206
+ end
207
+ end
208
+
209
+ periods
210
+ end
211
+
212
+ def expand_dates(start_date, end_date, cond, exclusive_dates)
213
+ (start_date..end_date).select {|date|
214
+ next false if exclusive_dates.include?(date)
215
+
216
+ case cond[:type]
217
+ when 'day' # everyday
218
+ next true
219
+
220
+ when 'week' # everyweek
221
+ next date.wday == cond[:week].to_i
222
+
223
+ when 'weekday'
224
+ next (1..5).include?(date.wday)
225
+
226
+ when '1stweek'
227
+ next nth_weekday_of_month(date) == 1 && date.wday == cond[:week].to_i
228
+
229
+ when '2ndweek'
230
+ next nth_weekday_of_month(date) == 2 && date.wday == cond[:week].to_i
231
+
232
+ when '3rdweek'
233
+ next nth_weekday_of_month(date) == 3 && date.wday == cond[:week].to_i
234
+
235
+ when '4thweek'
236
+ next nth_weekday_of_month(date) == 4 && date.wday == cond[:week].to_i
237
+
238
+ when 'lastweek'
239
+ next date.month != date.next_day(7).month &&
240
+ date.wday == cond[:week].to_i
241
+
242
+ when 'month'
243
+ next date.day == cond[:day].to_i
244
+
245
+ end
246
+ }
247
+ end
248
+
249
+ def nth_weekday_of_month(date)
250
+ (date.day + 6) / 7
251
+ end
175
252
  end
@@ -14,7 +14,7 @@ class Ragoon::Services::Workflow < Ragoon::Services
14
14
  options[:request_form_id] = request_form_id
15
15
  [ :start_request_date, :end_request_date,
16
16
  :start_approval_date, :end_approval_date ].each do |key|
17
- options[key] = to_datetime(options[key]) if options.has_key?(key)
17
+ options[key] = to_datetime_str(options[key]) if options.has_key?(key)
18
18
  end
19
19
 
20
20
  body_node = Ragoon::XML.create_node(action_name)
@@ -119,8 +119,8 @@ class Ragoon::Services::Workflow < Ragoon::Services
119
119
  body_node = Ragoon::XML.create_node(action_name)
120
120
 
121
121
  attributes = {}
122
- attributes['start'] = to_datetime(start_date) unless start_date.nil?
123
- attributes['end'] = to_datetime(end_date) unless end_date.nil?
122
+ attributes['start'] = to_datetime_str(start_date) unless start_date.nil?
123
+ attributes['end'] = to_datetime_str(end_date) unless end_date.nil?
124
124
  parameter_node = Ragoon::XML.create_node('parameters', attributes)
125
125
  body_node.add_child(parameter_node)
126
126
 
@@ -209,12 +209,4 @@ class Ragoon::Services::Workflow < Ragoon::Services
209
209
  }
210
210
  end
211
211
 
212
- def parse_time(time)
213
- Time.parse(time).localtime
214
- end
215
-
216
- def to_datetime(str)
217
- str.utc.strftime('%FT%TZ')
218
- end
219
-
220
212
  end
@@ -1,3 +1,3 @@
1
1
  module Ragoon
2
- VERSION = '0.7.0'
2
+ VERSION = '0.8.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ragoon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SHIOYA, Hiromu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-26 00:00:00.000000000 Z
11
+ date: 2016-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client