Skip to content

Instantly share code, notes, and snippets.

View JamesChevalier's full-sized avatar

James Chevalier JamesChevalier

View GitHub Profile
@JamesChevalier
JamesChevalier / about.md
Created July 28, 2016 20:27
Quick description of attr_encrypted

attr_encrypted is a nice gem for encrypting fields in Rails.

You add this line to a model: attr_encrypted :field_name, key: ENV['attr_encrypted_key']. I used rake secret to generate that attr_encrypted_key referenced in that line.

The field itself needs to be created in a particular way in the migration:

add_column :table_name, :encrypted_field_name,    :text
add_column :table_name, :encrypted_field_name_iv, :text
@JamesChevalier
JamesChevalier / lambda_function.py
Last active November 28, 2022 12:58
How to run Newspaper in an Amazon Lambda function
from newspaper import Article
def lambda_handler(event, context):
url = event['url']
article = Article(url)
article.download()
article.parse()
return {
'content' : article.text
@JamesChevalier
JamesChevalier / index.js
Created March 15, 2016 01:23
Get the bounding box of a GeoJSON object, regardless of how many polygons it contains. The core of this is from http://mikefowler.me/2014/06/10/drawing-geojson-in-a-canvas/
function getBoundingBox(data) {
var bounds = {}, coordinates, point, latitude, longitude;
// Loop through each "feature"
for (var i = 0; i < data.features.length; i++) {
coordinates = data.features[i].geometry.coordinates;
if(coordinates.length === 1){
// It's only a single Polygon
// For each individual coordinate in this feature's coordinates...
@JamesChevalier
JamesChevalier / nestable_hash.rb
Created February 4, 2016 18:08
Create an infinitely nestable hash in Ruby
# Create the empty hash
nestable_hash = Hash.new { |hash, key| hash[key] = Hash.new(&hash.default_proc) }
# Populate the hash with multiple levels of data
nestable_hash[:level_one][:level_two][:level_three][:level_four] = 'mic check'
nestable_hash[:level_one][:level_two_a][:level_three_a] = [1, 2, 1, 2]
# The hash structure is now:
# {
# level_one: {
@JamesChevalier
JamesChevalier / etc_letsencrypt_cli_ini.j2
Last active May 24, 2016 20:16
Ansible role to install, run, and schedule Let's Encrypt
# template file
# Use a 4096 bit RSA key instead of 2048
# rsa-key-size = 4096
# Register with the specified e-mail address
email = {{ server_admin_email }}
# Generate certificates for the specified domains
domains = {{ domain_name }}
@JamesChevalier
JamesChevalier / info.md
Last active November 25, 2015 16:39
An Ember Route/Template/Component setup that displays a list of Posts.
  • There's a page that displays a list of Posts.
  • Each Post in the list links out to its individual Post page.
  • Both pages contain a form to add a new Post - this is the Component.
  • Additionally, the form can be shown/hidden.

The problem is that the actions need to exist in all possible Routes.

The new-post component bubbles up to whichever Route it's being called in. I'm calling it from the Posts Route, so I need to handle the savePost there... I'm also calling it from the Post Route, so I need to handle the savePost there, as well.

console.log('Loading event');
var AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB();
exports.handler = function(event, context) {
console.log("Request received:\n", JSON.stringify(event));
console.log("Context received:\n", JSON.stringify(context));
var tableName = "OurBlogDemo";
var datetime = new Date().getTime().toString();
@JamesChevalier
JamesChevalier / AWSLambdaSimpleSMS.js
Created November 11, 2015 23:03 — forked from stevebowman/AWSLambdaSimpleSMS.js
AWS Lambda Function to send an SMS message via the Twilio API
console.log('Loading event');
// Twilio Credentials
var accountSid = '';
var authToken = '';
var fromNumber = '';
var https = require('https');
var queryString = require('querystring');
@JamesChevalier
JamesChevalier / gist:9852785fbf379f330662
Last active October 25, 2015 20:15
It seems like the Overpass API does not return a complete result set

I'm using Overpass Turbo: http://overpass-turbo.eu/

This is the query I'm using to collect all places that are Admin Level 8 within California:

[out:json];
area['admin_level'='4']['name'='California'];
(relation['admin_level'='8'](area););
out;
@JamesChevalier
JamesChevalier / howto.md
Last active September 14, 2021 01:39
How to recover from commits pushed to the wrong branch

The scenario here is that you've got a lot of commits on the master branch that should have been committed to a feature branch. You want to reset master back to the last commit, and you don't want to lose your work. The process below is fairly specific to SourceTree.

  1. Create a new branch named placeholder at the point that you want to restore to ... This will be deleted later, after everything is confirmed ok.
    • In SourceTree: right click the commit, choose Branch..., name it placeholder, and click Create Branch
    • Push this to origin
  2. Create a new branch at your latest commit ... This is the branch that will contain further work, so you should stick to your typical feature branch naming conventions.
    • In SourceTree: switch back to the branch for that latest commit and create a new branch from there
  • Push this to origin