aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/calendar_interface.rb
blob: 5282d790d3d6f049b74befe938022d50f927d8f2 (plain)
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
# encoding: UTF-8
require 'mauve/generic_http_api_client'

module Mauve

  # Interface to the Bytemark calendar.
  #
  class CalendarInterface  < GenericHttpApiClient
    
    class << self

      def get_attendees(klass, at=Time.now)
        #
        # Returns nil if no calendar_url has been set.
        #
        return [] unless Configuration.current.bytemark_calendar_url
  
        url = Configuration.current.bytemark_calendar_url.dup

        url.merge!(File.join(url.path, "/api/attendees/#{klass}/#{at.strftime("%Y-%m-%dT%H:%M:00")}"))
        ans = do_get_yaml(url)

        return [] unless ans.is_a?(Array)

        ans.select{|x| x.is_a?(String)}
      end

      #
      # This should return a list of dates of forthcoming bank holidays
      #
      def get_bank_holiday_list(at = Time.now)
        return [] unless Configuration.current.bytemark_calendar_url

        url = Configuration.current.bytemark_calendar_url.dup
        url.merge!(File.join(url.path, "/api/bank_holidays/#{at.strftime("%Y-%m-%d")}"))
        ans = do_get_yaml(url)

        return [] unless ans.is_a?(Array)
        ans.select{|x| x.is_a?(Date)}
      end

      # Check to see if the user is on holiday.
      #
      # Class method.
      #
      # @param [String] usr User single sign on.
      # 
      # @return [Boolean] True if on holiday, false otherwise.
      def is_user_on_holiday?(usr, at=Time.now)
        get_attendees("staff_holiday").include?(usr)
      end

      # Check to see if the user is on holiday.
      #
      # Class method.
      #
      # @param [String] usr User single sign on.
      # 
      # @return [Boolean] True if on holiday, false otherwise.
      def is_user_off_sick?(usr, at=Time.now)
        get_attendees("sick_period", at).include?(usr)
      end

      private

      def get_yaml(url)
        resp = do_get(url)

        return (resp.is_a?(String) ? YAML.load(resp) : nil)
      rescue StandardError => err
        logger.error "Caught #{ex.class.to_s} (#{ex.to_s}) whilst querying #{url.to_s}."
        logger.debug err.backtrace.join("\n")
        nil
      end

    end

  end

end