Class: Mu::HttpHelper

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/mu/http_helper.rb

Constant Summary

Constants included from Helper

Mu::Helper::ESCAPES

Instance Method Summary collapse

Methods included from Helper

#ask, #bin2hex, #error, #escape, #format_float, #get_file_as_string_array, #make_xml, #msg, #shift, #to_boolean

Constructor Details

#initialize(host, username, password, docroot) ⇒ HttpHelper

Returns a new instance of HttpHelper.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/mu/http_helper.rb', line 5

def initialize(host, username, password, docroot)
  # This check is to see if the user has set ENV variables or used the -m command line option
  if(host.nil? || username.nil? || password.nil? || docroot.nil?)
      raise "The host, username and/or password aren't defined. Please use the -m command line option '-m user:[email protected]' or set your MU environment variables MU_IP,MU_ADMIN_USER,MU_ADMIN_PASS"
  end

  @host     = host
  @username = username
  @password = password
  @docroot  = docroot
  
  $cookie = "" if $cookie.nil?
end

Instance Method Details

#delete(e) ⇒ Object

basic delete call

* e = the url suffix


190
191
192
193
# File 'lib/mu/http_helper.rb', line 190

def delete(e)
  url = "https://#{@username}:#{@password}@#{@host}#{@docroot}#{e}"
  return RestClient.delete(url)
end

#download_file(e, filename, p = {}) ⇒ Object

fetches a file and stores it locally

* e = the url suffix
* filename = the name to store the file locally
* p = hash of parameters, such as headers


222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/mu/http_helper.rb', line 222

def download_file(e, filename, p={})
  url = "https://#{@username}:#{@password}@#{@host}#{@docroot}#{e}"
  params = {}.merge! p
  params[:cookies] = $cookie if !$cookie.empty?
  resp = RestClient.get(url)
  $cookie = resp.cookies unless resp.cookies.empty?
  msg "got cookie #{$cookie}", Logger::DEBUG unless resp.cookies.empty?
  if resp.body.include?("Could not download")
      msg "==> Could not download the file"
      return -1
  end
  open(filename, "wb") { |f| f.write(resp.body) }
  return File.size(filename)
end

#get(e, p = {}) ⇒ Object

basic get call

* e = the url suffix
* p = hash of parameters, such as headers


24
25
26
27
28
29
30
31
32
33
# File 'lib/mu/http_helper.rb', line 24

def get(e, p={})
  url = "https://#{@username}:#{@password}@#{@host}#{@docroot}#{e}"
  msg url, Logger::DEBUG
  params = {}.merge! p
  params[:cookies] = $cookie if !$cookie.empty?
  resp = RestClient.get(url, params)
  $cookie = resp.cookies unless resp.cookies.empty?
  msg "got cookie #{$cookie}", Logger::DEBUG unless resp.cookies.empty?
  return resp
end

#get_json(e, p = {}) ⇒ Object

get call for json, converts the response to json if applicable

* e = the url suffix
* p = hash of parameters, such as headers


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mu/http_helper.rb', line 38

def get_json(e, p={})
  url = "https://#{@username}:#{@password}@#{@host}#{@docroot}#{e}"
  msg url, Logger::DEBUG
  params = {}.merge! p
  params[:cookies] = $cookie if !$cookie.empty?
  resp = RestClient.get(url, params)
  $cookie = resp.cookies unless resp.cookies.empty?
  msg "got cookie #{$cookie}", Logger::DEBUG unless resp.cookies.empty?
  begin
    msg resp, Logger::DEBUG
    jresp = JSON resp
    if jresp
      return jresp
    end
  rescue JSON::ParserError => e
     # nothing to do
  end
  return resp
end

#get_xml(e, p = {}) ⇒ Object

get call for xml, converts the response to xml if applicable

* e = the url suffix
* p = hash of parameters, such as headers


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mu/http_helper.rb', line 61

def get_xml(e, p={})
  url = "https://#{@username}:#{@password}@#{@host}#{@docroot}#{e}"
  msg url, Logger::DEBUG
  params = {}.merge! p
  params[:cookies] = $cookie if !$cookie.empty?
  resp = RestClient.get(url, params)
  $cookie = resp.cookies unless resp.cookies.empty?
  msg "got cookie #{$cookie}", Logger::DEBUG unless resp.cookies.empty?
  msg resp, Logger::DEBUG
  begin
    if (/<.+>/).match(resp)
      xmldoc = Nokogiri::XML(resp)
    else
      err_node = Nokogiri::XML::Node.new('err_node', xmldoc)
      err_node.content = resp
      xmldoc.root << err_node
    end
  rescue =>  e
    msg "Error parsing XML " + e.to_s, Logger::DEBUG
  ensure
    msg xmldoc, Logger::DEBUG
    return xmldoc
  end
end

#post(e, body = "", p = {}) ⇒ Object

basic post call

* e = the url suffix
* body = the data to post
* p = hash of parameters, such as headers


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/mu/http_helper.rb', line 90

def post(e, body="", p = {})
  url = "https://#{@username}:#{@password}@#{@host}#{@docroot}#{e}"
  msg "#{url}  #{body}", Logger::DEBUG
  params = {}.merge! p
  params[:cookies] = $cookie if !$cookie.empty?
  msg("using cookie #{$cookie}", Logger::DEBUG) if !$cookie.empty?  
  resp = RestClient.post(url, body, params)
  # msg resp.headers
  $cookie = resp.cookies unless resp.cookies.empty?
  msg "got cookie #{$cookie}", Logger::DEBUG unless resp.cookies.empty?
  begin
    msg resp, Logger::DEBUG
    jresp = JSON resp
    if jresp
      return jresp
    end
  rescue JSON::ParserError => e
     # do nothing
  end
  return resp
end

#post_form(e, filepath, p = {}) ⇒ Object

post call for uploading form data

* e = the url suffix
* filepath = the file to post
* p = hash of parameters, such as headers


176
177
178
179
180
181
182
183
184
# File 'lib/mu/http_helper.rb', line 176

def post_form(e, filepath, p = {})
  url = "https://#{@username}:#{@password}@#{@host}#{@docroot}#{e}"
  params = {}.merge! p
  params = { :content_type => "application/x-www-form-urlencoded", :file => File.new(filepath, 'rb') }
  resp = RestClient.post(url, params)
  $cookie = resp.cookies unless resp.cookies.empty?
  msg "got cookie #{$cookie}", Logger::DEBUG unless resp.cookies.empty?
  return resp
end

#post_json(e, json, p = {}) ⇒ Object

post call for uploading json

* e = the url suffix
* body = the json object to post
* p = hash of parameters, such as headers


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/mu/http_helper.rb', line 116

def post_json(e, json, p = {})
   url = "https://#{@username}:#{@password}@#{@host}#{@docroot}#{e}"
   msg "#{url}  #{json}", Logger::DEBUG
   params = { :content_type => "application/json" }.merge! p
   params[:cookies] = $cookie if !$cookie.empty?
   resp = RestClient.post(url, json, params  )
   begin
    msg resp, Logger::DEBUG
    $cookie = resp.cookies unless resp.cookies.empty?
    msg "got cookie #{$cookie}", Logger::DEBUG unless resp.cookies.empty?
    jresp = JSON resp
    if jresp
      return jresp
    end
  rescue JSON::ParserError => e
     # msg e, Logger::DEBUG
  end
  return resp
end

#post_xml(e, doc, p = {}) ⇒ Object

post call for uploading xml

* e = the url suffix
* body = the xml object to post
* p = hash of parameters, such as headers


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/mu/http_helper.rb', line 140

def post_xml(e, doc, p = {})
  begin
    url = "https://#{@username}:#{@password}@#{@host}#{@docroot}#{e}"
    msg "post to #{url}", Logger::DEBUG
    params = { :content_type => "application/xml" }.merge! p
    params[:cookies] = $cookie if !$cookie.empty?
    resp = RestClient.post(url, doc.to_s, params )
    msg resp, Logger::DEBUG
    $cookie = resp.cookies unless resp.cookies.empty?
    msg "got cookie #{$cookie}", Logger::DEBUG unless resp.cookies.empty?
    # XML Document
    if (/<.+>/).match(resp)
      xmldoc = Nokogiri::XML(resp)
    else
      err_node = Nokogiri::XML::Node.new('err_node', xmldoc)
      err_node.content = resp
      xmldoc.root << err_node
    end
  rescue SocketError
    raise "Host " + @host + " nicht erreichbar"
  rescue Exception => e
    msg "error parsing XML " + e.to_s, Logger::DEBUG
  end
  if (xmldoc.nil? || !xmldoc.xpath("//err_node").empty?)
    @posted_uuid = nil
  end
  if !xmldoc.nil?
    @posted_uuid = xmldoc.xpath("//uuid")[0].text if !xmldoc.xpath("//uuid").empty?
  end
  return xmldoc
end

#put_json(e, json, p = {}) ⇒ Object

put call for json

* e = the url suffix
* body = the json object to put
* p = hash of parameters, such as headers


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/mu/http_helper.rb', line 199

def put_json(e, json, p={})
   url = "https://#{@username}:#{@password}@#{@host}#{@docroot}#{e}"
   params = {}.merge! p
   params[:cookies] = $cookie if !$cookie.empty?
   resp = RestClient.put(url, json, { :content_type => "application/json" } )
   begin
    msg resp, Logger::DEBUG
    $cookie = resp.cookies unless resp.cookies.empty?
    msg "got cookie #{$cookie}", Logger::DEBUG unless resp.cookies.empty?
    jresp = JSON resp
    if jresp
      return jresp
    end
  rescue JSON::ParserError => e
     # msg e, Logger::DEBUG
  end
  return resp
end