Class: Trackchange::Exec

Inherits:
Struct
  • Object
show all
Defined in:
lib/trackchange/exec.rb

Constant Summary collapse

CRON_LINE =
'0 7 * * * trackchange probe >/dev/null'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argsObject

Returns the value of attribute args

Returns:

  • (Object)

    the current value of args



8
9
10
# File 'lib/trackchange/exec.rb', line 8

def args
  @args
end

Class Method Details

.run(args) ⇒ Object



13
14
15
# File 'lib/trackchange/exec.rb', line 13

def run(args)
  new(args).run
end

Instance Method Details

#addObject



47
48
49
50
51
52
53
54
55
# 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

#emailObject



32
33
34
35
# File 'lib/trackchange/exec.rb', line 32

def email
  config.email = args.first
  store_config!
end

#installObject



88
89
90
# File 'lib/trackchange/exec.rb', line 88

def install
  CronEdit::Crontab.Add('trackchange', CRON_LINE)
end

#listObject



57
58
59
60
61
# 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

#probeObject

commands



28
29
30
# File 'lib/trackchange/exec.rb', line 28

def probe
  Probe.run(config)
end

#removeObject



81
82
83
84
85
86
# 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

#rssObject



37
38
39
40
# File 'lib/trackchange/exec.rb', line 37

def rss
  config.rss_path = args.first
  store_config!
end

#runObject



18
19
20
21
22
23
24
# 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

#slackObject



42
43
44
45
# File 'lib/trackchange/exec.rb', line 42

def slack
  config.slack_hook = args.first
  store_config!
end

#testObject

TODO refactor to make remove code duplication



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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

#uninstallObject



92
93
94
# File 'lib/trackchange/exec.rb', line 92

def uninstall
  CronEdit::Crontab.Remove('trackchange')
end