public
Last active — forked from croaky/hoptoad_api.rb

Active Resource model for Hoptoad

  • Download Gist
hoptoad_api.rb
Ruby
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 'active_resource'
 
ActiveResource::Base.class_eval do
private
 
alias original_find_or_create_resource_for find_or_create_resource_for
 
# names like "foo.bar" become "foo_bar" so constants lookup doesn't barf
def find_or_create_resource_for(name)
original_find_or_create_resource_for name.to_s.gsub('.', '_')
end
end
 
class Hoptoad < ActiveResource::Base
cattr_accessor :auth_token
 
class << self
# yields every record, fetches in batches of 30
def find_each(options = {}, &block)
page = 1
options = options.dup.tap { |o| o[:params] ||= Hash.new }
begin
options[:params][:page] = page
batch = find(:all, options)
batch.each(&block)
page += 1
end until batch.size < 30
end
 
def find_matching(pattern)
[].tap do |items|
find_each do |item|
items << item if item.error_message.to_s.index(pattern)
end
end
end
 
def element_path(id, prefix_options = {}, query_options = nil)
super(id, *apply_auth_token(prefix_options, query_options))
end
 
def collection_path(prefix_options = {}, query_options = nil)
super(*apply_auth_token(prefix_options, query_options))
end
 
private
 
def apply_auth_token(prefix_options, query_options)
if query_options
[prefix_options, query_options.merge(:auth_token => auth_token)]
else
[prefix_options.merge(:auth_token => auth_token)]
end
end
end
 
def resolve!
self.resolved = true
save
end
 
alias original_encode encode
 
# IMPORTANT: overrides `encode` to serialize only the "resolved" attribute.
# If you want to update other attributes, remove this monkeypatch.
def encode(options = {})
self.class.format.encode(
attributes.slice('resolved'),
{ :root => self.class.element_name }.merge(options)
)
end
end
hoptoad_error.rb
Ruby
1 2 3 4 5 6 7 8
class Error < Hoptoad
self.site = 'http://ACCOUNT.hoptoadapp.com'
self.auth_token = 'API_TOKEN'
 
# have these in every subclass
self.collection_name = 'errors'
self.element_name = 'group'
end
usage_examples.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Errors are paginated. You get 30 at a time.
Error.find :all
Error.find :all, :params => { :page => 2 }
 
# Iterate through *all* unresolved errors:
Error.find_each do |error|
...
end
 
# Filter by pattern:
Error.find_matching('NoMethodError')
 
# To resolve an error:
error.resolve!

Quality stuff, thanks!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.