class Torque::ProjectManager

Retrieves the list of Pivotal Tracker projects from the Pivotal API, automates the switching of projects

Public Class Methods

new(torque_info_parser=TorqueInfoParser.new) click to toggle source

@param torque_info_parser An instance of the TorqueInfoParser class

# File lib/torque/project/project_manager.rb, line 24
def initialize(torque_info_parser=TorqueInfoParser.new)
  @torque_info_parser = torque_info_parser
end

Public Instance Methods

current_project() click to toggle source

@return The current project if 'load_project_list' has been called and one exists, else returns nil

# File lib/torque/project/project_manager.rb, line 48
def current_project
  @project_list.nil? \
  ? nil
  : @project_list.select{|project| project.current}[0]
end
format_project_list() click to toggle source

@return The project list formatted as a printable string

Prepends an asterisk to the current project

# File lib/torque/project/project_manager.rb, line 58
def format_project_list

  list_str = "PROJECTS\n"
  @project_list.each do |project|
    
    project.current \
      ? list_str += "* " \
      : list_str += "  "
    list_str += "#{project.id} #{project.name}" 
    list_str += "\n"
  end

  list_str
end
load_project_list(token=nil) click to toggle source

@param token A Pivotal Tracker API token (default: Loads token from .torqueinfo.yaml) @return the project list

Requests and processes the project list from the Pivotal Tracker API

# File lib/torque/project/project_manager.rb, line 33
def load_project_list(token=nil)
  get_project_list(token)
  @project_list
end
project_list() click to toggle source

@return The project list if 'load_project_list' has been called, else returns nil

Does not do any processing

# File lib/torque/project/project_manager.rb, line 42
def project_list
  @project_list
end

Private Instance Methods

get_project_list(token=nil) click to toggle source

token: The API token to use for the request

Retrieves the project list from Pivotal Tracker Stores the project list in @project_list Returns the project list

# File lib/torque/project/project_manager.rb, line 81
def get_project_list(token=nil)

  # Parses data from the torque info file
  torque_info_token, project_id = parse_torque_info
  token = torque_info_token unless token

  # If the API token doesn't exist, prompt for it
  raise MissingTokenError.new "API token for Pivotal Tracker has not been set" \
    unless token

  # Parses the project list from the Pivotal Tracker API
  html = Pivotal.new(token).get_project_data
  @project_list = parse_project_html(html)

  identify_current_project(project_id)
end
identify_current_project(project_id) click to toggle source

Identifies the current project and marks it as such

# File lib/torque/project/project_manager.rb, line 99
def identify_current_project(project_id)
  @project_list.each do
    |project|
    if String(project_id) == project.id
      project.current = true
    end
  end
end
parse_project_html(html) click to toggle source

Parses the html returned from the Pivotal API

# File lib/torque/project/project_manager.rb, line 118
def parse_project_html(html)
  project_html = Nokogiri::HTML(html)
  project_html_array = project_html.search('project')
  project_list = []

  project_html_array.each do
    |project_element|
  
    project_hash = {}
    project_element.children.each do
      |child|
      project_hash[child.name] = child.text
    end

    project_list << Project.new(project_hash["id"], project_hash["name"])
  end

  project_list
end
parse_torque_info() click to toggle source

Parses the API token and current project from .torqueinfo.yaml Returns [token, project]

# File lib/torque/project/project_manager.rb, line 110
def parse_torque_info

  torque_info = @torque_info_parser.parse

  return torque_info.token, torque_info.project
end