-
-
Save mislav/375945 to your computer and use it in GitHub Desktop.
Active Resource model for Hoptoad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quality stuff, thanks!