otrs_proxy 0.0.2

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.
@@ -0,0 +1,189 @@
1
+ module OtrsConnector
2
+ class OTRS::ConfigItem < OTRS
3
+
4
+ def initialize(attributes = {})
5
+ attributes.each do |name, value|
6
+ # cannot have numbers at beginning of field name
7
+ unless name =~ /^\d+/ or name =~ / / or name =~ /-/
8
+ self.class.set_accessor(name)
9
+ send("#{name.to_sym}=", value)
10
+ end
11
+ end
12
+ end
13
+
14
+ def create(attributes)
15
+ #params = "Object=ConfigItemObject&Method=ConfigItemAdd&Data={\"ClassID\":\"#{self.ClassID}\",\"UserID\":\"1\"}"
16
+ data = { 'ClassID' => self.ClassID, 'UserID' => 1 }
17
+ params = { :object => 'ConfigItemObject', :method => 'ConfigItemAdd', :data => data }
18
+ a = self.class.connect(params)
19
+ attributes[:ConfigItemID] = a.first
20
+ attributes[:XMLData] = self.class.to_otrs_xml(attributes)
21
+ data2 = attributes
22
+ #params2 = "Object=ConfigItemObject&Method=VersionAdd&Data=#{data}"
23
+ params2 = { :object => 'ConfigItemObject', :method => 'VersionAdd', :data => data2 }
24
+ b = self.class.connect(params2)
25
+ new_version_id = b.first
26
+ config_item = self.class.find(attributes[:ConfigItemID])
27
+ attributes = config_item.attributes
28
+ attributes.each do |key,value|
29
+ instance_variable_set "@#{key.to_s}", value
30
+ end
31
+ config_item
32
+ end
33
+
34
+ def self.where(attributes)
35
+ tmp = {}
36
+ attributes.each do |key,value|
37
+ tmp[key.to_s.camelize.to_sym] = value
38
+ end
39
+ data = tmp
40
+ #params = "Object=ConfigItemObject&Method=ConfigItemSearchExtended&Data=#{data}"
41
+ params = { :object => 'ConfigItemObject', :method => 'ConfigItemSearchExtended', :data => data }
42
+ a = connect(params).flatten
43
+ results = []
44
+ a.each do |b|
45
+ results << find(b)
46
+ end
47
+ results
48
+ end
49
+
50
+ def self.to_otrs_xml(attributes)
51
+ xml = attributes.except(:Name,:DeplStateID,:InciStateID,:DefinitionID,
52
+ :CreateTime,:ChangeBy,:ChangeTime,:Class,:ClassID,:ConfigItemID,:CreateBy,:CreateTime,
53
+ :CurDeplState,:CurDeplStateID,:CurDeplStateType,:CurInciState,:CurInciStateID,:CurInciStateType,
54
+ :DeplState,:DeplStateType,:InciState,:InciStateType,:LastVersionID,:Number,:VersionID)
55
+ xml_hash = {}
56
+ xml_data = [nil, { 'Version' => xml_hash }]
57
+ tmp = []
58
+ xml.each do |key,value|
59
+ key = key.to_s
60
+ tmp << key
61
+ end
62
+ # Order keys properly so they are parsed in the correct order
63
+ tmp.sort! { |x,y| x <=> y }
64
+ tmp.each do |key|
65
+ keys = key.split(/__/)
66
+ xml_key = keys[0]
67
+ unless keys[1].nil? then tag_key = keys[1].gsub(/^0/,'').to_i + 1 end
68
+ xml_subkey = keys[2]
69
+ case key
70
+ when /^[aA-zZ]+__0\d+__[aA-zZ]+__0\d+$/
71
+ if xml_hash[xml_key][tag_key][xml_subkey].nil?
72
+ xml_hash[xml_key][tag_key][xml_subkey] = [nil, { "Content" => xml[key.to_sym] }]
73
+ else
74
+ xml_hash[xml_key][tag_key][xml_subkey] << { "Content" => xml[key.to_sym] }
75
+ end
76
+ when /^[aA-zZ]+__0\d+__[aA-zZ]$/
77
+ xml_hash[xml_key][tag_key][xml_subkey] = xml[key.to_sym]
78
+ when /^[aA-zZ]+__0\d+$/
79
+ if xml_hash[xml_key].nil?
80
+ xml_hash[xml_key] = [nil]
81
+ end
82
+ xml_hash[xml_key] << { "Content" => xml[key.to_sym] }
83
+ when /^[aA-zZ]+__[aA-zZ]$/
84
+ xml_hash[xml_key][1][xml_subkey] = xml[key.to_sym]
85
+ when /^[aA-zZ]+$/
86
+ xml_hash[xml_key] = [ nil, { "Content" => xml[key.to_sym] }]
87
+ end
88
+ end
89
+ xml_data
90
+ end
91
+
92
+ def update_attributes(updated_attributes)
93
+ self.attributes.each do |key,value|
94
+ if updated_attributes[key].nil?
95
+ updated_attributes[key] = value
96
+ end
97
+ end
98
+ updated_attributes[:XMLData] = self.class.to_otrs_xml(updated_attributes)
99
+ xml_attributes = self.attributes.except(:Name,:DeplStateID,:InciStateID,:DefinitionID,
100
+ :CreateTime,:ChangeBy,:ChangeTime,:Class,:ClassID,:ConfigItemID,:CreateBy,:CreateTime,
101
+ :CurDeplState,:CurDeplStateID,:CurDeplStateType,:CurInciState,:CurInciStateID,:CurInciStateType,
102
+ :DeplState,:DeplStateType,:InciState,:InciStateType,:LastVersionID,:Number,:VersionID)
103
+ xml_attributes.each do |key,value|
104
+ updated_attributes = updated_attributes.except(key)
105
+ end
106
+ data = updated_attributes
107
+ params = "Object=ConfigItemObject&Method=VersionAdd&Data=#{data}"
108
+ params = { :object => 'ConfigItemObject', :method => 'VersionAdd', :data => data }
109
+ a = self.class.connect(params)
110
+ new_version_id = a.first
111
+ params2 = "Object=ConfigItemObject&Method=VersionConfigItemIDGet&Data={\"VersionID\":\"#{new_version_id}\"}"
112
+ data2 = { 'VersionID' => new_version_id }
113
+ params2 = { :object => 'ConfigItemObject', :method => 'VersionConfigItemIDGet', :data => data2 }
114
+ b = self.class.connect(params2)
115
+ config_item = self.class.find(b.first)
116
+ attributes = config_item.attributes
117
+ attributes.each do |key,value|
118
+ instance_variable_set "@#{key.to_s}", value
119
+ end
120
+ config_item
121
+ end
122
+
123
+ def self.from_otrs_xml(xml)
124
+ xml = xml.first[1].flatten[1][1].except("TagKey")
125
+ data = {}
126
+ xml.each do |key,value|
127
+ xml[key].delete(xml[key][0])
128
+ count = xml[key].count
129
+ if count == 1
130
+ data[key] = value[count - 1]["Content"]
131
+ count2 = value[count -1].except("Content","TagKey").count
132
+ if count2 >= 1
133
+ value[count - 1].except("Content","TagKey").each do |key2,value2|
134
+ value2.delete(value2[0])
135
+ data["#{key}__#{key2}"] = value2[0]["Content"]
136
+ end
137
+ end
138
+ else
139
+ while count != 0
140
+ data["#{key}__0#{count - 1}"] = value[count - 1]["Content"]
141
+ count3 = value[count - 1].except("TagKey").count
142
+ if count3 > 1
143
+ value[count - 1].except("Content","TagKey").each do |key3,value3|
144
+ value3.delete(value3[0])
145
+ count4 = value3.count
146
+ if count4 > 1
147
+ while count4 != 0
148
+ unless value3[count4 - 1]["Content"].nil?
149
+ data["#{key}__0#{count - 1}__#{key3}__0#{count4 - 1}"] = value3[count4 - 1]["Content"]
150
+ end
151
+ count4 = count4 - 1
152
+ end
153
+
154
+ else
155
+ data["#{key}__0#{count - 1}__#{key3}"] = value3[0]["Content"]
156
+ end
157
+ end
158
+ end
159
+ count = count - 1
160
+ end
161
+ end
162
+ end
163
+ data
164
+ end
165
+
166
+ def self.lookup(id)
167
+ data = { 'ConfigItemNumber' => id }
168
+ params = { :object => 'ITSMConfigItemObject', :method => 'ConfigItemLookup', :data => data }
169
+ connect(params).first
170
+ end
171
+
172
+ def self.find(id)
173
+ data = { 'ConfigItemID' => id }
174
+ params = { :object => 'ConfigItemObject', :method => 'ConfigItemGet', :data => data }
175
+ a = connect(params).first
176
+ class_id = a["ClassID"]
177
+ version_id = a["LastVersionID"]
178
+ data2 = { 'ClassID' => class_id, 'VersionID' => version_id }
179
+ params2 = { :object => 'ConfigItemObject', :method => '_XMLVersionGet', :data => data2 }
180
+ b = connect(params2)
181
+ b = self.from_otrs_xml(b)
182
+ data3 = { 'ConfigItemID' => id, 'XMLDataGet' => 0 }
183
+ params3 = { :object => 'ConfigItemObject', :method => 'VersionGet', :data => data3 }
184
+ c = connect(params3).first
185
+ self.new(a.merge(c).merge(b))
186
+ end
187
+
188
+ end
189
+ end
@@ -0,0 +1,15 @@
1
+ module OtrsConnector
2
+ class OTRS::GeneralCatalog < OTRS
3
+
4
+ def self.find(id)
5
+ data = { 'ItemID' => id }
6
+ params = { :object => 'GeneralCatalogObject', :method => 'ItemGet', :data => data }
7
+ a = connect(params)
8
+ unless a.first.nil?
9
+ a = a.first.except('Class') ## Class field is causing issues in Rails
10
+ end
11
+ self.new(a)
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,34 @@
1
+ module OtrsConnector
2
+ class OTRS::Group < OTRS
3
+
4
+ def self.find(id)
5
+ data = { 'ID' => id }
6
+ params = { :object => 'UserObject', :method => 'GroupGet', :data => data }
7
+ a = Hash[*connect(params)]
8
+ if a.empty? == false
9
+ self.new(a.symbolize_keys)
10
+ else
11
+ raise "ERROR::NoSuchID #{id}"
12
+ end
13
+ end
14
+
15
+ def self.where(attributes, only_ids = false)
16
+ tmp = {}
17
+ attributes.each do |key,value|
18
+ tmp[key.to_s.camelize.to_sym] = value
19
+ end
20
+ attributes = tmp
21
+ data = attributes
22
+ params = { :object => 'GroupObject', :method => 'GroupMemberList', :data => data }
23
+ a = connect(params)
24
+ a = Hash[*a]
25
+ return a.keys if only_ids
26
+ results = []
27
+ a.each do |key, value|
28
+ results << find(key)
29
+ end
30
+ results
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,62 @@
1
+ module OtrsConnector
2
+ class OTRS::Link < OTRS
3
+
4
+ def self.create(attributes)
5
+ attributes["State"] ||= 'Valid'
6
+ params = { :object => 'LinkObject', :method => 'LinkAdd', :data => attributes }
7
+ a = connect(params)
8
+ if a.first == "1"
9
+ return self
10
+ else
11
+ nil
12
+ end
13
+ end
14
+
15
+ def self.where_with_data(attributes)
16
+ attributes["State"] ||= 'Valid'
17
+ attributes["Object"] ||= "ITSMConfigItem"
18
+ params = { :object => 'LinkObject', :method => 'LinkListWithData', :data => attributes }
19
+ a = connect(params).collect do |item|
20
+ next if item.blank?
21
+ OTRS::Ticket.new(item['Ticket']['RelevantTo']['Source'].values.first.symbolize_keys) rescue nil
22
+ end.compact
23
+ end
24
+
25
+ def self.where(attributes)
26
+ # Returns list of link objects as Source => Target
27
+ # Haven't decided if I want this to return the link object or what is being linked to
28
+ attributes[:state] ||= 'Valid'
29
+ tmp = {}
30
+ attributes.each do |key,value|
31
+ tmp[key.to_s.camelize.to_sym] = value
32
+ end
33
+ data = tmp
34
+ params = { :object => 'LinkObject', :method => 'LinkKeyList', :data => data }
35
+ a = connect(params)
36
+ a = Hash[*a]
37
+ b = []
38
+ a.each do |key,value|
39
+ c = {}
40
+ c[:key2] = "#{key}"
41
+ c[:object2] = tmp[:Object2]
42
+ c[:object1] = tmp[:Object1]
43
+ c[:key1] = tmp[:Key1]
44
+ b << self.new(c)
45
+ end
46
+ b
47
+ end
48
+
49
+ def where(attributes)
50
+ self.class.where(attributes)
51
+ end
52
+
53
+ def destroy
54
+ @type ||= 'Normal'
55
+ data = { 'Object1' => @object1, :key1 => @key1, :object2 => @object2, key2 => @key2, :type => @type }
56
+ #params = "Object=LinkObject&Method=LinkDelete&Data={\"Object1\":\"#{@object1}\",\"Key1\":\"#{@key1}\",\"Object2\":\"#{@object2}\",\"Key2\":\"#{@key2}\",\"Type\":\"#{@type}\"}"
57
+ params = { :object => 'LinkObject', :method => 'LinkDelete', :data => data }
58
+ a = connect(params)
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,78 @@
1
+ module OtrsConnector
2
+ class OTRS::Service < OTRS
3
+
4
+ def self.find(id)
5
+ #params = "Object=ServiceObject&Method=ServiceGet&Data={\"ServiceID\":\"#{id}\",\"UserID\":\"1\"}"
6
+ data = { 'ServiceID' => id, 'UserID' => 1 }
7
+ params = { :object => 'ServiceObject', :method => 'ServiceGet', :data => data }
8
+ a = Hash[*connect(params)]
9
+ if a.empty? == false
10
+ self.new(a.symbolize_keys)
11
+ else
12
+ raise "ERROR::NoSuchID #{id}"
13
+ end
14
+ end
15
+
16
+ def create(attributes)
17
+ attributes[:valid_id] ||= 1
18
+ attributes[:user_id] ||= 1
19
+ attributes[:type_id] ||= 1
20
+ attributes[:criticality_id] ||= 3
21
+ tmp = {}
22
+ attributes.each do |key,value|
23
+ if key == :user_id
24
+ tmp[:UserID] = value
25
+ end
26
+ if key == :valid_id
27
+ tmp[:ValidID] = value
28
+ end
29
+ if key == :type_id
30
+ tmp[:TypeID] = value
31
+ end
32
+ if key == :criticality_id
33
+ tmp[:CriticalityID] = value
34
+ end
35
+ if key == :parent_id
36
+ tmp[:ParentID] = value
37
+ end
38
+ if key != :user_id or key != :valid_id or key != :type_id or key != :crticality_id or key != :parent_id
39
+ tmp[key.to_s.camelize.to_sym] = value
40
+ end
41
+ end
42
+ attributes = tmp
43
+ data = attributes
44
+ #params = "Object=ServiceObject&Method=ServiceAdd&Data=#{data}"
45
+ params = { :object => 'ServiceObject', :method => 'ServiceAdd', :data => data }
46
+ a = connect(params)
47
+ service_id = a.first
48
+ unless service_id.nil?
49
+ self.class.find(service_id)
50
+ else
51
+ raise "Could not create service"
52
+ end
53
+ service = self.class.find(service_id)
54
+ service.attributes.each do |key,value|
55
+ instance_variable_set "@#{key.to_s}", value
56
+ end
57
+ service
58
+ end
59
+
60
+ def self.where(attributes)
61
+ tmp = {}
62
+ attributes.each do |key,value|
63
+ tmp[key.to_s.camelize.to_sym] = value
64
+ end
65
+ attributes = tmp
66
+ data = attributes
67
+ #params = "Object=ServiceObject&Method=ServiceSearch&Data=#{data}"
68
+ params = { :object => 'ServiceObject', :method => 'ServiceSearch', :data => data }
69
+ a = connect(params)
70
+ results = []
71
+ a.each do |s|
72
+ results << find(s)
73
+ end
74
+ results
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,43 @@
1
+ module OtrsConnector
2
+ class OTRS::Ticket::Article < OTRS::Ticket
3
+
4
+ def self.simple_create!(data)
5
+ params = { :object => 'TicketObject', :method => 'ArticleCreate', :data => data }
6
+ a = connect(params)
7
+ a.first.nil? ? nil : a
8
+ end
9
+
10
+ def create(attributes)
11
+ data = { 'TicketID' => attributes[:ticket_id], 'From' => attributes[:email], 'Subject' => attributes[:title], 'Body' => attributes[:body] }
12
+ data['ArticleType'] ||= 'email-external'
13
+ data['UserID'] ||= 1
14
+ data['SenderType'] ||= 'agent'
15
+ data['HistoryType'] ||= 'NewTicket'
16
+ data['HistoryComment'] ||= ' '
17
+ data['ContentType'] ||= 'text/plain'
18
+ params = { :object => 'TicketObject', :method => 'ArticleCreate', :data => data }
19
+ a = connect(params)
20
+ if a.first.nil? then nil else a end
21
+ end
22
+
23
+ def self.find(id)
24
+ data = { 'ArticleID' => id, 'UserID' => 1 }
25
+ params = { :object => 'TicketObject', :method => 'ArticleGet', :data => data }
26
+ a = connect(params)
27
+ a = Hash[*a].symbolize_keys
28
+ self.new(a)
29
+ end
30
+
31
+ def self.where(ticket_id)
32
+ data = { 'TicketID' => ticket_id }
33
+ params = { :object => 'TicketObject', :method => 'ArticleIndex', :data => data }
34
+ a = connect(params)
35
+ b = []
36
+ a.each do |c|
37
+ b << find(c)
38
+ end
39
+ b
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,29 @@
1
+ module OtrsConnector
2
+ class OTRS::Ticket::State < OTRS::Ticket
3
+
4
+ def self.all
5
+ data = { 'UserID' => 1 }
6
+ #params = 'Object=StateObject&Method=StateList&Data={"UserID":"1"}'
7
+ params = { :object => 'StateObject', :method => 'StateList', :data => data }
8
+ a = connect(params)
9
+ a = Hash[*a]
10
+ b = []
11
+ a.each do |key,value|
12
+ c = {}
13
+ c[key] = value
14
+ b << c
15
+ end
16
+ c = []
17
+ b.each do |d|
18
+ d.each do |key,value|
19
+ tmp = {}
20
+ tmp[:id] = key
21
+ tmp[:name] = value
22
+ c << new(tmp)
23
+ end
24
+ end
25
+ c
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ module OtrsConnector
2
+ class OTRS::Ticket::TicketQueue < OTRS::Ticket # Namespace conflict with OTRS::Ticket::Queue
3
+
4
+ def self.all
5
+ data = { 'UserID' => 1 }
6
+ #params = 'Object=QueueObject&Method=QueueList&Data={"UserID":"1"}'
7
+ params = { :object => 'QueueObject', :method => 'QueueList', :data => data }
8
+ a = connect(params)
9
+ a = Hash[*a]
10
+ b = []
11
+ a.each do |key,value|
12
+ c = {}
13
+ c[key] = value
14
+ b << c
15
+ end
16
+ c = []
17
+ b.each do |d|
18
+ d.each do |key,value|
19
+ tmp = {}
20
+ tmp[:id] = key
21
+ tmp[:name] = value
22
+ c << new(tmp)
23
+ end
24
+ end
25
+ c
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ module OtrsConnector
2
+ class OTRS::Ticket::Type < OTRS::Ticket
3
+
4
+ def self.all
5
+ data = { 'UserID' => 1 }
6
+ #params = 'Object=TicketObject&Method=TicketTypeList&Data={"UserID":"1"}'
7
+ params = { :object => 'TicketObject', :method => 'TicketTypeList', :data => data }
8
+ a = connect(params)
9
+ a = Hash[*a]
10
+ b = []
11
+ a.each do |key,value|
12
+ c = {}
13
+ c[key] = value
14
+ b << c
15
+ end
16
+ c = []
17
+ b.each do |d|
18
+ d.each do |key,value|
19
+ tmp = {}
20
+ tmp[:id] = key
21
+ tmp[:name] = value
22
+ c << new(tmp)
23
+ end
24
+ end
25
+ c
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,148 @@
1
+ module OtrsConnector
2
+ class OTRS::Ticket < OTRS
3
+ # Validations aren't working
4
+ validates_presence_of :title
5
+ validates_presence_of :body
6
+ validates_presence_of :email
7
+
8
+ def self.free_text_get(type, user_id, ticket_id = nil)
9
+ data = { 'Type' => type, 'UserID' => user_id }
10
+ data.merge!({'TicketID' => ticket_id}) if ticket_id
11
+ params = { :object => 'TicketObject', :method => 'TicketFreeTextGet', :data => data }
12
+ connect(params).first
13
+ end
14
+
15
+ def self.free_text_set(user_id, ticket_id, key, value, counter)
16
+ data = {
17
+ "Counter" => counter,
18
+ "Key" => key,
19
+ "Value" => value,
20
+ "UserID" => user_id,
21
+ "TicketID" => ticket_id
22
+ }
23
+
24
+ params = { :object => 'TicketObject', :method => 'TicketFreeTextSet', :data => data }
25
+ connect(params).first
26
+ end
27
+
28
+ def self.ticket_number_lookup(ticket_id)
29
+ data = { 'TicketID' => ticket_id, 'UserID' => 1 }
30
+ #params = "Object=TicketObject&Method=TicketNumberLookup&Data={\"TicketID\":\"#{ticket_id}\",\"UserID\":\"1\"}"
31
+ params = { :object => 'TicketObject', :method => 'TicketNumberLookup', :data => data }
32
+ connect(params)
33
+ end
34
+
35
+ def self.simple_create!(data)
36
+ params = { :object => 'TicketObject', :method => 'TicketCreate', :data => data }
37
+ a = connect(params)
38
+ ticket_id = a.first
39
+ self.find(ticket_id)
40
+ end
41
+
42
+ def create(attributes)
43
+ attributes[:otrs_type] ||= 'Incident'
44
+ attributes[:state] ||= 'new'
45
+ attributes[:queue] ||= 'Service Desk'
46
+ attributes[:lock] ||= 'unlock'
47
+ attributes[:priority] ||= '3 normal'
48
+ attributes[:user_id] ||= '1'
49
+ attributes[:owner_id] ||= attributes[:user_id]
50
+ tmp = {}
51
+ attributes.each do |key,value|
52
+ if key == :otrs_type
53
+ tmp[:Type] = value
54
+ end
55
+ if key == :user_id
56
+ tmp[:UserID] = value
57
+ end
58
+ if key == :owner_id
59
+ tmp[:OwnerID] = value
60
+ end
61
+ if key == :customer_id
62
+ tmp[:CustomerID] = value
63
+ end
64
+ if key != :user_id or key != :owner_id or key != :customer_id
65
+ tmp[key.to_s.camelize.to_sym] = value
66
+ end
67
+
68
+ end
69
+ attributes = tmp
70
+ data = attributes
71
+ params = { :object => 'TicketObject', :method => 'TicketCreate', :data => data }
72
+ a = connect(params)
73
+ ticket_id = a.first
74
+ article = OTRS::Ticket::Article.new(
75
+ :ticket_id => ticket_id,
76
+ :body => attributes[:Body],
77
+ :email => attributes[:Email],
78
+ :title => attributes[:Title])
79
+ if article.save
80
+ ticket = self.class.find(ticket_id)
81
+ attributes = ticket.attributes
82
+ attributes.each do |key,value|
83
+ instance_variable_set "@#{key.to_s}", value
84
+ end
85
+ ticket
86
+ else
87
+ ticket.destroy
88
+ raise 'Could not create ticket'
89
+ end
90
+ end
91
+
92
+ def destroy
93
+ id = @ticket_id
94
+ if self.class.find(id)
95
+ data = { 'TicketID' => id, 'UserID' => 1 }
96
+ params = { :object => 'TicketObject', :method => 'TicketDelete', :data => data }
97
+ connect(params)
98
+ "Ticket ID: #{id} deleted"
99
+ else
100
+ raise 'Error:NoSuchID'
101
+ end
102
+ end
103
+
104
+ def self.find(id)
105
+ data = { 'TicketID' => id, 'UserID' => 1 }
106
+ #params = "Object=TicketObject&Method=TicketGet&Data={\"TicketID\":\"#{id}\",\"UserID\":\"1\"}"
107
+ params = { :object => 'TicketObject', :method => 'TicketGet', :data => data }
108
+ a = Hash[*connect(params)]
109
+ if a.empty? == false
110
+ self.new(a.symbolize_keys)
111
+ else
112
+ raise "ERROR::NoSuchID #{id}"
113
+ end
114
+ end
115
+
116
+
117
+ def self.where(attributes)
118
+ data = attributes
119
+ params = { :object => 'TicketObject', :method => 'TicketSearch', :data => data }
120
+ a = connect(params)
121
+ b = Hash[*a] # Converts array to hash where key = TicketID and value = TicketNumber, which is what gets returned by OTRS
122
+ c = []
123
+ b.each do |key,value| # Get just the ID values so we can perform a find on them
124
+ c << key
125
+ end
126
+ results = []
127
+ c.each do |t|
128
+ results << find(t) #Add find results to array
129
+ end
130
+ results # Return array of hashes. Each hash is one ticket record
131
+ end
132
+
133
+ def where(attributes)
134
+ self.class.where(attributes)
135
+ end
136
+
137
+ def self.delete_by_id(id)
138
+ if find(id)
139
+ data = { 'TicketID' => id, 'UserID' => 1 }
140
+ params = { :object => 'TicketObject', :method => 'TicketDelete', :data => data }
141
+ connect(params)
142
+ "Ticket ID: #{id} deleted"
143
+ else
144
+ raise 'Error:NoSuchID'
145
+ end
146
+ end
147
+ end
148
+ end