lena 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +48 -0
  4. data/Rakefile +29 -0
  5. data/app/assets/javascripts/lena.coffee +78 -0
  6. data/app/controllers/lena/lena_controller.rb +8 -0
  7. data/config/routes.rb +3 -0
  8. data/lib/lena.rb +4 -0
  9. data/lib/lena/engine.rb +20 -0
  10. data/lib/lena/rails/routing.rb +7 -0
  11. data/lib/lena/routing.rb +13 -0
  12. data/lib/lena/version.rb +3 -0
  13. data/spec/dummy/Rakefile +6 -0
  14. data/spec/dummy/app/assets/javascripts/application.js +14 -0
  15. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  16. data/spec/dummy/app/views/application/index.html +3 -0
  17. data/spec/dummy/app/views/application/log.html +4 -0
  18. data/spec/dummy/app/views/application/throw.html +4 -0
  19. data/spec/dummy/app/views/application/throw_callstack.html +10 -0
  20. data/spec/dummy/app/views/layouts/application.html.erb +13 -0
  21. data/spec/dummy/bin/bundle +3 -0
  22. data/spec/dummy/bin/rails +4 -0
  23. data/spec/dummy/bin/rake +4 -0
  24. data/spec/dummy/config.ru +4 -0
  25. data/spec/dummy/config/application.rb +14 -0
  26. data/spec/dummy/config/boot.rb +5 -0
  27. data/spec/dummy/config/environment.rb +5 -0
  28. data/spec/dummy/config/environments/development.rb +9 -0
  29. data/spec/dummy/config/environments/test.rb +12 -0
  30. data/spec/dummy/config/initializers/lena.rb +10 -0
  31. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  32. data/spec/dummy/config/routes.rb +7 -0
  33. data/spec/dummy/log/development.log +170 -0
  34. data/spec/dummy/log/test.log +352 -0
  35. data/spec/dummy/spec/javascripts/support/jasmine.yml +20 -0
  36. data/spec/dummy/tmp/cache/assets/development/sprockets/1c6fd1c437c9411ba952f11bacc0320f +0 -0
  37. data/spec/dummy/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  38. data/spec/dummy/tmp/cache/assets/development/sprockets/746c4b5e95c34b98c11d314938f4eaf1 +0 -0
  39. data/spec/dummy/tmp/cache/assets/development/sprockets/b7a52d401e731277ea560db378ef97c3 +0 -0
  40. data/spec/dummy/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  41. data/spec/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  42. data/spec/dummy/tmp/cache/assets/test/sprockets/1c6fd1c437c9411ba952f11bacc0320f +0 -0
  43. data/spec/dummy/tmp/cache/assets/test/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  44. data/spec/dummy/tmp/cache/assets/test/sprockets/b7a52d401e731277ea560db378ef97c3 +0 -0
  45. data/spec/dummy/tmp/cache/assets/test/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  46. data/spec/dummy/tmp/cache/assets/test/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  47. data/spec/features/lena_spec.rb +30 -0
  48. data/spec/spec_helper.rb +51 -0
  49. metadata +239 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: af1c091763d076159eb0bf25c8a8dc5c324108fb
4
+ data.tar.gz: 61316f9e5b1c8acad448fc65753c7038202546c6
5
+ SHA512:
6
+ metadata.gz: 7f6409860c8a4d0ea141264dd6a478e16d5dd3ef6ad26db77273658763fcb943a2ec53b16ccee02b4503a46210b2e987453f4c2983437c87d2c4f29404bfd18d
7
+ data.tar.gz: a562b29218b58b5f36ca29f88c636842bfa57643bcc537b1bc620f3250f84e429eea364533a1a2e87137a3fd77a4a6495d27f5d69fe563a9db2d4d93ba1aae05
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Whitney Young
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ = Leña {<img src="https://secure.travis-ci.org/wbyoung/lena.png"/>}[http://travis-ci.org/wbyoung/lena] {<img src="https://codeclimate.com/github/wbyoung/lena.png"/>}[https://codeclimate.com/github/wbyoung/lena] {<img src="https://coveralls.io/repos/wbyoung/lena/badge.png"/>}[https://coveralls.io/r/wbyoung/lena]
2
+
3
+ Leña provides simple server-side JavaScript error logging for production Rails applications. This allows you to better track errors that occur in your front-end code.
4
+
5
+ == Installation
6
+
7
+ Add the following to your +application.js+:
8
+
9
+ //= require lena
10
+
11
+ Add the following to your +routes.rb+:
12
+
13
+ mount Lena::Engine => "/lena"
14
+
15
+ Update your +application.html.erb+:
16
+
17
+ <%= javascript_include_tag "application", lena.configuration %>
18
+
19
+ If you use Turbolinks or provide other options for your javascript include, you can merge them: <tt>lena.configuration.merge("data-something" => true)</tt>.
20
+
21
+ == Usage Client Side
22
+
23
+ Leña will now track all exceptions that are thrown in your application. You can also use Leña to log individual errors without throwing an exception. In your JavaScript, simply:
24
+
25
+ lena.log('My Error Message')
26
+
27
+ == Usage Server Side
28
+
29
+ Leña will simply throw an exception, <tt>Lena::JavaScriptError</tt>, when it receives a log message. Why? Because you should be using {something}[https://github.com/smartinez87/exception_notification] to report server errors when they occur. Also that's basically what's happening on the client side, so why not throw an exception on the server?
30
+
31
+ If you need to configure what Leña does, you can add +config/initializers/lena.rb+:
32
+
33
+ Lena.setup do |config|
34
+ config.javascript_handler = Proc.new do |params|
35
+ # Custom handling of log message here
36
+ end
37
+ end
38
+
39
+
40
+ == Alternative Setup
41
+
42
+ An alternative to setting up Leña on your +application.js+ file is to import Leña separately. This may impact performance, but it will catch compiler errors in any scripts included after it:
43
+
44
+ <%= javascript_include_tag "lena", lena.configuration %>
45
+
46
+ == License
47
+
48
+ This project is distributed under the MIT-LICENSE.
@@ -0,0 +1,29 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Lena'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ Bundler::GemHelper.install_tasks
21
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each { |f| load f }
22
+
23
+ require 'rspec/core'
24
+ require 'rspec/core/rake_task'
25
+
26
+ desc "Run all specs in spec directory (excluding plugin specs)"
27
+ RSpec::Core::RakeTask.new
28
+
29
+ task :default => :spec
@@ -0,0 +1,78 @@
1
+ throw new Error('Lena has already been loaded!') if window.lena != undefined
2
+
3
+ # Global settings for Leña read from the script element that loaded this.
4
+ Settings = do ->
5
+ elements = document.getElementsByTagName 'script'
6
+ read = (attribute) ->
7
+ attributes = (el.getAttribute(attribute) for el in elements)
8
+ attributes = (attribute for attribute in attributes when attribute)
9
+ attributes[0]
10
+ destination: read('data-lena-destination')
11
+ submitURL: read('data-lena-url')
12
+
13
+ # The handlers for each type of logging.
14
+ # These take an object with the following properties:
15
+ # message: The error message
16
+ # stacktrace: The stack trace that led to the error (could be null)
17
+ handlers =
18
+ remote: (data) ->
19
+ query = for key, value of data when value
20
+ "#{key}=#{encodeURIComponent(value)}"
21
+ xhr = new XMLHttpRequest()
22
+ xhr.open "post", Settings.submitURL, true
23
+ xhr.setRequestHeader 'Content-type', 'application/x-www-form-urlencoded'
24
+ xhr.send query.join('&')
25
+
26
+ local: (data) ->
27
+ message = data.message
28
+ message = "#{message}\n#{data.stacktrace}" if data.stacktrace
29
+
30
+ if console?.warn
31
+ console.warn(message)
32
+ else if console?.log
33
+ console.log(message)
34
+ else
35
+ alert(message)
36
+
37
+ # The main Leña handler. All user calls go through here. The final
38
+ # data is aggregated before it is dispatched to one of the handlers.
39
+ lena = (message, stacktrace, destinations=[]) ->
40
+ if stacktrace == undefined
41
+ try
42
+ throw new Error('')
43
+ catch e
44
+ stacktrace = e.stack
45
+ try
46
+ stacktrace = stacktrace.split("\n")[2..].join("\n")
47
+ try
48
+ stacktrace ||= 'unsupported'
49
+
50
+ data =
51
+ message: message
52
+ stacktrace: stacktrace
53
+
54
+ handlers[d](data) for d in destinations
55
+
56
+ # Log a message via Leña. This uses the configuration values to determine
57
+ # where the message should be sent (remote/local/both).
58
+ lena.log = (message, stacktrace) ->
59
+ destinations = []
60
+ switch Settings.destination
61
+ when 'local' then destinations = ['local']
62
+ when 'remote' then destinations = ['remote']
63
+ else destinations = ['local', 'remote']
64
+ lena(message, stacktrace, destinations)
65
+
66
+ # Log a message via Leña. Only logs remote (server).
67
+ lena.remote = (message, stacktrace) ->
68
+ lena(message, stacktrace, ['remote'])
69
+
70
+ # Log a message via Leña. Only logs to local (browser).
71
+ lena.local = (message, stacktrace) ->
72
+ lena(message, stacktrace, ['local'])
73
+
74
+ window.lena = lena
75
+ window.lena.onerror = window.onerror
76
+ window.onerror = (exception, url, linenum) ->
77
+ lena.log("#{exception}\nResource: #{url}:#{linenum}", null)
78
+ if lena.onerror then lena.onerror() else true
@@ -0,0 +1,8 @@
1
+ module Lena
2
+ class LenaController < ActionController::Base
3
+ def log
4
+ Lena::Engine.config.javascript_handler.call params
5
+ render text: ""
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ Lena::Engine.routes.draw do
2
+ post 'log', to: 'lena#log', as: :submission
3
+ end
@@ -0,0 +1,4 @@
1
+ require "lena/engine"
2
+
3
+ module Lena
4
+ end
@@ -0,0 +1,20 @@
1
+ require 'lena/rails/routing'
2
+
3
+ module Lena
4
+ class JavaScriptError < Exception; end
5
+
6
+ class Engine < ::Rails::Engine
7
+ isolate_namespace Lena
8
+ initializer "lena.assets.precompile" do |app|
9
+ app.config.assets.precompile += %w(lena.js)
10
+ end
11
+
12
+ config.javascript_handler = Proc.new do
13
+ raise JavaScriptError.new
14
+ end
15
+ end
16
+
17
+ def self.setup
18
+ yield Engine.config
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ require 'lena/routing'
2
+
3
+ module ActionDispatch::Routing
4
+ class RoutesProxy
5
+ include Lena::Routing
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ module Lena
2
+ # Extending the object that's returned from rails routing helpers (for
3
+ # instance +main_app+ or +lena+) that allow calling specific URL helper
4
+ # methods.
5
+ module Routing
6
+ def configuration
7
+ return {
8
+ 'data-lena-destination' => Rails.env.development? ? "local" : "",
9
+ 'data-lena-url' => self.submission_path,
10
+ }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Lena
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks
@@ -0,0 +1,14 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require lena
14
+ //= require_tree .
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,3 @@
1
+ <h1>Home Page</h1>
2
+ <script type="text/javascript">
3
+ </script>
@@ -0,0 +1,4 @@
1
+ <h1>Error Generating Page</h1>
2
+ <script type="text/javascript">
3
+ lena.log('Simple error log')
4
+ </script>
@@ -0,0 +1,4 @@
1
+ <h1>Error Generating Page</h1>
2
+ <script type="text/javascript">
3
+ throw new Error('Simple error throw');
4
+ </script>
@@ -0,0 +1,10 @@
1
+ <h1>Error Generating Page</h1>
2
+ <script type="text/javascript">
3
+ function callUndefined() {
4
+ undefinedFunctionCall();
5
+ }
6
+ function begin() {
7
+ callUndefined();
8
+ }
9
+ begin();
10
+ </script>
@@ -0,0 +1,13 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= javascript_include_tag "application", lena.configuration.merge("data-turbolinks-track" => true) %>
6
+ <%= csrf_meta_tags %>
7
+ </head>
8
+ <body>
9
+
10
+ <%= yield %>
11
+
12
+ </body>
13
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,14 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ # Pick the frameworks you want:
4
+ require "action_controller/railtie"
5
+ require "action_mailer/railtie"
6
+ require "sprockets/railtie"
7
+
8
+ Bundler.require(*Rails.groups)
9
+ require "lena"
10
+
11
+ module Dummy
12
+ class Application < Rails::Application
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,5 @@
1
+ # Load the Rails application.
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the Rails application.
5
+ Dummy::Application.initialize!
@@ -0,0 +1,9 @@
1
+ Dummy::Application.configure do
2
+ config.cache_classes = false
3
+ config.eager_load = false
4
+ config.consider_all_requests_local = true
5
+ config.action_controller.perform_caching = false
6
+ config.action_mailer.raise_delivery_errors = false
7
+ config.active_support.deprecation = :log
8
+ config.assets.debug = true
9
+ end
@@ -0,0 +1,12 @@
1
+ Dummy::Application.configure do
2
+ config.cache_classes = true
3
+ config.eager_load = false
4
+ config.serve_static_assets = true
5
+ config.static_cache_control = "public, max-age=3600"
6
+ config.consider_all_requests_local = true
7
+ config.action_controller.perform_caching = false
8
+ config.action_dispatch.show_exceptions = false
9
+ config.action_controller.allow_forgery_protection = false
10
+ config.action_mailer.delivery_method = :test
11
+ config.active_support.deprecation = :stderr
12
+ end
@@ -0,0 +1,10 @@
1
+
2
+ # For testing, we'll store all events into an array
3
+ Lena.class.send(:cattr_accessor, :events)
4
+ Lena.events = []
5
+
6
+ Lena.setup do |config|
7
+ config.javascript_handler = Proc.new do |params|
8
+ Lena.events << params
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key is used for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+
6
+ # Make sure the secret is at least 30 characters and all random,
7
+ # no regular words or you'll be exposed to dictionary attacks.
8
+ # You can use `rake secret` to generate a secure secret key.
9
+
10
+ # Make sure your secret_key_base is kept private
11
+ # if you're sharing your code publicly.
12
+ Dummy::Application.config.secret_key_base = '71332c3167f676af35edf85850150c3d311af76e1888679e5b424459a8045ba9bdfcd07ddf39a45db4e11922ab74a14a5b6e7e82a5f1d8014affcfc266ecab74'
@@ -0,0 +1,7 @@
1
+ Rails.application.routes.draw do
2
+ mount Lena::Engine => "/lena"
3
+ root to: 'application#index'
4
+ get 'log', to: 'application#log'
5
+ get 'throw', to: 'application#throw'
6
+ get 'throw_callstack', to: 'application#throw_callstack'
7
+ end