class Object

Public Instance Methods

accumulate(toggl_data) click to toggle source
# File bin/togglmine, line 31
def accumulate(toggl_data)
  toggl_data.inject([]) do |m, toggl_entry|
    matched_entry = m.find do |e|
      e['description'] == toggl_entry['description'] && e['tags'] == toggl_entry['tags']
    end

    if matched_entry
      matched_entry['dur'] = matched_entry['dur'].to_i + toggl_entry['dur'].to_i
    else
      m << toggl_entry
    end
    m
  end
end
build_xml(opt) click to toggle source
# File bin/togglmine, line 12
def build_xml(opt)
  xml = Builder::XmlMarkup.new(indent: 2)
  xml.instruct! :xml, encoding: "UTF-8"
  xml.time_entry do |time_entry|
    opt.each { |k, v| time_entry.tag!(k, v) }
  end
end
convert_msec_to_hour(msec) click to toggle source
# File bin/togglmine, line 27
def convert_msec_to_hour(msec)
  (msec.to_f / 1000 / 60 / 60).round(2)
end
init_config() click to toggle source
# File bin/togglmine, line 84
def init_config
  if File.exists? "#{ENV['HOME']}/.togglmine.yaml"
    puts '~/.togglmine.yaml already exists.'
    return
  end

  init_config = File.read(File.expand_path('../../asset/template_togglmine.yaml', __FILE__))

  open("#{ENV['HOME']}/.togglmine.yaml", 'w') do |f|
    f.write init_config
  end
  puts "Put template file on #{ENV['HOME']}/.togglmine.yaml"
end
parse_options() click to toggle source
# File bin/togglmine, line 46
def parse_options
  opts = {}
  opt = OptionParser.new('Usage:', 48)
  opt.separator '  togglmine [date]'
  opt.separator '  togglmine [options]'
  opt.separator ''
  opt.separator 'date example: 2017-02-10'
  opt.separator '     default: today'
  opt.separator ''

  opt.separator 'Options:'
  opt.on('-d', '--update-default-issue-id default_issue_id',
         'Update default_issue_id in togglmine.yaml') { |v| opts['default_issue_id'] = v }
  opt.on('', '--init-config', 'Create togglmine.yaml') { |v| opts['init'] = v }
  opt.version = ::Togglmine::VERSION

  args = opt.parse(ARGV)
  [opts, args]
end
resolve_tag_value_to_activity_id(tags, mapping) click to toggle source
# File bin/togglmine, line 20
def resolve_tag_value_to_activity_id(tags, mapping)
  activity = tags.map { |tag| mapping[tag] }
                 .reject(&:nil?)
                 .first
  activity && activity['id']
end
target_date(arg) click to toggle source
# File bin/togglmine, line 98
def target_date(arg)
  return Date.today.strftime('%Y-%m-%d') unless arg
  return arg if arg =~ /\A\d{4}-\d{2}-\d{2}\z/
  return (Date.today - arg.to_i).strftime('%Y-%m-%d') if (1..31).cover? arg.to_i
  raise "You can pass a parameter either YYYY-MM-DD format or integer less than or equal to 31 but: #{arg}"
end
update_default_issue_id(id) click to toggle source
# File bin/togglmine, line 66
def update_default_issue_id(id)
  puts 'trying to update default_issue_id.'
  config = YAML.load_file("#{ENV['HOME']}/.togglmine.yaml")
  puts "current: #{config['default_issue_id']}"
  puts "tobe: #{id}"
  print 'ok? [y/n]: '
  ans = STDIN.gets
  if ans =~ /^[Yy]/
    row_text = File.read("#{ENV['HOME']}/.togglmine.yaml")
    open("#{ENV['HOME']}/.togglmine.yaml", 'w') do |f|
      f.write row_text.gsub(/default_issue_id:.*$/, "default_issue_id: #{id}")
    end
    puts 'updated.'
  else
    puts 'quit'
  end
end