class Object

Constants

CONFIG
CONFIG_FILE

Public Instance Methods

b() click to toggle source
# File lib/fogbugz/common.rb, line 27
def b; HighLine.use_color? ? HighLine::BLUE    : ''; end
c() click to toggle source
# File lib/fogbugz/common.rb, line 31
def c; HighLine.use_color? ? HighLine::CLEAR   : ''; end
check_api_token() click to toggle source
# File lib/fogbugz/common.rb, line 40
def check_api_token
  unless CONFIG[:api_token]
    puts "Not logged in."
    exit 1
  end
end
check_api_url() click to toggle source
# File lib/fogbugz/common.rb, line 33
def check_api_url
  unless CONFIG[:api_url]
    puts "Not logged in."
    exit 1
  end
end
dispatch_subcommand(file, commands = {}) click to toggle source
# File lib/fogbugz/common.rb, line 345
def dispatch_subcommand(file, commands = {})
  basename = File.basename(file)
  absolute = File.expand_path(file)

  usage = <<HERE
usage: #{basename} <subcommands> [options]

The subcommands are:
HERE
  commands.keys.sort { |a,b| a.to_s <=> b.to_s }.each do |k|
    usage << format("   %-16.16s%s\n", k, commands[k])
  end
  usage << <<HERE

See '#{basename} help <commands>' for more information on a specific command.
HERE

  if ARGV[0] == 'help'
    if ARGV[1] and commands[ARGV[1].to_sym]
      subcommand = ARGV[1]
      ARGV.replace ['--help']
      load "#{absolute}-#{subcommand}"
    elsif ARGV[1] and which("#{basename}-#{ARGV[1]}")
      command = which("#{basename}-#{ARGV[1]}")
      system "#{command} --help"
    else
      puts usage
      exit 1
    end
  elsif ARGV[0] and commands[ARGV[0].to_sym]
    load "#{absolute}-#{ARGV.shift}"
  elsif ARGV[0] and which("#{basename}-#{ARGV[0]}")
    command = which("#{basename}-#{ARGV.shift}")
    Process.wait(Process.fork { Process.exec(command, *ARGV) })
  elsif ARGV[0].to_i.to_s == ARGV[0]
    load "#{absolute}-show"
    exit 0
  else
    puts usage
    exit 1
  end
end
do_api(cmd, params={}) click to toggle source
# File lib/fogbugz/common.rb, line 302
def do_api(cmd, params={})
  api_url = CONFIG[:api_url]
  api_token = CONFIG[:api_token]

  query = ''
  params.each_pair do |k,v|
    query << format("&%s=%s", URI.escape(k.to_s), URI.escape(v.to_s)) if v
  end

  uri = URI "#{api_url}?cmd=#{cmd}&token=#{api_token}#{query}"
  http = Net::HTTP.new(uri.host, uri.port)
  if uri.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  response = http.start do |h|
    h.request Net::HTTP::Get.new(uri.request_uri)
  end
  if response.code != '200'
    puts "HTTP request to #{api_url} failed with code #{response.code}."
    exit 1
  end

  result = REXML::Document.new(response.body)
  error = result.elements['/response/error']
  if error
    puts "Failed with error: #{error.text}."
    exit 1
  end
  result
end
g() click to toggle source
# File lib/fogbugz/common.rb, line 28
def g; HighLine.use_color? ? HighLine::GREEN   : ''; end
get_editor_content(options, template) click to toggle source
# File lib/fogbugz/common.rb, line 105
def get_editor_content(options, template)
  editor = ENV['EDITOR'] || 'vim'
  invoke_editor = true
  old_argv = ARGV.clone
  if options[:file]
    if options[:file] == '-'
      ARGV.replace []
    else
      ARGV.replace [options[:file]]
    end
    template = ARGF.read
    invoke_editor = false
  elsif options[:template]
    if options[:template] == '-'
      ARGV.replace []
    else
      ARGV.replace [options[:template]]
    end
    template = ARGF.read
  end

  content = template
  if invoke_editor
    tempfile = Tempfile.new ['case', '.md']
    tempfile.write template
    tempfile.close
    rc = system "#{editor} #{tempfile.path}"
    unless rc
      puts "Editor exited with non-zero status. Aborting."
      exit 1
    end
    tempfile.open
    content = tempfile.read
    tempfile.close
    tempfile.delete
  end

  data = {}
  if content =~ /(.*?\n?)^(---\s*$\n?)/m
    # Combined YAML front matter with text content.
    begin
      data = YAML.load($1) || {}
      data['body'] = $POSTMATCH if $POSTMATCH and not $POSTMATCH.empty?
    rescue => e
      puts "Exception reading YAML front matter. #{e.inspect}"
      exit 1
    end
  else
    begin
      # YAML only content.
      data = YAML.load(content)
      if data.instance_of? String
        # Text only content.
        data = { 'body' => content }
      end
    rescue => e
      data = {}
    end
  end

  if not data or data.empty?
    puts "No new content for case. Aborting."
    exit 1
  end
  ARGV.replace old_argv
  data
end
m() click to toggle source
# File lib/fogbugz/common.rb, line 30
def m; HighLine.use_color? ? HighLine::MAGENTA : ''; end
maybe_append(key, xml) click to toggle source
# File lib/fogbugz/common.rb, line 217
def maybe_append(key, xml)
  if xml and xml.text and not xml.text.empty?
    "# #{key}: #{xml.text}\n"
  else
    "# #{key}: <#{key}>\n"
  end
end
maybe_array(key, xml) click to toggle source
# File lib/fogbugz/common.rb, line 225
def maybe_array(key, xml)
  if xml and not xml.empty?
    "# #{key}: [#{xml.join(', ')}]\n"
  else
    "# #{key}: [<#{key}>]\n"
  end
end
maybe_event(xml, time_xml) click to toggle source
# File lib/fogbugz/common.rb, line 280
def maybe_event(xml, time_xml)
  if xml and xml.text and not xml.text.empty?
    if time_xml and time_xml.text and not time_xml.text.empty?
      format("# %s at %s.\n", xml.text, time_string(time_xml.text))
    else
      commented = xml.text.gsub(/\n/, "\n# ")
      "# #{commented}"
    end
  else
    ''
  end
end
maybe_literal(xml) click to toggle source
# File lib/fogbugz/common.rb, line 293
def maybe_literal(xml)
  if xml and xml.text and not xml.text.empty?
    commented = xml.text.gsub(/\n/, "\n# ")
    "# #{commented}\n"
  else
    ''
  end
end
maybe_show(key, xml) click to toggle source
# File lib/fogbugz/common.rb, line 173
def maybe_show(key, xml)
  if xml and xml.text and not xml.text.empty?
    format "#{b}%-11.11s#{c}#{xml.text}\n", "#{key}:"
  else
    ''
  end
end
maybe_show_array(key, xml) click to toggle source
# File lib/fogbugz/common.rb, line 181
def maybe_show_array(key, xml)
  if xml and not xml.empty?
    format "#{b}%-11.11s#{c}#{xml.join(', ')}\n", "#{key}:"
  else
    ''
  end
end
maybe_show_event(color, xml, time_xml) click to toggle source
# File lib/fogbugz/common.rb, line 205
def maybe_show_event(color, xml, time_xml)
  if xml and xml.text and not xml.text.empty?
    if time_xml and time_xml.text and not time_xml.text.empty?
      format("#{color}%s at %s.#{c}\n", xml.text, time_string(time_xml.text))
    else
      "#{color}#{xml.text}#{c}\n"
    end
  else
    ''
  end
end
maybe_show_literal(color, xml) click to toggle source
# File lib/fogbugz/common.rb, line 189
def maybe_show_literal(color, xml)
  if xml and xml.text and not xml.text.empty?
    "#{color}#{xml.text}#{c}\n"
  else
    ''
  end
end
maybe_show_time(key, xml) click to toggle source
# File lib/fogbugz/common.rb, line 197
def maybe_show_time(key, xml)
  if xml and xml.text and not xml.text.empty?
    format "#{b}%-11.11s#{c}#{time_string(xml.text)}\n", "#{key}:"
  else
    ''
  end
end
maybe_time(key, xml) click to toggle source
# File lib/fogbugz/common.rb, line 272
def maybe_time(key, xml)
  if xml and xml.text and not xml.text.empty?
    "# #{key}: #{time_string(xml.text)}\n"
  else
    "# #{key}: <#{key}>\n"
  end
end
parse_opts(usage, num_args) click to toggle source
# File lib/fogbugz/common.rb, line 47
def parse_opts(usage, num_args)
  options = {}
  optparse = OptionParser.new do |opts|
    opts.banner = usage

    opts.on_tail('-h', '--help') do
      puts optparse.help
      exit 1
    end
  end
  optparse.parse!

  unless ARGV.length == num_args
    puts optparse.help
    exit 1
  end
  options
end
parse_stdin_opts(usage, num_args) click to toggle source
# File lib/fogbugz/common.rb, line 66
def parse_stdin_opts(usage, num_args)
  options = {}
  optparse = OptionParser.new do |opts|
    opts.banner = usage

    opts.on_tail('-h', '--help') do
      puts optparse.help
      exit 1
    end

    options[:file] = nil
    opts.on('--file=<file>',
            'Take the case content from the given file. Use - to read from ' +
            'STDIN.') do |file|
      options[:file] = file
    end

    options[:template] = nil
    opts.on('--template=<template>',
            'Use the file content or - for STDIN as the initial case ' +
            'content.') do |template|
      options[:template] = template
    end
  end
  optparse.parse!

  unless ARGV.length == num_args or
      (ARGV.length == num_args + 1 and ARGV.last == '-')
    puts optparse.help
    exit 1
  end

  if ARGV.length == num_args + 1
    ARGV.pop
    options[:file] = '-'
  end
  options
end
parse_time(text) click to toggle source
# File lib/fogbugz/common.rb, line 251
def parse_time(text)
  return nil unless text
  begin
    time = Time.parse(text)
    # Ruby 1.8.7 returns the current time on bad parses.
    raise ArgumentError if Time.now.to_s == time.to_s
  rescue ArgumentError
    begin
      time = Chronic.parse(text).utc.iso8601
    rescue
      puts "Unable to parse date #{text}."
      exit 1
    end
  end
  unless time
    puts "Unable to parse date #{text}."
    exit 1
  end
  time
end
r() click to toggle source
# File lib/fogbugz/common.rb, line 26
def r; HighLine.use_color? ? HighLine::RED     : ''; end
time_short_string(text) click to toggle source
# File lib/fogbugz/common.rb, line 233
def time_short_string(text)
  if text
    Time.parse(text).localtime.strftime('%D')
  else
    ''
  end
end
time_string(text) click to toggle source
# File lib/fogbugz/common.rb, line 241
def time_string(text)
  if text
    time = Time.parse(text).localtime
    format("%s on %s", time.strftime('%-l:%M %p'),
           time.strftime('%A, %B %e %Y'))
  else
    ''
  end
end
which(cmd) click to toggle source
# File lib/fogbugz/common.rb, line 334
def which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each { |ext|
      exe = "#{path}/#{cmd}#{ext}"
      return exe if File.executable? exe
    }
  end
  return nil
end
y() click to toggle source
# File lib/fogbugz/common.rb, line 29
def y; HighLine.use_color? ? HighLine::YELLOW  : ''; end