class Torque::PivotalHTMLParser

Public Class Methods

new() click to toggle source
# File lib/torque/pivotal_html_parser.rb, line 13
def initialize
  @date_filter = false
  @field_filters = false
end

Public Instance Methods

add_date_filter(accept_from, accept_to) click to toggle source

@param accept_from A Date marking the lower bound for the date_accepted field of a story (inclusive) @param accept_to A Date marking the upper bound for the date_accepted field of a story (inclusive)

Adds a date filter to stories, excluding them if they lie outside the provided dates

# File lib/torque/pivotal_html_parser.rb, line 23
def add_date_filter(accept_from, accept_to)
  @date_filter = true
  @accept_from = accept_from
  @accept_to = accept_to
end
add_field_filters(field_filter_list) click to toggle source

@param field_filter_list A list of FieldFilter objects to use

Adds field filters to stories, excluding them if they do not pass all filters in field_filter_list

# File lib/torque/pivotal_html_parser.rb, line 33
def add_field_filters(field_filter_list)
  @field_filters = true
  @field_filter_list = field_filter_list
end
process_project(project_html_string) click to toggle source

@param project_html_string An html string containing the story data for a Pivotal Tracker project

@return A list of all Story objects parsed from project_html_string (least recent to most recent date accepted)

Applies during processing any filters that have been added

# File lib/torque/pivotal_html_parser.rb, line 44
def process_project(project_html_string)

  project_element = Nokogiri::HTML(project_html_string)
  process_stories_element(project_element)
end
process_project_iterations(project_html_string) click to toggle source

@param project_html_string An html string containing iteration data for a Pivotal Tracker project

@return A list of Iteration objects parsed from project_html_string (least recent to most recent iteration)

# File lib/torque/pivotal_html_parser.rb, line 55
def process_project_iterations(project_html_string)

  project_html = Nokogiri::HTML(project_html_string)
  iteration_html_array = project_html.search('iteration')
  iteration_list = []

  iteration_html_array.each do
    |iteration_element|
    iteration_list << process_iteration_element(iteration_element)
  end

  iteration_list
end

Private Instance Methods

passes_filters(story) click to toggle source

story: A Story object

True if story passes the list of filters, else false

# File lib/torque/pivotal_html_parser.rb, line 128
def passes_filters(story)

  if @date_filter        
    return false if story.date_accepted.nil?
    return false unless (story.date_accepted <= @accept_to)
    return false unless (story.date_accepted >= @accept_from)
  end

  if @field_filters
    @field_filter_list.each do |filter|
      return false unless filter.eval(story)
    end
  end

  return true
end
process_iteration_element(iteration_element) click to toggle source

iteraton_element: A Nokogiri::XML::Element representing an iteration (root element “iteration”)

Returns an Interation object based off the object in the element

# File lib/torque/pivotal_html_parser.rb, line 108
def process_iteration_element(iteration_element)
  
  iter_number = nil
  stories = []

  iteration_element.children.each do
    |child|
    if child.name == "id"
      iter_number = child.children[0].text
    elsif child.name == "stories"
      stories = process_stories_element(child)
    end
  end

  Iteration.new(iter_number, stories)
end
process_stories_element(stories_element) click to toggle source

stories_element: A Nokogiri::XML::Element representing a collection of stories (root element “stories”)

Returns a list of all Story objects parsed from project_element (least recent to most recent date accepted)

Excludes stories that fall outside of any added filters

# File lib/torque/pivotal_html_parser.rb, line 91
def process_stories_element(stories_element)

  story_html_array = stories_element.search('story')
  story_list = []

  story_html_array.each do
    |story_element|
    story = process_story_element(story_element)
    story_list << story if passes_filters(story)
  end

  story_list
end
process_story_element(story_element) click to toggle source

story_element: A Nokogiri::XML::Element representing a story (root element “story”)

Returns a Story object based off the information in the element

# File lib/torque/pivotal_html_parser.rb, line 74
def process_story_element(story_element)

  story_hash = {}

  story_element.children.each do
    |child|
    story_hash[child.name] = child.text
  end

  Story.create(story_hash)
end