class Trackchange::Exec

Constants

CRON_LINE

Public Class Methods

run(args) click to toggle source
# File lib/trackchange/exec.rb, line 13
def run(args)
  new(args).run
end

Public Instance Methods

add() click to toggle source
# File lib/trackchange/exec.rb, line 47
def add
  config.sites ||= []
  config.sites |= [ { url: args.first } ]
  store_config!

  # instant probe on add
  config.sites = [ { url: args.first } ]
  Probe.new(config).probe(config.sites.last)
end
email() click to toggle source
# File lib/trackchange/exec.rb, line 32
def email
  config.email = args.first
  store_config!
end
install() click to toggle source
# File lib/trackchange/exec.rb, line 88
def install
  CronEdit::Crontab.Add('trackchange', CRON_LINE)
end
list() click to toggle source
# File lib/trackchange/exec.rb, line 57
def list
  config.sites.each_with_index do |site, pos|
    puts "% 4s %s" % [pos+1, site[:url]]
  end
end
probe() click to toggle source

commands

# File lib/trackchange/exec.rb, line 28
def probe
  Probe.run(config)
end
remove() click to toggle source
# File lib/trackchange/exec.rb, line 81
def remove
  pos = args.first.to_i - 1
  raise "Invalid position" if pos == -1
  config.sites.delete_at(pos)
  store_config!
end
rss() click to toggle source
# File lib/trackchange/exec.rb, line 37
def rss
  config.rss_path = args.first
  store_config!
end
run() click to toggle source
# File lib/trackchange/exec.rb, line 18
def run
  FileUtils.mkdir_p(path) unless File.exist?(path)
  cmd = args.shift
  raise "No command" unless cmd
  return send(cmd) if respond_to?(cmd)
  raise "Unknown command #{cmd}"
end
slack() click to toggle source
# File lib/trackchange/exec.rb, line 42
def slack
  config.slack_hook = args.first
  store_config!
end
test() click to toggle source

TODO refactor to make remove code duplication

# File lib/trackchange/exec.rb, line 64
def test
  pos = args.first.to_i - 1
  raise "Invalid position" if pos == -1
  site = config.sites[pos]
  url = site[:url]

  cmd = config.fetch
  substitutions = {
    url: url,
    queryscript: File.expand_path('../query.coffee', __FILE__),
    selector: site[:selector]
  }
  substitutions.each { |key, value| cmd = cmd.gsub("%#{key}%", value.to_s) }
  puts "% #{cmd}"
  puts %x[#{cmd}]
end
uninstall() click to toggle source
# File lib/trackchange/exec.rb, line 92
def uninstall
  CronEdit::Crontab.Remove('trackchange')
end

Private Instance Methods

config() click to toggle source
# File lib/trackchange/exec.rb, line 113
def config
  return @config if @config
  data = default_config
  data = YAML.load(File.read(config_path)) if File.exist?(config_path)

  # upgrade from <= 0.2.0
  if v(data[:version]) <= v('0.2.0')
    data[:version] = '0.2.0'
    data[:sites] = data[:sites].map do |site|
      { url: site }
    end
    @config = OpenStruct.new(data)
    store_config!
  end

  # upgrade from 0.3.0 to 0.4.0
  if v(data[:version]) < v('0.4.0')
    data[:version ] = '0.4.0'
    data[:fetch] = default_config[:fetch]
    data[:feed_size] = 20
    @config = OpenStruct.new(data)
    store_config!
  end

  @config = OpenStruct.new(data)
end
config_path() click to toggle source
# File lib/trackchange/exec.rb, line 102
def config_path
  File.join(path, 'config.yml')
end
default_config() click to toggle source
# File lib/trackchange/exec.rb, line 106
def default_config
  {
    version: VERSION,
    fetch: "lynx -nolist -dump '%url%' | uniq"
  }
end
path() click to toggle source
# File lib/trackchange/exec.rb, line 98
def path
  File.expand_path('~/.trackchange')
end
store_config!() click to toggle source
# File lib/trackchange/exec.rb, line 140
def store_config!
  File.open(config_path, 'w') { |f| f.print(YAML.dump(config.marshal_dump)) }
end
v(version) click to toggle source
# File lib/trackchange/exec.rb, line 144
def v(version)
  Gem::Version.new(version)
end