class Yammdesk::WHD
Constants
- CONFIG
Public Class Methods
new()
click to toggle source
# File lib/yammdesk/whd.rb, line 15 def initialize @url = CONFIG['whd']['url'] @token = CONFIG['whd']['tech_token'] @uri = URI(@url) current_dir = File.dirname(__FILE__) @post_file = JSON.parse(IO.read("#{current_dir}/../../templates/create_ticket.json")) @update_file = JSON.parse(IO.read("#{current_dir}/../../templates/update_ticket.json")) end
Public Instance Methods
create_ticket(thread_id, body)
click to toggle source
# File lib/yammdesk/whd.rb, line 136 def create_ticket(thread_id, body) @post_file["subject"] = "#{thread_id}" @post_file["detail"] = body post_data = @post_file.to_s url = @url + "Tickets?apiKey=#{@token}" uri = URI(url) request = Net::HTTP::Post.new(uri.request_uri) request.body = post_data response = Net::HTTP.new(uri.host, uri.port).start { |http| http.request(request) } # DEBUG # puts "Creating ticket for thread: ".yellow + "#{thread_id}\n" + "with message: ".yellow + "#{post_data}\n" + # "using put URL: ".yellow + "#{url}\n\n" # DEBUG end
get()
click to toggle source
# File lib/yammdesk/whd.rb, line 24 def get Net::HTTP.start(@uri.host, @uri.port) do |http| response = http.get(@url + "Tickets/?list=group&apiKey=#{@token}") # response = http.get(@url + "Tickets/?list=group", :params => {:apiKey => @token}) @data = JSON.parse(response.body) end end
get_notes(ticket_id)
click to toggle source
# File lib/yammdesk/whd.rb, line 65 def get_notes(ticket_id) url = @url + "TicketNotes?jobTicketId=#{ticket_id}&apiKey=#{@token}" uri = URI(url) Net::HTTP.start(@uri.host, @uri.port) do |http| response = http.get(url) get_data = JSON.parse(response.body) @whd_entries = [] get_data.each { |h| note = CGI.unescapeHTML(h['mobileNoteText']) note = note.gsub('<br/> ', "\n") note = Nokogiri::HTML.parse(note) note = note.text @whd_entries << note } return @whd_entries end end
last_ticket()
click to toggle source
# File lib/yammdesk/whd.rb, line 115 def last_ticket last_ticket = "10559118" end
note_exists?(ticket_id, yamm_body)
click to toggle source
# File lib/yammdesk/whd.rb, line 48 def note_exists?(ticket_id, yamm_body) yamm_body = CGI.unescapeHTML(yamm_body) url = @url + "TicketNotes?jobTicketId=#{ticket_id}&apiKey=#{@token}" uri = URI(url) Net::HTTP.start(@uri.host, @uri.port) do |http| response = http.get(url) get_data = JSON.parse(response.body) if note = get_data.find { |note| CGI.unescapeHTML(note['mobileNoteText']) } return true else return false end end end
put(options = {})
click to toggle source
# File lib/yammdesk/whd.rb, line 105 def put(options = {}) ticket_id = options[:ticket_id] put_data = options[:put_data] Net::HTTP.start(@uri.host, @uri.port) do |http| response = http.request_put(@url + "Tickets/#{ticket_id}/?apiKey=#{@token}", put_data) @data = JSON.parse(response.body) end end
search(thread_id)
click to toggle source
# File lib/yammdesk/whd.rb, line 88 def search(thread_id) url = @url + "Tickets?qualifier=(subject%3D'#{thread_id}')&apiKey=#{@token}" uri = URI(url) Net::HTTP.start(uri.host, uri.port) do |http| response = http.get(url) #puts "DEBUG: #{response.code}, #{response.body}" get_data = JSON.parse(response.body) #data = (get_data[0] || {}).to_hash data = get_data[0].to_hash ticket_id = data["id"] return ticket_id end end
thread_exists?(thread_id)
click to toggle source
# File lib/yammdesk/whd.rb, line 32 def thread_exists?(thread_id) url = @url + "Tickets?qualifier=(subject%3D'#{thread_id}')&apiKey=#{@token}" uri = URI(url) Net::HTTP.start(uri.host, uri.port) do |http| response = http.get(url) get_data = JSON.parse(response.body) if get_data.empty? return false else return true end end end
update_ticket(ticket_id, message)
click to toggle source
# File lib/yammdesk/whd.rb, line 119 def update_ticket(ticket_id, message) @update_file["ticketId"] = ticket_id @update_file["jobticket"]["id"] = ticket_id @update_file["noteText"] = message post_data = @update_file.to_s url = @url + "TechNotes?apiKey=#{@token}" uri = URI(url) request = Net::HTTP::Post.new(uri.request_uri) request.body = post_data response = Net::HTTP.new(uri.host, uri.port).start { |http| res = http.request(request) # puts "CODE: #{res.code} with BODY: #{res.body}\n".white } end