Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sumirolabs committed Nov 26, 2012
0 parents commit 2fb9d64
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in loudmouthjs-rails.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2012 Pete Campbell

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 changes: 29 additions & 0 deletions README.md
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,29 @@
# Loudmouthjs::Rails

TODO: Write a gem description

## Installation

Add this line to your application's Gemfile:

gem 'loudmouthjs-rails'

And then execute:

$ bundle

Or install it yourself as:

$ gem install loudmouthjs-rails

## Usage

TODO: Write usage instructions here

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
8 changes: 8 additions & 0 deletions lib/loudmouthjs-rails.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,8 @@
require "loudmouthjs-rails/version"

module Loudmouthjs
module Rails
class Engine < ::Rails::Engine
end
end
end
5 changes: 5 additions & 0 deletions lib/loudmouthjs-rails/version.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
module Loudmouthjs
module Rails
VERSION = "0.1.0"
end
end
16 changes: 16 additions & 0 deletions loudmouthjs-rails.gemspec
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/loudmouthjs-rails/version', __FILE__)

Gem::Specification.new do |gem|
gem.authors = ["Pete Campbell"]
gem.email = ["pete@sumirolabs.com"]
gem.description = "Shout about JavaScript errors and alerts in both dev and production."
gem.summary = "Loudmouth tells you about JavaScript errors that occur in your app. It can complain loudly (eg. alerts) or silently (posting the error info back to your server). It can also tell you what alerts the users have seen."
gem.homepage = "http://code.livingsocial.net/petecampbell/loudmouth"

gem.files = `git ls-files`.split($\)
gem.name = "loudmouthjs-rails"
gem.require_paths = ["lib"]
gem.version = Loudmouthjs::Rails::VERSION
gem.add_dependency "railties", "~> 3.1"
end
109 changes: 109 additions & 0 deletions vendor/assets/javascripts/loudmouth.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,109 @@
// Make sure the developer knows that there is a JS error

var Loudmouth = (function(my){
var complaints = [], original_onerror = window.onerror, config = {}, original_alert = window.alert;

var complainLoudly = function(){
if (complaints.length > 0) {

var complaints_str = 'There was a JavaScript exception';
if (complaints.length > 1) complaints_str = 'There were ' + complaints.length + ' JavaScript exceptions';
complaints_str += " when the page was loading:\n";

var error;
while ( (complaint = complaints.pop()) != null){
complaints_str += "\n-------------------";
for ( var p in complaint){
if (complaint.hasOwnProperty(p)) complaints_str += "\n" + p + " : " + complaint[p];
}
};
complaints_str += "\n-------------------\n\n";

alert(complaints_str);
} else {
console.log("This loudmouth has nothing to complain about, kthxbye!");
};

return this;
};

var complainSilently = function(){
var complaint;
while( (complaint = complaints.pop()) != null){
hollaback(complaint);
};
};

// Report errors back to the server
var hollaback = function(error_info) {
error_info.href = window.location.href;
if ( Loudmouth.hollaback_url() ) {
var image_str = "<img src='" + Loudmouth.hollaback_url() + '?error_info=' + JSON.stringify(error_info) + "'>";
console.log(image_str);
$(document).append(image_str);
} else {
console.log("Warning: Loudmouth.js captured a complaint, but the hollaback server URL is not defined, so I'm telling you about the problem: ");
console.log(error_info);
};
};

// URL for reporting errors
my.hollaback_url = function(target_url){
if (target_url) config.hollaback_url = target_url;
return config.hollaback_url;
};

var addError = function(errorMessage, url, lineNumber){
addComplaint('error', {errorMessage: errorMessage, url: url, lineNumber: lineNumber});
};

var addComplaint = function(type, error_info){
error_info.type = type;
complaints.push(error_info);
};

my.captureAlerts = function(){
console.log("Loudmouth will capture all window.alert calls");
window.alert = function() {
addComplaint('alert', {alert_message: arguments[0]});
console.log("Loudmouth saw this alert: " + arguments[0]);
return original_alert.apply(this, arguments);
};
return this; // Enable chaining
};

// Complain right away
my.watch = function(){
var watch_onerror = function(errorMessage, url, lineNumber){
addError(errorMessage, url, lineNumber);
complainLoudly();
return original_onerror ? original_onerror.call(errorMessage, url, lineNumber) : original_onerror;
};
window.onerror = watch_onerror;
return this; // Enable chaining
};

// Keep track of all complaints quietly
my.lurk = function(){
var lurk_onerror = function(errorMessage, url, lineNumber){
addError(errorMessage, url, lineNumber);
complainSilently();
return original_onerror ? original_onerror.call(errorMessage, url, lineNumber) : original_onerror;
};
window.onerror = lurk_onerror;
if ( ! Loudmouth.hollaback_url() ) {
console.log("Warning: The hollaback server URL is not defined, so errors will not be reported back to the server. Use Loudmouth.hollaback_url(<your url>) to tell Loudmouth where to shout.");
}
return this; // Enable chaining
};

// Stop watching
my.goAway = function(){
window.onerror = original_onerror;
return this; // Enable chaining
};

return my;

})(Loudmouth || {});

0 comments on commit 2fb9d64

Please sign in to comment.