otrs_connector 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.1
@@ -18,27 +18,11 @@ class OTRS::Change < OTRS
18
18
  :change_free_text13, :change_free_key31, :accounted_time, :priority_id,
19
19
  :change_free_key40, :change_free_text4, :change_free_key4, :change_free_key26,
20
20
  :change_free_key42, :change_free_key23, :change_free_key30, :change_free_key36,
21
- :planned_end_time,
22
- :change_free_key49,
23
- :change_free_key32,
24
- :change_free_text48,
25
- :change_free_text28,
26
- :work_order_i_ds,
27
- :change_free_key20,
28
- :change_free_text14,
29
- :change_free_key29,
30
- :change_free_text7,
31
- :change_free_text27,
32
- :change_free_text18,
33
- :change_number,
34
- :planned_effort,
35
- :change_free_key37,
36
- :change_free_text11,
37
- :justification,
38
- :impact,
39
- :change_free_key41,
40
- :change_free_text22,
41
- :change_free_key10,
21
+ :planned_end_time, :change_free_key49, :change_free_key32, :change_free_text48,
22
+ :change_free_text28, :work_order_i_ds, :change_free_key20, :change_free_text14,
23
+ :change_free_key29, :change_free_text7, :change_free_text27, :change_free_text18,
24
+ :change_number, :planned_effort, :change_free_key37, :change_free_text11, :justification,
25
+ :impact, :change_free_key41, :change_free_text22, :change_free_key10,
42
26
  :change_free_key19,
43
27
  :create_by,
44
28
  :change_free_text50,
@@ -82,11 +66,6 @@ class OTRS::Change < OTRS
82
66
  :change_free_text32,
83
67
  :description
84
68
 
85
-
86
- #def self.set_accessor(key)
87
- # attr_accessor key.to_sym
88
- #end
89
-
90
69
  def persisted?
91
70
  false
92
71
  end
@@ -98,14 +77,6 @@ class OTRS::Change < OTRS
98
77
  end
99
78
  end
100
79
 
101
- def attributes
102
- attributes = {}
103
- self.instance_variables.each do |v|
104
- attributes[v.to_s.gsub('@','').to_sym] = self.instance_variable_get(v)
105
- end
106
- attributes
107
- end
108
-
109
80
  def save
110
81
  self.create(self.attributes)
111
82
  end
@@ -1,5 +1,7 @@
1
1
  class OTRS::ConfigItem < OTRS
2
+ # I did not convert fields in this classes to underscores because of the wide range of names possible in the OTRS's custom fields for ConfigItems. In attempting to do so I kept running into issues, even in just our setup, so to KISS I'm just pulling in the fields directly
2
3
 
4
+ # Field Names that are part of all ConfigItem objects, not stored in XMLData table
3
5
  @@builtin_fields = [:Name,:DeplStateID,:InciStateID,:DefinitionID,
4
6
  :CreateTime,:ChangeBy,:ChangeTime,:Class,:ClassID,:ConfigItemID,:CreateBy,:CreateTime,
5
7
  :CurDeplState,:CurDeplStateID,:CurDeplStateType,:CurInciState,:CurInciStateID,:CurInciStateType,
@@ -28,14 +30,6 @@ class OTRS::ConfigItem < OTRS
28
30
  end
29
31
  end
30
32
 
31
- def attributes
32
- attributes = {}
33
- self.instance_variables.each do |v|
34
- attributes[v.to_s.gsub('@','').to_sym] = self.instance_variable_get(v)
35
- end
36
- attributes
37
- end
38
-
39
33
  def save
40
34
  self.create
41
35
  end
@@ -59,6 +53,7 @@ class OTRS::ConfigItem < OTRS
59
53
  config_item
60
54
  end
61
55
 
56
+ # Converts search hash for search itmes that ar enot the in @@builtin_fields to OTRS XMLData searches
62
57
  def self.xml_search(attributes)
63
58
  what = []
64
59
  attributes.each do |key,value|
@@ -69,6 +64,7 @@ class OTRS::ConfigItem < OTRS
69
64
  what
70
65
  end
71
66
 
67
+ # Custom object processor because of XMLData
72
68
  def self.object_preprocessor(object)
73
69
  unless object.nil? or object.empty?
74
70
  xml = self.from_otrs_xml(object['XMLData'])
@@ -78,12 +74,14 @@ class OTRS::ConfigItem < OTRS
78
74
  end
79
75
  end
80
76
 
77
+ # Find by ConfigItemID
81
78
  def self.find(id)
82
79
  data = { 'ConfigItemID' => id, 'XMLDataGet' => 1 }
83
80
  params = { :object => 'ConfigItemObjectCustom', :method => 'VersionGet', :data => data }
84
81
  self.object_preprocessor (connect(params).first)
85
82
  end
86
83
 
84
+ # Find by Version ID
87
85
  def self.find_version(id)
88
86
  data = { 'VersionID' => id, 'XMLDataGet' => 1 }
89
87
  params = { :object => 'ConfigItemObject', :method => 'VersionGet', :data => data }
@@ -110,6 +108,7 @@ class OTRS::ConfigItem < OTRS
110
108
  results
111
109
  end
112
110
 
111
+ # Get history of CI object, returns as CI's... may want to create a new class, subclassed from this one called ConfigItemHistoryEntry, or some such, but this works for now.
113
112
  def get_history
114
113
  data = { :ConfigItemID => self.id, 'XMLDataGet' => 1 }
115
114
  params = { :object => 'ConfigItemObjectCustom', :method => 'VersionList', :data => data }
@@ -121,6 +120,8 @@ class OTRS::ConfigItem < OTRS
121
120
  return b
122
121
  end
123
122
 
123
+
124
+ # Convert non-builtin fields to OTRS's XMLData structure
124
125
  def self.to_otrs_xml(attributes)
125
126
  xml = attributes.except(:Name,:DeplStateID,:InciStateID,:DefinitionID,
126
127
  :CreateTime,:ChangeBy,:ChangeTime,:Class,:ClassID,:ConfigItemID,:CreateBy,:CreateTime,
@@ -136,6 +137,7 @@ class OTRS::ConfigItem < OTRS
136
137
  # Order keys properly so they are parsed in the correct order
137
138
  tmp.sort! { |x,y| x <=> y }
138
139
  tmp.each do |key|
140
+ # In some cases we created special field names because there were multiple fields with the same name. Fields with the "__" are these special fields and need to be handled specially
139
141
  keys = key.split(/__/)
140
142
  xml_key = keys[0]
141
143
  unless keys[1].nil? then tag_key = keys[1].gsub(/^0/,'').to_i + 1 end
@@ -192,7 +194,10 @@ class OTRS::ConfigItem < OTRS
192
194
  config_item
193
195
  end
194
196
 
197
+
198
+ # Convert OTRS XMLData structure to our object structure
195
199
  def self.from_otrs_xml(xml)
200
+ # OTRS Allows multiples of the same field name. To handle this, and to make sure we pull all the fields these fields are being handled specially. Fields with __keyname__count are these fields
196
201
  xml = xml[1].flatten[1][1].except("TagKey")
197
202
  data = {}
198
203
  xml.each do |key,value|
@@ -235,10 +240,4 @@ class OTRS::ConfigItem < OTRS
235
240
  data
236
241
  end
237
242
 
238
- #def self.from_otrs_xml(xml)
239
- # data = { :XMLHash => xml }
240
- # params = { :object => 'XMLObject', :method => 'XMLHash2D', :data => data }
241
- # a = Hash[*(self.connect(params))]
242
- #end
243
-
244
243
  end
@@ -1,4 +1,6 @@
1
1
  class OTRS::GeneralCatalog < OTRS
2
+ # Really recommend reading OTRS API Documentation for the GeneralCatalog
3
+
2
4
  attr_accessor :name, :change_time, :change_by, :valid_id, :create_time, :item_id, :comment, :create_by
3
5
  def self.set_accessor(key)
4
6
  attr_accessor key.to_sym
@@ -14,15 +14,7 @@ class OTRS::Link < OTRS
14
14
  send("#{name.to_s.underscore.to_sym}=", value)
15
15
  end
16
16
  end
17
-
18
- def attributes
19
- attributes = {}
20
- self.instance_variables.each do |v|
21
- attributes[v.to_s.gsub('@','').to_sym] = self.instance_variable_get(v)
22
- end
23
- attributes
24
- end
25
-
17
+
26
18
  def save
27
19
  self.class.create(self.attributes)
28
20
  end
@@ -51,9 +43,10 @@ class OTRS::Link < OTRS
51
43
  end
52
44
  end
53
45
 
46
+
47
+ # Returns list of link objects as Source => Target
48
+ # Haven't decided if I want this to return the link object or what is being linked to
54
49
  def self.where(attributes)
55
- # Returns list of link objects as Source => Target
56
- # Haven't decided if I want this to return the link object or what is being linked to
57
50
  attributes[:state] ||= 'Valid'
58
51
  tmp = {}
59
52
  attributes.each do |key,value|
@@ -1,4 +1,8 @@
1
1
  class OTRS::Relation < Array
2
+ # OTRS::Relation is my attempt at making something similar to ActiveRecord::Relation
3
+ # There is still a lot to do here.
4
+ # Right now where chains don't work as AR does, the first in the chain connects to OTRS, the rest of the chained items are parsing through the returned objects... somtimes this is faster, but the more objects get returned the slower it will be.
5
+
2
6
  require 'will_paginate/array'
3
7
 
4
8
  #attr_accessor :where_hash, :connect_params
@@ -26,6 +30,7 @@ class OTRS::Relation < Array
26
30
  # a.collect { |b| b.collect {|c| OTRS.object_preprocessor c }}
27
31
  #end
28
32
 
33
+ # Allows chaining where methods, this method only parses the already returned objects from OTRS, currently. In the future I hope to have this grouping the where chains together into one OTRS request for all in the chain
29
34
  def where(attributes)
30
35
  relation = self.class.new
31
36
  attributes.each do |lookup_key,lookup_value|
@@ -44,6 +49,7 @@ class OTRS::Relation < Array
44
49
  self[0...int]
45
50
  end
46
51
 
52
+ # This is hit and miss for some reason... sometimes the order works correctly, sometimes it's backwards (desc/asc)
47
53
  def order(field='id',order='desc')
48
54
  case order.downcase.to_s
49
55
  when 'asc'
@@ -53,6 +59,7 @@ class OTRS::Relation < Array
53
59
  end
54
60
  end
55
61
 
62
+ # Removes duplicate records
56
63
  def uniqify
57
64
  ids = []
58
65
  results = self.class.new
@@ -3,10 +3,6 @@ class OTRS::Service < OTRS
3
3
  :cur_inci_state, :create_by, :cur_inci_state_type_from_c_is, :change_time, :change_by, :create_time,
4
4
  :criticality, :comment, :criticality, :name_short, :type_id, :name, :parent_id, :cur_inci_state_id,
5
5
  :criticality_id
6
-
7
- #def self.set_accessor(key)
8
- # attr_accessor key.to_sym
9
- #end
10
6
 
11
7
  def persisted?
12
8
  false
@@ -14,7 +10,6 @@ class OTRS::Service < OTRS
14
10
 
15
11
  def initialize(attributes = {})
16
12
  attributes.each do |name, value|
17
- #self.class.set_accessor(name.to_s.underscore)
18
13
  send("#{name.to_s.underscore.to_sym}=", value)
19
14
  end
20
15
  end
@@ -24,14 +19,6 @@ class OTRS::Service < OTRS
24
19
  params = { :object => 'ServiceObject', :method => 'ServiceGet', :data => data }
25
20
  self.object_preprocessor self.connect(params)
26
21
  end
27
-
28
- def attributes
29
- attributes = {}
30
- self.instance_variables.each do |v|
31
- attributes[v.to_s.gsub('@','').to_sym] = self.instance_variable_get(v)
32
- end
33
- attributes
34
- end
35
22
 
36
23
  def save
37
24
  self.create(self.attributes)
@@ -10,15 +10,6 @@ class OTRS::Ticket::Article < OTRS::Ticket
10
10
  send("#{name.to_s.underscore.to_sym}=", value)
11
11
  end
12
12
  end
13
-
14
- def attributes
15
- attributes = {}
16
- self.instance_variables.each do |v|
17
- attributes[v.to_s.gsub('@','').to_sym] = self.instance_variable_get(v)
18
- end
19
- attributes
20
- end
21
-
22
13
  def save
23
14
  self.create(self.attributes)
24
15
  end
@@ -26,17 +26,12 @@ class OTRS::Ticket < OTRS
26
26
  self.ticket_id
27
27
  end
28
28
 
29
- #def self.set_accessor(key)
30
- # attr_accessor key.to_sym
31
- #end
32
-
33
29
  def persisted?
34
30
  false
35
31
  end
36
32
 
37
33
  def initialize(attributes = {})
38
34
  attributes.each do |name, value|
39
- #OTRS::Ticket.set_accessor(name.to_s.underscore)
40
35
  send("#{name.to_s.underscore.to_sym}=", value)
41
36
  end
42
37
  end
@@ -47,14 +42,6 @@ class OTRS::Ticket < OTRS
47
42
  connect(params).first
48
43
  end
49
44
 
50
- def attributes
51
- attributes = {}
52
- self.instance_variables.each do |v|
53
- attributes[v.to_s.gsub('@','').to_sym] = self.instance_variable_get(v)
54
- end
55
- attributes
56
- end
57
-
58
45
  def save
59
46
  self.create(self.attributes)
60
47
  end
@@ -1,16 +1,21 @@
1
1
  class OTRS
2
+ # This class is mostly used for inheritance purposes
3
+ # All subclasses will be able to directly connect to OTRS
4
+ # All subclasses have active_record style callbacks
5
+
6
+ # Include stuff for calbacks, validations
2
7
  include ActiveModel::Conversion
3
8
  include ActiveModel::Naming
4
9
  include ActiveModel::Validations
5
10
  extend ActiveModel::Callbacks
6
11
 
12
+ # Create callbacks on before/after create/save/update
7
13
  define_model_callbacks :create, :update, :save
8
14
 
9
- # @@otrs_host is the address where the OTRS server presides
10
15
  # api_url is the base URL used to connect to the json api of OTRS, this will be the custom json.pl as the standard doesn't include ITSM module
11
16
  @@otrs_api_url ||= "https://loalhost/otrs/json.pl"
17
+
12
18
  # Username / password combo should be an actual OTRS agent defined on the OTRS server
13
- # I have not tested this with other forms of OTRS authentication
14
19
  @@otrs_user ||= 'rails'
15
20
  @@otrs_pass ||= 'rails'
16
21
 
@@ -35,6 +40,7 @@ class OTRS
35
40
  @@otrs_api_url = url
36
41
  end
37
42
 
43
+ # Convert object's instance variables to a hash
38
44
  def attributes
39
45
  attributes = {}
40
46
  self.instance_variables.each do |v|
@@ -43,17 +49,22 @@ class OTRS
43
49
  attributes
44
50
  end
45
51
 
52
+ # Handles communication with OTRS
46
53
  def self.connect(params)
47
54
  require 'net/https'
48
55
  base_url = self.api_url
56
+
57
+ # Build request URL
49
58
  logon = URI.encode("User=#{self.user}&Password=#{self.password}")
50
59
  object = URI.encode(params[:object])
51
60
  method = URI.encode(params[:method])
52
61
  data = params[:data].to_json
53
62
  data = URI.encode(data)
63
+ # Had some issues with certain characters not being escaped properly and causing JSON issues
54
64
  data = URI.escape(data, '=\',\\/+-&?#.;')
55
65
  uri = URI.parse("#{base_url}?#{logon}&Object=#{object}&Method=#{method}&Data=#{data}")
56
-
66
+
67
+ # Connect to OTRS
57
68
  http = Net::HTTP.new(uri.host, uri.port)
58
69
  http.use_ssl = true
59
70
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
@@ -67,6 +78,8 @@ class OTRS
67
78
  end
68
79
  end
69
80
 
81
+ # Base method for processing objects returned by OTRS into Ruby objects
82
+ # This works in most cases, but not all, namely with OTRS::ConfigItem due to extra attributes
70
83
  def self.object_preprocessor(object)
71
84
  unless object.empty? or object.nil?
72
85
  a = Hash[*object]
@@ -76,9 +89,8 @@ class OTRS
76
89
  end
77
90
  end
78
91
 
79
-
92
+ # Not sure why this is here
80
93
  def connect(params)
81
94
  self.class.connect(params)
82
95
  end
83
96
  end
84
- #require_rel 'otrs'
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "otrs_connector"
8
- s.version = "1.0.0"
8
+ s.version = "1.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Brian Goff"]
12
- s.date = "2012-05-14"
12
+ s.date = "2012-05-15"
13
13
  s.description = "Connect your RAILS app to OTRS/ITSM"
14
14
  s.email = "cpuguy83@gmail.com"
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: otrs_connector
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-14 00:00:00.000000000 Z
12
+ date: 2012-05-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -155,7 +155,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
155
155
  version: '0'
156
156
  segments:
157
157
  - 0
158
- hash: 3376265551325201422
158
+ hash: -3777876175560197454
159
159
  required_rubygems_version: !ruby/object:Gem::Requirement
160
160
  none: false
161
161
  requirements: