Sha256: 3844d528b7f631fea919c4dfd70749ab3e19ba0c56f206cbbf761c141b67b620

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

# code to communicate with your backend goes here...

class CommentUtil

  def initialize(ticket_id, trac, doc = nil)
    @doc = doc || Nokogiri::HTML(open("#{trac[:url]}/ticket/#{ticket_id}", :http_basic_authentication=>[trac[:username], trac[:password]]))
    @ticket_id = ticket_id
  end

  def comments
    comment_id = 0
    @doc.css('h3.change a.timeline').collect do |value|
      body = @doc.css('div.comment')
      authors = @doc.css('h3.change')
      comment_id += 1
      unless body[comment_id-1].nil? || authors[comment_id-1].nil?
        comment_body = body[comment_id-1].text
        if comment_body =~ /\w+/
          build_comment_hash(value, comment_id, comment_body, authors[comment_id-1].content)
        end
      end
    end
  end

  private
  def build_comment_hash(value, comment_id, body, author)
    comment = {}
    comment[:created_at] = date_from_attributes(value)
    comment[:updated_at] = date_from_attributes(value)
    comment[:ticket_id] = @ticket_id
    comment[:body] = body
    comment[:author] = normalize_author_field(author)
    comment[:id] = comment_id
    comment
  end

  def normalize_author_field(author)
    author.strip.split(/by/)[1].split(/@/)[0]
  end

  def date_from_attributes(value)
    value.attributes["title"].value.split(/\s/)[0]
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
taskmapper-trac-0.7.0 lib/trac/trac.rb
taskmapper-trac-0.6.1 lib/trac/trac.rb
taskmapper-trac-0.6.0 lib/trac/trac.rb