<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -4,85 +4,126 @@ require 'tempfile'
 
 class Jira &lt; CampfireBot::Plugin
   
-  at_interval 3.minutes, :fetch_jira
-  on_command 'checkjira', :check_jira
+  at_interval 3.minutes, :check_jira
+  on_command 'checkjira', :checkjira_command
   
   
   def initialize
-    puts &quot;#{Time.now} | #{bot.config['room']} | JIRA Plugin | initializing... &quot;
+    log &quot;initializing... &quot;
     @data_file  = File.join(BOT_ROOT, 'tmp', &quot;jira-#{BOT_ENVIRONMENT}-#{bot.config['room']}.yml&quot;)
     @cached_ids = YAML::load(File.read(@data_file)) rescue {}
     @last_checked ||= 10.minutes.ago
   end
-  
 
-  def fetch_jira(msg)
+  # respond to checkjira command-- same as interval except we answer with 'no issues found' if 
+  def checkjira_command(msg)
+    msg.speak &quot;no new issues since I last checked #{lastlast} ago&quot; if check_jira(msg)
+  end
+  
+  def check_jira(msg)
     
-    issuecount = 0
-    @cached_ids ||= {}
+    saw_an_issue = false
     old_cache = Marshal::load(Marshal.dump(@cached_ids)) # since ruby doesn't have deep copy
-    puts &quot;#{Time.now} | #{msg[:room].name} | JIRA Plugin | checking jira for new issues...&quot;
-
-    begin
-
-      xmldata = open(bot.config['jira_url']).read
-      doc = REXML::Document.new(xmldata)
-
-      
-      tix = []
-      doc.elements.each('rss/channel/item') do |ele|
-        tix.push(ele)
-      end
-      
-      # need to reverse these elements so we get the oldest one first
-      tix.reverse.each do |ele|
-        id = ele.elements['key'].text
-        id, key = split_spacekey_and_id(id)
-       
-        if !old_cache.key?(key) or old_cache[key] &lt; id
-
-          # puts &quot;#{Time.now} | #{msg[:room].name} | JIRA Plugin | ticket #{ele.elements['key'].text} is new, updating cache and speaking&quot;
-          @cached_ids[key] = id if !@cached_ids.key?(id) or @cached_ids[key] &lt; id
-
-          issuecount += 1
-          link = ele.elements['link'].text
-          title = ele.elements['title'].text
-          reporter = ele.elements['reporter'].text
-          type = ele.elements['type'].text
-          priority = ele.elements['priority'].text
-          msg.speak(&quot;#{type} - #{title} - #{link} - reported by #{reporter} - #{priority}&quot;)
-          puts &quot;#{Time.now} | #{msg[:room].name} | JIRA Plugin | #{type} - #{title} - #{link} - reported by #{reporter} - #{priority}&quot;
-        end
-      end
     
-    rescue Exception =&gt; e
-      puts &quot;#{Time.now} | #{msg[:room].name} | JIRA Plugin | error connecting to jira: #{e.message}&quot;
-    end
+    lastlast = time_ago_in_words(@last_checked)
+    tix = fetch_jira_url
     
-    File.open(@data_file, 'w') do |out|
-      YAML.dump(@cached_ids, out)
+    tix.each do |ticket|
+      if seen?(ticket, old_cache)
+        saw_an_issue = true
+
+        @cached_ids = update_cache(ticket, @cached_ids) 
+        flush_cache(@cached_ids)
+  
+        messagetext = &quot;#{ticket[:type]} - #{ticket[:title]} - #{ticket[:link]} - reported by #{ticket[:reporter]} - #{ticket[:priority]}&quot;
+        msg.speak(messagetext)
+        log messagetext
+          
+      end
     end
 
     @last_checked = Time.now
-    puts &quot;#{Time.now} | #{msg[:room].name} | JIRA Plugin | no new issues.&quot; if issuecount == 0
-    issuecount
+    log &quot;no new issues.&quot; if !saw_an_issue
   
+    saw_an_issue
   end
   
-  def check_jira(msg)
-    lastlast = time_ago_in_words(@last_checked)
-    count = fetch_jira(msg)
-    msg.speak &quot;no new issues since I last checked #{lastlast} ago&quot; if count == 0
+  protected
+  
+  # fetch jira url and return a list of ticket Hashes
+  def fetch_jira_url()
+    begin
+      log &quot;checking jira for new issues...&quot;
+      xmldata = open(bot.config['jira_url']).read
+      doc = REXML::Document.new(xmldata)
+    rescue Exception =&gt; e
+      log &quot;error connecting to jira: #{e.message}&quot;
+    end
+      
+    doc.elements.inject('rss/channel/item', []) do |tix, element|
+      tix.push(parse_ticket_info(element))
+    end
+    
   end
   
-  protected
+  # extract ticket hash from individual xml element
+  def parse_ticket_info(xml_element)
+    id = xml_element.elements['key'].text
+    id, spacekey = split_spacekey_and_id(id)
+    
+    link = xml_element.elements['link'].text
+    title = xml_element.elements['title'].text
+    reporter = xml_element.elements['reporter'].text
+    type = xml_element.elements['type'].text
+    priority = xml_element.elements['priority'].text
+    
+    return {
+      :spacekey =&gt; spacekey,
+      :id =&gt; id,
+      :link =&gt; link,
+      :title =&gt; title,
+      :reporter =&gt; reporter,
+      :type =&gt; type,
+      :priority =&gt; priority
+    }
+  end
   
+  # extract the spacekey and id from the ticket id
   def split_spacekey_and_id(key)
     spacekey = key.scan(/^([A-Z]+)/).to_s
     id = key.scan(/([0-9]+)$/)[0].to_s.to_i
     return id, spacekey
   end
   
+  # has this ticket been seen before this run?
+  def seen?(ticket, old_cache)
+    !old_cache.key?(ticket[:spacekey]) or old_cache[ticket[:spacekey]] &lt; ticket[:id]
+  end
+  
+  # only update the cached highest ID if it is in fact the highest ID
+  def update_cache(ticket, cache)
+    cache[ticket[:spacekey]] = ticket[:id] if seen?(ticket, cache)
+    cache
+  end
+
+  # write the cache to disk
+  def flush_cache(cache)
+    File.open(@data_file, 'w') do |out|
+      YAML.dump(cache, out)
+    end
+  end
+  
+  # log
+  def log(message)
+    puts &quot;#{Time.now} | #{bot.config['room']} | JIRA Plugin | #{message}&quot;
+  end
+  
+  
+  # 
+  # time/utility functions
+  # 
+  
+  
   def time_ago_in_words(from_time, include_seconds = false)
     distance_of_time_in_words(from_time, Time.now, include_seconds)
   end</diff>
      <filename>plugins/jira.rb</filename>
    </modified>
    <modified>
      <diff>@@ -78,7 +78,7 @@ describe &quot;checking jira and&quot; do
     
     @jira.stub!(:open).and_return(mock('foo', :read =&gt; xmldata))
     
-    @jira.fetch_jira(@message)
+    @jira.check_jira(@message)
   end
 
   describe &quot;seeing a ticket higher than the last stored id&quot; do</diff>
      <filename>spec/jira_spec.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>30e5047b7586d6b2da9c416e97150c27531d7c1b</id>
    </parent>
  </parents>
  <author>
    <name>joshwand</name>
    <email>p123jsf@JoshWand.local</email>
  </author>
  <url>http://github.com/timriley/campfire-bot/commit/dd69c9e376197b63fec2ac2808e5f6914c1dde84</url>
  <id>dd69c9e376197b63fec2ac2808e5f6914c1dde84</id>
  <committed-date>2009-08-02T20:35:39-07:00</committed-date>
  <authored-date>2009-06-17T10:56:05-07:00</authored-date>
  <message>jira plugin refactor to not rely on issue ordering in rss feed</message>
  <tree>40924ce79140464f81aaecb332d47db3357a10e0</tree>
  <committer>
    <name>Tim Riley</name>
    <email>tim@openmonkey.com</email>
  </committer>
</commit>
