Class: Trackchange::Probe

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config

Returns:

  • (Object)

    the current value of config



9
10
11
# File 'lib/trackchange/probe.rb', line 9

def config
  @config
end

Class Method Details

.run(config) ⇒ Object



12
13
14
# File 'lib/trackchange/probe.rb', line 12

def run(config)
  new(config).run
end

Instance Method Details

#faradayObject



21
22
23
24
25
26
# File 'lib/trackchange/probe.rb', line 21

def faraday
  @faraday ||= Faraday.new do |f|
    f.request :url_encoded
    f.adapter Faraday.default_adapter
  end
end

#loggerObject



152
153
154
155
156
157
158
159
160
# File 'lib/trackchange/probe.rb', line 152

def logger
  return @logger if @logger
  @logger = Logger.new(STDOUT).tap do |logger|
    logger.level = (config.log_level || 2).to_i
    logger.formatter = proc do |severity, datetime, progname, msg|
      "#{msg}\n"
    end
  end
end

#probe(site) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/trackchange/probe.rb', line 35

def probe(site)
  url = site[:url]
  fname = url.sub(%r{https?://}, '').tr_s('/?&', '...')
  site_path = File.expand_path(fname, '~/.trackchange')

  # build cmd
  cmd = config.fetch  + "> #{site_path}.new"
  substitutions = {
    url: url,
    queryscript: File.expand_path('../query.coffee', __FILE__),
    selector: site[:selector]
  }
  substitutions.each { |key, value| cmd = cmd.gsub("%#{key}%", value.to_s) }
  logger.debug "% #{cmd}"
  system cmd

  unless File.exist?(site_path) # new site
    logger.warn "new site #{url}"
    FileUtils.mv("#{site_path}.new", site_path)
    return
  end

  diff = "diff -u #{site_path} #{site_path}.new"
  logger.debug "% #{diff}"
  result = %x[ #{diff} ]

  if result.empty? # same old
    logger.info "same old #{url}"
    FileUtils.rm_f("#{site_path}.new")
    return
  end

  if site[:threshold]
    diffgrepwc = "#{diff} | grep '^[-+]:' | wc -l"
    logger.debug "% #{diffgrepwc}"
    degree = %x[ #{diffgrepwc} ]
    if degree.to_i < site[:threshold].to_i
      logger.warn 'change below threshold, skipping notification'
      skip_notification = true
    end
  end

  # changed
  logger.warn "changed #{url}"
  if pat = config.archive_pattern
    time = File.ctime(site_path).strftime(pat)
    logger.info "archived #{site_path}.#{time}"
    FileUtils.mv(site_path, "#{site_path}.#{time}")
  end
  FileUtils.mv("#{site_path}.new", site_path)

  return if skip_notification

  if slack_hook = config.slack_hook
    message = slack_defaults.merge(config.slack.to_h)
    message['text'] = "#{url}\n\n#{result}"
    faraday.post slack_hook, payload: JSON.unparse(message)
  end

  if email = config.email # send email
    logger.info "sending notification to #{email}"
    begin
      file = Tempfile.new('changetrack')
      file.write "#{url}\n\n#{result}"
      file.close
      mail = "cat #{file.path} | mail -s 'Change detected on #{url}' #{email}"
      logger.debug mail
      system mail
    ensure
      file.unlink
    end
  end

  if file = File.expand_path(config.rss_path, ENV['HOME']) # write rss
    logger.info "update rss feed '#{file}'"

    out_rss = RSS::Maker.make('1.0') do |maker|
      maker.channel.id = %x[ whoami ].chomp
      maker.channel.author = %x[ whoami ].chomp
      maker.channel.updated = Time.now.to_s
      maker.channel.title = "Trackchange"
      maker.channel.link = "http://github.com/brnach14/trackchange"
      maker.channel.about = "Feed of detected changes"
      maker.channel.description = "Feed of detected changes"

      # new item
      maker.items.new_item do |item|
        item.link = "#{url}##{Time.now.to_i}"
        item.title = "Change detected on #{url}"
        item.date = Time.now.to_s
        item.description = "<pre>#{result}</pre>"
      end

      # read rss, keepn old items
      if File.exist?(file)
        in_rss = RSS::Parser.parse(File.read(file))
        in_rss.items.each_with_index do |in_item, index|
          break if index > config.feed_size - 1
          maker.items.new_item do |out_item|
            out_item.link = in_item.link
            out_item.title = in_item.title
            out_item.date = in_item.date
            out_item.description = in_item.description
          end
        end
      end
    end

    # write rss
    if File.exist?(file) and !File.writable?(file)
      raise "'#{file}' is not writable, skipping rss persistency"
    else
      File.open(file, 'w') { |f| f.puts(out_rss) }
    end
  end
end

#runObject



17
18
19
# File 'lib/trackchange/probe.rb', line 17

def run
  config.sites.each { |site| probe(site) }
end

#slack_defaultsObject



28
29
30
31
32
33
# File 'lib/trackchange/probe.rb', line 28

def slack_defaults
  {
    'username' => 'trackchange',
    'icon_emoji' => ':fax:'
  }
end