Class: Post2Zendesk

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

Instance Method Summary collapse

Constructor Details

#initializePost2Zendesk

Returns a new instance of Post2Zendesk.



11
12
13
14
15
16
# File 'lib/post2zendesk.rb', line 11

def initialize
  configdata = YAML.load_file(ENV['HOME'] + '/.post2zendesk.yaml')
  @uri = URI.parse("https://#{configdata['baseurl']}/")
  @username = configdata['username']
  @password = configdata['password']
end

Instance Method Details

#makerequest(type = 'Get', body = '') ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/post2zendesk.rb', line 18

def makerequest(type='Get', body='')
  @http = Net::HTTP.new(@uri.host, @uri.port)
  @http.use_ssl = true
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  if type == 'Get'
    @request = Net::HTTP::Get.new(@uri.request_uri)
  elsif type == 'Put'
    @request = Net::HTTP::Put.new(@uri.request_uri)
    @request['Content-Type'] = 'application/json'
    @request.body = body
  else
    raise ArgumentError.new('Unrecognized HTTP request type.')
  end

  @request.basic_auth(@username, @password)
  return @response = @http.request(@request).body
end

#printreplyObject



51
52
53
54
55
# File 'lib/post2zendesk.rb', line 51

def printreply
  response_hash = JSON.parse(@response)
  puts "Ticket #{response_hash['ticket']['id']} updated. Probably."
  puts "Status: #{response_hash['ticket']['status']}"
end

#updateticket(ticketid, comment, status = 'pending', is_public = 'true') ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/post2zendesk.rb', line 37

def updateticket(ticketid, comment, status='pending', is_public='true')
  # https://support.puppetlabs.com/api/v2/tickets/3717.json
  @uri.path = "/api/v2/tickets/#{ticketid.to_s}.json"

  updatearray = { 
    'ticket' => {
      'status'  => status,
      'comment' => { 'body' => comment, 'public' => is_public }
    }
  }
  updatearray_json = updatearray.to_json
  @response = makerequest('Put', updatearray_json)
end