class PivotalTrackerCli::Client

Attributes

username_to_user_id_map[R]

Public Class Methods

new(args, local_options, config) click to toggle source
Calls superclass method
# File lib/pivotal_tracker_cli.rb, line 18
def initialize(args, local_options, config)
  @config = YAML.load_file(ENV['HOME'] + '/.pt')

  @api_token = @config['api_token']
  @project_id = @config['project_id']
  @usernames = @config['usernames']

  @story_statuses = {
      'unstart' => 'unstarted',
      'start' => 'started',
      'deliver' => 'delivered',
      'finish' => %w(finished accepted)
  }

  @username_to_user_id_map = build_or_assign_user_cache(@config)
  super
end

Public Instance Methods

backlog() click to toggle source
# File lib/pivotal_tracker_cli.rb, line 60
def backlog
  get_backlog(3).map do |story|
    output.puts("* #{story.id.to_s.red} - #{colorize_status(story.current_state)} - #{embiggen_string(story.name)} <#{get_owner_name_from_ids(story.owner_ids)}>")
  end
end
list() click to toggle source
# File lib/pivotal_tracker_cli.rb, line 38
def list
  get_current_stories_for_user.map do |story|
    output.puts('*' * 40)
    format_story(story)
  end
  output.puts('*' * 40)
end
refresh() click to toggle source
# File lib/pivotal_tracker_cli.rb, line 68
def refresh
  rebuild_user_cache(@config)
end
show(id) click to toggle source
# File lib/pivotal_tracker_cli.rb, line 48
def show(id)
  format_story(get_story(id))
end
update(id, status) click to toggle source
# File lib/pivotal_tracker_cli.rb, line 54
def update(id, status)
  validate_and_update_story(id, status)
end

Private Instance Methods

build_or_assign_user_cache(config) click to toggle source
# File lib/pivotal_tracker_cli.rb, line 114
def build_or_assign_user_cache(config)
  PivotalTrackerCli::UserCache.build_or_assign_user_cache(config, @project_id, @api_token)
end
colorize_status(status) click to toggle source
# File lib/pivotal_tracker_cli.rb, line 126
def colorize_status(status)
  PivotalTrackerCli::StringUtilities.colorize_status(status)
end
embiggen_string(string) click to toggle source
# File lib/pivotal_tracker_cli.rb, line 122
def embiggen_string(string)
  PivotalTrackerCli::StringUtilities.embiggen_string(string)
end
find_name_given_id(owners) click to toggle source
# File lib/pivotal_tracker_cli.rb, line 110
def find_name_given_id(owners)
  PivotalTrackerCli::HashManager.find_name_given_id(owners, @username_to_user_id_map)
end
format_story(story) click to toggle source
# File lib/pivotal_tracker_cli.rb, line 93
def format_story(story)
  output.puts("#{'Story ID'.bold}          : #{story.id}")
  output.puts("#{'Status'.bold}            : #{colorize_status(story.current_state)}")
  output.puts("#{'Story Type'.bold}        : #{story.story_type}")
  output.puts("#{'Story Name'.bold}        : #{embiggen_string(story.name)}")
  output.puts("#{'Owners'.bold}            : #{get_owner_name_from_ids(story.owner_ids).yellow}")
  output.puts("#{'Story Description'.bold} :")
  output.puts("                    #{wrap(embiggen_string(story.description), 150, 10)}")
end
get_backlog(iterations) click to toggle source
# File lib/pivotal_tracker_cli.rb, line 130
def get_backlog(iterations)
  PivotalTrackerCli::Api.get_backlog_for_project(@project_id, @api_token, iterations)
end
get_current_stories_for_user() click to toggle source
# File lib/pivotal_tracker_cli.rb, line 134
def get_current_stories_for_user
  PivotalTrackerCli::Api.get_current_stories_for_user(@project_id, @api_token, @usernames)
end
get_owner_name_from_ids(owners) click to toggle source
# File lib/pivotal_tracker_cli.rb, line 106
def get_owner_name_from_ids(owners)
  PivotalTrackerCli::HashManager.get_owner_name_from_ids(owners, @username_to_user_id_map)
end
get_story(id) click to toggle source
# File lib/pivotal_tracker_cli.rb, line 138
def get_story(id)
  PivotalTrackerCli::Api.get_story_by_id(@project_id, @api_token, id)
end
get_user_ids_from_usernames(user_map, username) click to toggle source
# File lib/pivotal_tracker_cli.rb, line 150
def get_user_ids_from_usernames(user_map, username)
  PivotalTrackerCli::UserCache.get_user_ids_from_usernames(user_map, username)
end
output() click to toggle source
# File lib/pivotal_tracker_cli.rb, line 146
def output
  @output ||= $stdout
end
rebuild_user_cache(config) click to toggle source
# File lib/pivotal_tracker_cli.rb, line 118
def rebuild_user_cache(config)
  PivotalTrackerCli::UserCache.rebuild_user_cache(config, @project_id, @api_token)
end
update_story(id, status) click to toggle source
# File lib/pivotal_tracker_cli.rb, line 142
def update_story(id, status)
  PivotalTrackerCli::Api.update_story_state(@project_id, @api_token, id, status, get_user_ids_from_usernames(@username_to_user_id_map, @usernames))
end
validate_and_update_story(id, status) click to toggle source
# File lib/pivotal_tracker_cli.rb, line 74
def validate_and_update_story(id, status)
  return output.puts('Invalid story status. Story statuses are: unstart, start, deliver, finish') unless @story_statuses[status]

  story = get_story(id)

  return output.puts('Story not found, please validate story number.') unless story

  if status != 'finish'
    output.puts(update_story(id, @story_statuses[status]))
  else
    if story.story_type == 'chore'
      output.puts(update_story(id, 'accepted'))
    else
      output.puts(update_story(id, 'finished'))
    end
  end
end
wrap(s, width=150, offset=0) click to toggle source
# File lib/pivotal_tracker_cli.rb, line 102
def wrap(s, width=150, offset=0)
  s.gsub(/(.{1,#{width}})(\s+|\Z)/, "#{' '* offset}\\1\n")
end