Last active
August 29, 2015 14:25
-
-
Save kreynolds/a129f5bcb84a8cf21d79 to your computer and use it in GitHub Desktop.
ActiveSupport/Rails Stripe Instrumentation
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
# Define some unobtrusive instrumentation | |
module Stripe | |
module Instrumentation | |
module Collection | |
def all(*args) | |
ActiveSupport::Notifications.instrument("stripe.#{collection_type}.list") { super } | |
end | |
def create(*args) | |
ActiveSupport::Notifications.instrument("stripe.#{collection_type}.create") { super } | |
end | |
def retrieve(id, *args) | |
ActiveSupport::Notifications.instrument("stripe.#{collection_type}.retrieve", id: id) { super } | |
end | |
def collection_type | |
@_collection_type ||= self.url.split('/').last.singularize.capitalize | |
end | |
end | |
module List | |
def self.prepended(base); base.extend ClassMethods; end | |
module ClassMethods | |
def all(*args) | |
ActiveSupport::Notifications.instrument("stripe.#{self.class_name.downcase}.list") { super } | |
end | |
end | |
end | |
module Request | |
def self.prepended(base); base.extend ClassMethods; end | |
module ClassMethods | |
def retrieve(id, *args) | |
ActiveSupport::Notifications.instrument("stripe.#{self.class_name.downcase}.retrieve", id: id) { super } | |
end | |
end | |
end | |
module Create | |
def self.prepended(base); base.extend ClassMethods; end | |
module ClassMethods | |
def create(*args) | |
ActiveSupport::Notifications.instrument("stripe.#{self.class_name.downcase}.create") { super } | |
end | |
end | |
end | |
module Update | |
def save(*args) | |
ActiveSupport::Notifications.instrument("stripe.#{self.class.class_name.downcase}.update", id: self['id']) { super } | |
end | |
end | |
module Delete | |
def delete(*args) | |
ActiveSupport::Notifications.instrument("stripe.#{self.class.class_name.downcase}.delete", id: self['id']) { super } | |
end | |
end | |
end | |
end | |
# Prepend the instrumentation if required into each APIResource | |
Stripe::APIResource.descendants.each do |klass| | |
klass.prepend Stripe::Instrumentation::Request if klass.ancestors.include?(Stripe::APIOperations::Request) | |
klass.prepend Stripe::Instrumentation::List if klass.ancestors.include?(Stripe::APIOperations::List) | |
klass.prepend Stripe::Instrumentation::Update if klass.ancestors.include?(Stripe::APIOperations::Update) | |
klass.prepend Stripe::Instrumentation::Delete if klass.ancestors.include?(Stripe::APIOperations::Delete) | |
klass.prepend Stripe::Instrumentation::Create if klass.ancestors.include?(Stripe::APIOperations::Create) | |
end | |
Stripe::ListObject.prepend Stripe::Instrumentation::Collection |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment