Class: ServiceNow::Incident

Inherits:
Object
  • Object
show all
Defined in:
lib/classes/incident.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, saved_on_sn = false, internal_call = false) ⇒ Incident

Returns a new instance of Incident.



10
11
12
13
14
15
16
17
18
# File 'lib/classes/incident.rb', line 10

def initialize(attributes = {}, saved_on_sn = false, internal_call = false)
    Incident.check_configuration
    symbolized_attributes = Hash[attributes.map{|k, v| [k.to_sym, v]}]
    if !symbolized_attributes[:number].nil? && !internal_call # allow setting INC number if it's called internally
        raise "SN::ERROR: You are not allowed to set INC Number manually, the server will take care of that"
    end
    @attributes = symbolized_attributes
    @saved_on_sn = saved_on_sn
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, args = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/classes/incident.rb', line 28

def method_missing(method, args = nil)
    method_name = method.to_s
    if match = method_name.match(/(.*)=/) # writer method
        attribute = match[1]
        if attribute == "number" && @saved_on_sn
            raise "SN::ERROR: You are not allowed to set INC Number manually, the server will take care of that"
        end
        @attributes[attribute.to_sym] = args
    else # reader method
        @attributes[method_name.to_sym]
    end
end

Class Method Details

.find(inc_number) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/classes/incident.rb', line 66

def self.find(inc_number)
    Incident.check_configuration
    inc_string = inc_number.to_s.match(/[123456789]+\d*$/).to_s
    if inc_string.length > 7
        raise "SN::Error: invalid Incident number"
    end
    query_hash = {}
    query_hash[:number] = "INC" + "0"*(7-inc_string.length) + inc_string
    response = Configuration.get_resource(query_hash, table = "incident").get();
    # returned hash
    hash = JSON.parse(response, { :symbolize_names => true })
    # return the Incident object
    inc_obj = Incident.new(attributes = hash[:records][0], saved_on_sn = true, internal_call = true)
    if inc_obj.attributes.nil?
        "SN::Alert: No incident with incident number #{query_hash[:number]} found"
    else
        inc_obj
    end
end

.where(query_hash = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/classes/incident.rb', line 86

def self.where(query_hash = {})
    Incident.check_configuration
    response = Configuration.get_resource(query_hash, table = "incident").get();
    hash = JSON.parse(response, { :symbolize_names => true })
    array_of_records = hash[:records]
    array_of_inc = []
    array_of_records.each do |record|
        array_of_inc << Incident.new(attributes = record, saved_on_sn = true, internal_call = true)
    end
    array_of_inc
end

Instance Method Details

#attributesObject



20
21
22
# File 'lib/classes/incident.rb', line 20

def attributes
    @attributes
end

#clientObject

must be used only when displayvable is false



24
25
26
# File 'lib/classes/incident.rb', line 24

def client # must be used only when displayvable is false
    return User.find_by_sys_id(self.caller_id)
end

#inspectObject



4
5
6
7
8
# File 'lib/classes/incident.rb', line 4

def inspect
    @attributes.each do |k, v|
        puts "#{k} => #{v}"
    end
end

#save!Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/classes/incident.rb', line 41

def save!
    # if this is a new incident (still in memory and not on SN), and the user set the Incident number
    # we raise an exception
    if !@attributes[:number].nil? && !@saved_on_sn
        raise "SN::ERROR: You are not allowed to set INC Number manually, the server will take care of that"
    end
    # we only create new incidents if it's not saved already
    if !@saved_on_sn
        response = Configuration.post_resource(table = "incident").post(self.attributes.to_json)
    else
        response = Configuration.update_resource(self.number, table = "incident").post(self.attributes.to_json)
    end
    hash = JSON.parse(response, { :symbolize_names => true })
    # this is the object
    # and there is always only one
    # since we're creating or updating
    inc_object = hash[:records][0]
    inc_object.each do |key, value|
        key_name = key.to_s
        eval("self.#{key_name} = value")
    end
    @saved_on_sn = true
    self
end