#!/usr/bin/env ruby require 'yammdesk' require 'colorize' require 'cgi' beginning = Time.now yamm = Yammdesk::Yammer.new whd = Yammdesk::WHD.new yamm_results = yamm.get yamm_results.sort_by { |id| } yamm_results.each { |m| id = m['id'] replied_to_id = m["replied_to_id"] thread_id = m["thread_id"] body = m["body"]["plain"] thread_exists = whd.thread_exists?(thread_id) thread_starter = yamm.is_thread_starter?(id, thread_id) # TODO: Write these methods # ticket_closed = WHD.ticket_closed?(thread_id) # if thread_exists && !thread_starter puts "Thread is not a thread starter, AND ticket exists".yellow puts "thread_exists = #{thread_exists}\nthread_starter = #{thread_starter}".yellow puts "thread_id = #{thread_id}".yellow puts "messag id = #{id}".yellow puts "body = #{body}\n".yellow ticket_id = whd.search(thread_id) if !whd.note_exists?(ticket_id, body) puts "Thread is not a thread starter, AND ticket exists, AND note does not exist".blue puts "thread_exists = #{thread_exists}\nthread_starter = #{thread_starter}".blue puts "thread_id = #{thread_id}".blue puts "messag id = #{id}".blue puts "body = #{body}\n".blue whd.update_ticket(ticket_id, body) end elsif !thread_exists && thread_starter puts "Thread does not exist, AND is thread_starter".green puts "thread_exists = #{thread_exists}\nthread_starter = #{thread_starter}".green puts "thread_id = #{thread_id}".green puts "messag id = #{id}".green puts "body = #{body}".green whd.create_ticket(thread_id, body) ticket_id = whd.search(thread_id) thread_replies = yamm.get_thread_replies(thread_id) thread_replies.sort_by { |id| } thread_replies.each { |m| id = m['id'] replied_to_id = m["replied_to_id"] thread_id = m["thread_id"] body = m["body"]["plain"] if !yamm.is_thread_starter?(id, thread_id) whd.update_ticket(ticket_id, body) end } elsif !thread_exists && !thread_starter puts "Thread does not exist AND this is not a thread starter".red puts "thread_exists = #{thread_exists}\nthread_starter = #{thread_starter}".red puts "thread_id = #{thread_id}".red puts "messag id = #{id}".red puts "body = #{body}\n".red missing_thread_starter = yamm.get_thread_starter(thread_id) id = missing_thread_starter['id'] replied_to_id = missing_thread_starter["replied_to_id"] thread_id = missing_thread_starter["thread_id"] body = missing_thread_starter["body"]["plain"] # We found a missing thread starter from a reply returned by the YAPI # Let's create a ticket from the ID we found then: whd.create_ticket(thread_id, body) # ... and get all the replies ticket_id = whd.search(thread_id) thread_replies = yamm.get_thread_replies(thread_id) thread_replies.sort_by { |id| } thread_replies.each { |m| id = m['id'] replied_to_id = m["replied_to_id"] thread_id = m["thread_id"] body = m["body"]["plain"] if !yamm.is_thread_starter?(id, thread_id) whd.update_ticket(ticket_id, body) end } else puts "Thread exists AND this is a thread starter\n".red end } # Now check for updated tickets, and push to Yammer whd_tickets = whd.get whd_tickets.sort_by { |id| } whd_tickets.each { |t| yamm_id = t['shortSubject'] ticket_id = t['id'] whd_id = whd.search(yamm_id) yamm_replies = yamm.get_thread_replies(yamm_id) whd_notes = whd.get_notes(ticket_id) whd_notes.each { | note | puts "Now searching for note:\n#{note}\n".red if yamm.reply_exists?(note, yamm_replies) puts "Ticket: #{ticket_id}\n" puts "Thread from WHD: #{yamm_id}\n" puts "Nothin' doin'. The note is already a comment on Yammer:\n" + "#{note}\n\n".blue else puts "Ticket: #{ticket_id}\n" puts "Thread from WHD: #{yamm_id}\n" puts "Didn't find the note in the thread. Updating it with the new reply:\n" + "#{note}\n\n".green yamm.update_thread(yamm_id, note) end } } puts "Application run completed in #{Time.now - beginning} seconds\n"