Class: Ragoon::Services::Schedule

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

Constant Summary

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, #initialize, start_and_end

Constructor Details

This class inherits a constructor from Ragoon::Services

Instance Method Details

#default_options(action_name) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/ragoon/services/schedule.rb', line 84

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

#event_url(id) ⇒ Object



36
37
38
# File 'lib/ragoon/services/schedule.rb', line 36

def event_url(id)
  "#{Ragoon::garoon_endpoint.gsub(/\?.*\Z/, '')}/schedule/view?event=#{id}"
end

#facility_names(event) ⇒ Object



40
41
42
43
44
# File 'lib/ragoon/services/schedule.rb', line 40

def facility_names(event)
  event.xpath('ev:members', ev: "http://schemas.cybozu.co.jp/schedule/2008").
    children.map { |c| c.xpath('ev:facility', ev: "http://schemas.cybozu.co.jp/schedule/2008").first }.
    compact.map { |n| n[:name] }
end

#parse_event_time(time) ⇒ Object



80
81
82
# File 'lib/ragoon/services/schedule.rb', line 80

def parse_event_time(time)
  Time.parse(time).localtime.to_s
end

#schedule_get_events(options = {}) ⇒ Object



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

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:           options[:start].strftime('%FT%T'),
    end:             options[:end].strftime('%FT%T'),
    start_for_daily: options[:start].strftime('%F'),
    end_for_daily:   options[:end].strftime('%F'),
  )
  body_node.add_child(parameter_node)

  client.request(action_name, body_node)

  events = client.result_set.xpath('//schedule_event').
           find_all { |ev| ev[:event_type] != 'banner' }.map do |event|
    period = start_and_end(event)
    {
      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),
      private:    !(event[:public_type] == 'public'),
      allday:     event[:allday] == 'true',
    }
  end
end

#start_and_end(event) ⇒ Object



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

def start_and_end(event)
  start_at = nil
  end_at   = nil

  unless event[:allday] == 'true'
    case event[:event_type]
    when 'normal'
      period = event.children.xpath('ev:datetime', ev: "http://schemas.cybozu.co.jp/schedule/2008").first
      unless period.nil?
        start_at = parse_event_time(period[:start])
        unless event[:start_only] == 'true'
          end_at = parse_event_time(period[:end])
        end
      end
    when 'repeat'
      repeat_info = event.xpath('ev:repeat_info', ev: 'http://schemas.cybozu.co.jp/schedule/2008').first
      unless repeat_info.nil?
        period = repeat_info.xpath('ev:condition', ev: 'http://schemas.cybozu.co.jp/schedule/2008').first
        unless period.nil?
          start_at = parse_event_time(period[:start_time])
          unless event[:start_only] == 'true'
            end_at = parse_event_time(period[:end_time])
          end
        end
      end
    end
  end

  {
    start_at: start_at,
    end_at:   end_at,
  }
end