jamie (owner)

Revisions

gist: 229952 Download_button fork
public
Description:
I'm far to lazy to manually close several hundred hoptoad errors, so I automated it!
Public Clone URL: git://gist.github.com/229952.git
Embed All Files: show embed
hoptoad_api.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
require 'rubygems'
require 'activesupport'
require 'restclient'
require 'hpricot'
 
module Hoptoad
  class API
    attr_accessor :dirty
    
    def initialize(base, token)
      @base, @token = base, token
    end
    
    def each
      page = 1
      loop do
        begin
          page_xml = RestClient.get(errors_uri(page))
          errors = (Hpricot.parse(page_xml)/'group')
          break if errors.empty?
        
          errors.each do |error_xml|
            yield Toad.new(error_xml, self)
          end
        rescue => e
          puts "Error getting page contents: #{e.message}"
        end
        
        page += 1 unless @dirty
        @dirty = false
      end
    end
    
    def errors_uri(page=1)
      "#{@base}/errors.xml?auth_token=#{@token}&page=#{page}"
    end
    
    def error_uri(id)
      "#{@base}/errors/#{id}?auth_token=#{@token}"
    end
  end
  
  class Toad
    def initialize(xml, api)
      @xml = xml
      @api = api
    end
    
    # Object#id isn't deprecated yet :/
    def id
      method_missing(:id)
    end
    
    def method_missing(method, *args)
      if (node = @xml/method.to_s.gsub('_','-')) && !node.empty?
        node.first.innerHTML
      else
        super
      end
    end
    
    def resolve!
      @api.dirty = true
      RestClient.put(uri, :group => {:resolved => true})
    end
    
    def uri
      @api.error_uri(self.id)
    end
  end
end
 
prune_hoptoad.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
require 'hoptoad_api'
 
token = "..."
base_uri = "..."
 
Hoptoad::API.new(base_uri, token).each do |toad|
  # Don't want to worry about ancient errors that haven't resurfaced lately
  # and don't want to close a few hundred hoptoads manually.
  time = Time.parse(toad.most_recent_notice_at)
  
  if time < 14.days.ago
    p [toad.id, toad.error_message]
    toad.resolve!
  end
end