Class: Yammdesk::WHD

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

Constant Summary collapse

CONFIG =
YAML.load_file("config.yml")

Instance Method Summary collapse

Constructor Details

#initializeWHD

Returns a new instance of WHD.



15
16
17
18
19
20
21
22
# 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

Instance Method Details

#create_ticket(thread_id, body) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# 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

#getObject



24
25
26
27
28
29
30
# 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) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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_ticketObject



115
116
117
# File 'lib/yammdesk/whd.rb', line 115

def last_ticket
  last_ticket = "10559118"
end

#note_exists?(ticket_id, yamm_body) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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 = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
# 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) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 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