Skip to content

Instantly share code, notes, and snippets.

View evanleck's full-sized avatar

Evan Lecklider evanleck

View GitHub Profile
@evanleck
evanleck / i18n.js
Created December 1, 2015 22:51
The Simplest JavaScript Internationalization I Can Think Of
//= require locales/en.js
//= require locales/es.js
/*
* Set a global variable `i18n` that contains the current localization strings.
* Locale files create simple objects, we just map it in based on the 'lang' attribute
* of the HTML node.
*/
(function(window, undefined) {
var currentLocale = window.document.documentElement.lang,
generalLocale = currentLocale.split('-')[0],
@evanleck
evanleck / en.js
Created December 1, 2015 22:52
The Simplest JavaScript Internationalization I Can Think Of
/*
* English language translations for use in JavaScript.
*/
(function(window, undefined) {
if (window.locales === undefined) {
window.locales = {};
}
window.locales.en = {
forms: {
@evanleck
evanleck / using-i18n.js
Created December 1, 2015 22:56
The Simplest JavaScript Internationalization I Can Think Of
return i18n.forms.numberPatternMismatch;
@evanleck
evanleck / example-app.rb
Created December 8, 2015 18:34
Simple Strong Parameters in Sinatra
require 'sinatra/base'
require 'sinatra/strong-params'
class ExampleApp < Sinatra::Base
configure do
register Sinatra::StrongParams
end
get '/', allows: [:search] do
# Only the 'search' parameter will make it to the execution scope.
@evanleck
evanleck / allows.rb
Created December 8, 2015 18:39
Simple Strong Parameters in Sinatra
#
# A way to whitelist parameters.
#
# get '/', allows: [:id, :action] do
# erb :index
# end
#
# Modifies the parameters available in the request scope.
# Stashes unmodified params in @_params
#
@evanleck
evanleck / needs.rb
Created December 8, 2015 18:41
Simple Strong Parameters in Sinatra
#
# A way to require parameters
#
# get '/', needs: [:id, :action] do
# erb :index
# end
#
# Does not modify the parameters available to the request scope.
# Raises a RequiredParamMissing error if a needed param is missing
#
@evanleck
evanleck / puma.rb
Created December 10, 2015 22:00
Better Low-Level Error Handling with Puma
# Inside of your Puma configuration file.
lowlevel_error_handler do
[500, { 'Content-Type' => 'text/html' }, File.open('50x.html')]
end
@evanleck
evanleck / 50x.html
Created December 10, 2015 22:06
Better Low-Level Error Handling with Puma
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='refresh' content='5'>
<title>Temporarily Unavailable</title>
<link rel='stylesheet' type='text/css' href='//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css'>
</head>
<body>
<div class='container'>
/*
* This is a naive attempt to protect against transparent proxies.
* - We pass the request host and path in from Rack and compare it against what JS sees.
* - If they don't match, put them where they should be.
*
*/
(function(location) {
if (location.hostname !== proxyCheck.hostname || location.pathname !== proxyCheck.pathname) {
/* Use an anchor tag as a parser. */
var redirectParser = document.createElement('a');
<script>proxyCheck = { hostname: '<%= request.host %>', pathname: '<%= request.path %>' };</script>