Class: OTRS

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
ActiveModel::Conversion, ActiveModel::Naming, ActiveModel::Validations
Defined in:
lib/otrs_connector/otrs.rb

Defined Under Namespace

Classes: Change, ConfigItem, GeneralCatalog, Link, Relation, Service, Ticket

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.api_urlObject



36
37
38
# File 'lib/otrs_connector/otrs.rb', line 36

def self.api_url
  @@otrs_api_url
end

.api_url=(url) ⇒ Object



39
40
41
# File 'lib/otrs_connector/otrs.rb', line 39

def self.api_url=(url)
  @@otrs_api_url = url
end

.connect(params, timeout = 60, retries = 4) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/otrs_connector/otrs.rb', line 94

def self.connect(params, timeout=60, retries=4)
  uri = self.setup_connection_params(params)
  retry_counter = 0
  # Connect to OTRS
  begin
  response = self.get_from_remote(uri, timeout)
  rescue EOFError
    retry_counter += 1
    puts "EOFError, Attempt: #{retry_counter+1}"
    retry if retry_counter < retries
    raise EOFError if retry_counter >= retries

  rescue Timeout::Error
    retry_counter += 1
    timeout = timeout*1.5
    puts "Timeout::Error Attempt: #{retry_counter+1}, Timeout #{timeout}"
    retry if retry_counter < retries
    raise Timeout::Error if retry_counter >= retries
  end
  return self.process_response(response)
end

.get_from_remote(uri, read_timeout = 60) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/otrs_connector/otrs.rb', line 73

def self.get_from_remote(uri, read_timeout=60)
  http = Net::HTTP.new(uri.host, uri.port)
  http.read_timeout = read_timeout
  if self.api_url =~ /^https/
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)
end

.object_preprocessor(object) ⇒ Object

Base method for processing objects returned by OTRS into Ruby objects This works in most cases, but not all, namely with OTRS::ConfigItem due to extra attributes



118
119
120
121
122
123
124
125
# File 'lib/otrs_connector/otrs.rb', line 118

def self.object_preprocessor(object)
  unless object.empty? or object.nil?
    a = Hash[*object]
    self.new(a.symbolize_keys)
  else
    raise 'NoSuchObject'
  end
end

.passwordObject



29
30
31
# File 'lib/otrs_connector/otrs.rb', line 29

def self.password
  @@otrs_pass
end

.password=(password) ⇒ Object



32
33
34
# File 'lib/otrs_connector/otrs.rb', line 32

def self.password=(password)
  @@otrs_pass = password
end

.process_response(response) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/otrs_connector/otrs.rb', line 84

def self.process_response(response)
  result = ActiveSupport::JSON::decode(response.body)
  if result["Result"] == 'successful'
    return result["Data"]
  else
    raise "Error:#{result["Result"]} #{result["Data"]}"
  end
end

.setup_connection_params(params) ⇒ Object

Handles communication with OTRS



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/otrs_connector/otrs.rb', line 54

def self.setup_connection_params(params)
  if self.api_url =~ /^https/
    require 'net/https'
  else
    require 'net/http'
  end
  
  base_url = self.api_url
  # Build request URL
  logon = URI.encode("User=#{self.user}&Password=#{self.password}")
  object = URI.encode(params[:object])
  method = URI.encode(params[:method])
  data = params[:data].to_json
  data = URI.encode(data)
  # Had some issues with certain characters not being escaped properly and causing JSON issues
  data = URI.escape(data, '=\',\\/+-&?#.;')
  URI.parse("#{base_url}?#{logon}&Object=#{object}&Method=#{method}&Data=#{data}")
end

.userObject



22
23
24
# File 'lib/otrs_connector/otrs.rb', line 22

def self.user
  @@otrs_user
end

.user=(username) ⇒ Object



25
26
27
# File 'lib/otrs_connector/otrs.rb', line 25

def self.user=(username)
  @@otrs_user = username
end

Instance Method Details

#attributesObject

Convert object’s instance variables to a hash



44
45
46
47
48
49
50
# File 'lib/otrs_connector/otrs.rb', line 44

def attributes
  attributes = {}
  self.instance_variables.each do |v|
    attributes[v.to_s.gsub('@','').to_sym] = self.instance_variable_get(v)
  end
  attributes
end

#connect(params) ⇒ Object

Not sure why this is here



128
129
130
# File 'lib/otrs_connector/otrs.rb', line 128

def connect(params)
  self.class.connect(params)
end