Method: RT_Client#initialize

Defined in:
rt_client.rb

#initialize(*params) ⇒ RT_Client

Create a new RT_Client object. Load up our stored cookie and check it. Log into RT again if needed and store the new cookie. You can specify login and cookie storage directories in 3 different ways:

  1. Explicity during object creation

  2. From a .rtclientrc file in the working directory of your ruby program

  3. From a .rtclientrc file in the same directory as the library itself

These are listed in order of priority; if you have explicit parameters, they are always used, even if you have .rtclientrc files. If there is both an .rtclientrc in your program’s working directory and in the library directory, the one from your program’s working directory is used. If no parameters are specified either explicity or by use of a .rtclientrc, then the defaults of “rt_user”, “rt_pass” are used with a default server of “localhost”, and cookies are stored in the directory where the library resides.

rt= RT_Client.new( :server  => "https://tickets.ambulance.com/",
                   :user    => "rt_user",
                   :pass    => "rt_pass",
                   :cookies => "/my/cookie/dir" )

rt= RT_Client.new # use defaults from .rtclientrc

.rtclientrc format:

server=<RT server>
user=<RT user>
pass=<RT password>
cookies=<directory>


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'rt_client.rb', line 68

def initialize(*params)
	@boundary = "----xYzZY#{rand(1000000).to_s}xYzZY"
	@version = "0.7.1"
	@status = "Not connected"
	@server = "http://localhost/"
	@user = "rt_user"
	@pass = "rt_pass"
	@cookies = Dir.pwd
	config_file = Dir.pwd + "/.rtclientrc"
	config = ""
	if File.file?(config_file)
		config = File.read(config_file)
	else
		config_file = File.dirname(__FILE__) + "/.rtclientrc"
		config = File.read(config_file) if File.file?(config_file)
	end
	@server = $~[1] if config =~ /\s*server\s*=\s*(.*)$/i
	@user = $~[1] if config =~ /^\s*user\s*=\s*(.*)$/i
	@pass = $~[1] if config =~ /^\s*pass\s*=\s*(.*)$/i
	@cookies = $~[1] if config =~ /\s*cookies\s*=\s*(.*)$/i
	@resource = "#{@server}REST/1.0/"
	if params.class == Array && params[0].class == Hash
		param = params[0]
		@user = param[:user] if param.has_key? :user
		@pass = param[:pass] if param.has_key? :pass
		if param.has_key? :server
			@server = param[:server]
			@server += "/" if @server !~ /\/$/
			@resource = "#{@server}REST/1.0/"
		end
		@cookies  = param[:cookies] if param.has_key? :cookies
	end
	@login = { :user => @user, :pass => @pass }
	cookiejar = "#{@cookies}/RT_Client.#{@user}.cookie" # cookie location
	cookiejar.untaint
	if File.file? cookiejar
		@cookie = File.read(cookiejar).chomp
		headers = { 'User-Agent'   => UA,
		'Content-Type' => "application/x-www-form-urlencoded",
		'Cookie'       => @cookie }
	else
		headers = { 'User-Agent'   => UA,
		'Content-Type' => "application/x-www-form-urlencoded" }
		@cookie = ""
	end


	site = RestClient::Resource.new(@resource, :headers => headers, :timeout => 240)
	data = site.post "" # a null post just to check that we are logged in

	if @cookie.length == 0 or data =~ /401/ # we're not logged in
		data = site.post @login, :headers => headers
		#      puts data
		@cookie = data.headers[:set_cookie].first.split('; ')[0]
		# write the new cookie
		if @cookie !~ /nil/
			f = File.new(cookiejar,"w")
			f.puts @cookie
			f.close
		end
	end
	headers = { 'User-Agent'   => UA,
	   'Content-Type' => "multipart/form-data; boundary=#{@boundary}",
	'Cookie'       => @cookie }
	@site = RestClient::Resource.new(@resource, :headers => headers)
	@status = data
	self.untaint
end