Class: WHD::SendRestRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/whd_ticket/whd_rest_method.rb

Instance Method Summary collapse

Constructor Details

#initialize(whd_url, api_key, data) ⇒ SendRestRequest

Returns a new instance of SendRestRequest.



7
8
9
10
11
12
# File 'lib/whd_ticket/whd_rest_method.rb', line 7

def initialize(whd_url, api_key, data)
  @whd_url      = whd_url
  @whd_url_path = "/helpdesk/WebObjects/Helpdesk.woa"
  @api_key      = api_key
  @data         = data
end

Instance Method Details

#createObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/whd_ticket/whd_rest_method.rb', line 14

def create

  url = "#{@whd_url}#{@whd_url_path}/ra/Tickets"
  uri = URI.parse(url)
  params = { :apiKey => @api_key }
  uri.query = URI.encode_www_form(params)

  http = Net::HTTP.new(uri.host, uri.port)
  # http.set_debug_output $stdout
  http.use_ssl = true

  request = Net::HTTP::Post.new(uri.request_uri)
  request['Content-Type'] = "application/json"

  request.body = @data.to_json

  response = http.request(request)

  begin
    json = JSON.parse(response.body)
  rescue JSON::ParserError => e
    print "There seems to be an issue with the json reponse. Please have a look!!\n"
    return
  end

  if response.code == '201'
    print "Ticket with id: #{json["id"]} and subject '#{json["subject"]}' has been created.\n"
  else
    print "There was an error creating the ticket.\n"
    print "http code    : #{response.code}"
    print "http message : #{respnse.message}"
  end
end