Class: Ragoon::Services::Schedule

Inherits:
Ragoon::Services show all
Defined in:
lib/ragoon/services/schedule.rb

Constant Summary collapse

XML_NS =
'http://schemas.cybozu.co.jp/schedule/2008'

Constants inherited from Ragoon::Services

SERVICE_LOCATIONS

Instance Attribute Summary

Attributes inherited from Ragoon::Services

#action_type, #client

Instance Method Summary collapse

Methods inherited from Ragoon::Services

#endpoint, #garoon_endpoint, #initialize, start_and_end

Constructor Details

This class inherits a constructor from Ragoon::Services

Instance Method Details

#default_options(action_name) ⇒ Object



176
177
178
179
180
181
182
183
# File 'lib/ragoon/services/schedule.rb', line 176

def default_options(action_name)
  case action_name
  when 'ScheduleGetEvents'
    Ragoon::Services.start_and_end
  else
    {}
  end
end

#event_url(id) ⇒ Object



116
117
118
# File 'lib/ragoon/services/schedule.rb', line 116

def event_url(id)
  "#{base_endpoint}/schedule/view?event=#{id}"
end

#facility_names(event) ⇒ Object



120
121
122
123
124
# File 'lib/ragoon/services/schedule.rb', line 120

def facility_names(event)
  event.xpath('ev:members', ev: XML_NS).
    children.map { |c| c.xpath('ev:facility', ev: XML_NS).first }.
    compact.map { |n| n[:name] }
end

#parse_event(event, period = nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ragoon/services/schedule.rb', line 98

def parse_event(event, period = nil)
  period = start_and_end(event) if period.nil?

  {
    id:         event[:id],
    url:        event_url(event[:id]),
    title:      event[:detail],
    start_at:   period[:start_at],
    end_at:     period[:end_at],
    plan:       event[:plan],
    facilities: facility_names(event),
    users:      users_info(event),
    private:    !(event[:public_type] == 'public'),
    allday:     event[:allday] == 'true',
    event_type: event[:event_type],
  }
end

#parse_event_time(time, timezone = , now = Time.now) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/ragoon/services/schedule.rb', line 164

def parse_event_time(time, timezone = ENV['TZ'], now = Time.now)
  return nil if time.nil?

  begin
    old_timezone = ENV['TZ']
    ENV['TZ'] = timezone
    Time.parse(time, now).localtime.to_s
  ensure
    ENV['TZ'] = old_timezone
  end
end

#schedule_add_event(options = {}) ⇒ Object



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
# File 'lib/ragoon/services/schedule.rb', line 37

def schedule_add_event(options = {})
  action_name = 'ScheduleAddEvents'

  options = default_options(action_name).merge(options)

  body_node = Ragoon::XML.create_node(action_name)
  parameter_node = Ragoon::XML.create_node('parameters')
  body_node.add_child(parameter_node)

  schedule_event_node = Ragoon::XML.create_node(
    'schedule_event',
    {
      xmlns:       '',
      id:          'dummy',
      version:     'dummy',
      event_type:  'normal',
      plan:        options[:plan],
      detail:      options[:title],
      description: options[:description],
      public_type: (options[:private] ? 'private' : 'public')
    }
  )
  parameter_node.add_child(schedule_event_node)

  members_node = Ragoon::XML.create_node('members', xmlns: XML_NS)
  schedule_event_node.add_child(members_node)
  if options[:users]
    options[:users].each do |user|
      member_node = Ragoon::XML.create_node('member')
      user_node = Ragoon::XML.create_node('user', id: user.to_i)
      member_node.add_child(user_node)
      members_node.add_child(member_node)
    end
  end

  when_node = Ragoon::XML.create_node('when', xmlns: XML_NS)
  date_node =
    if options[:allday]
      Ragoon::XML.create_node(
        'date',
        start: to_date_str(options[:start_at]),
        end:   to_date_str(options[:end_at])
      )
    else
      Ragoon::XML.create_node(
        'datetime',
        start: to_datetime_str(options[:start_at]),
        end:   to_datetime_str(options[:end_at])
      )
    end
  when_node.add_child(date_node)
  schedule_event_node.add_child(when_node)

  client.request(action_name, body_node)

  events = client.result_set.xpath('//schedule_event').
           find_all { |ev| ev[:event_type] != 'banner' }.map { |event|
    parse_event(event)
  }.first
end

#schedule_get_events(options = {}) ⇒ Object



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
# File 'lib/ragoon/services/schedule.rb', line 4

def schedule_get_events(options = {})
  action_name = 'ScheduleGetEvents'

  options = default_options(action_name).merge(options)

  body_node = Ragoon::XML.create_node(action_name)
  parameter_node = Ragoon::XML.create_node(
    'parameters',
    start:           to_datetime_str(options[:start]),
    end:             to_datetime_str(options[:end]),
    start_for_daily: to_date_str(options[:start]),
    end_for_daily:   to_date_str(options[:end]),
  )
  body_node.add_child(parameter_node)

  client.request(action_name, body_node)

  events_by_type = client.result_set.xpath('//schedule_event')
    .group_by {|ev| ev[:event_type] }

  # events_by_type['banner'] does not include results
  [
    (events_by_type['normal'] || []).map {|event|
      parse_event(event)
    },
    (events_by_type['repeat'] || []).map {|event|
      expand_repeat_event_periods(event, options[:start], options[:end]).map {|period|
        parse_event(event, period)
      }
    }
  ].flatten.sort_by { |e| e[:start_at] }
end

#start_and_end(event) ⇒ Object



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
# File 'lib/ragoon/services/schedule.rb', line 137

def start_and_end(event)
  start_at = nil
  end_at   = nil
  timezone = event[:timezone]
  end_timezone = event[:end_timezone] || event[:timezone]

  unless event[:allday] == 'true'
    period = event.xpath('ev:when/ev:datetime', ev: XML_NS).first
    unless period.nil?
      start_at = parse_event_time(period[:start], timezone)
      end_at = parse_event_time(period[:end], end_timezone)
    end

  else
    period = event.xpath('ev:when/ev:date', ev: XML_NS).first
    unless period.nil?
      start_at = parse_event_time(period[:start], timezone)
      end_at = parse_event_time(period[:end] + " 23:59:59", end_timezone)
    end
  end

  {
    start_at: start_at,
    end_at:   end_at,
  }
end

#users_info(event) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/ragoon/services/schedule.rb', line 126

def users_info(event)
  event.xpath('ev:members', ev: XML_NS).
    children.map { |c| c.xpath('ev:user', ev: XML_NS).first }.
    compact.map do |n|
      {
        id: n[:id].to_i,
        name: n[:name].to_s,
      }
    end
end