Skip to content

Instantly share code, notes, and snippets.

View jamesmartin's full-sized avatar
🤫
Shhh

James Martin jamesmartin

🤫
Shhh
View GitHub Profile
@jamesmartin
jamesmartin / unit_definitions.txt
Last active January 28, 2020 07:59
Alfred Convert plugin unit definitions for computer units
# Add your custom definitions to this file. See the Pint documentation for
# information on the necessary format:
# http://pint.readthedocs.org/en/latest/defining.html
# You can see the default definitions built in to Pint here:
# https://github.com/hgrecco/pint/blob/master/pint/default_en.txt
gigabyte = 1000 * megabyte = gb = GB
megabyte = 1024 * kilobyte = mb = MB
kilobyte = 1024 * byte = kb = KB
byte = 8 * bit = B
bit = [data] = b
@jamesmartin
jamesmartin / app.rb
Created February 18, 2016 08:01
Custom title tag for each page of a Sinatra app
before do
# We set this @title instance variable to a default value, so that if any of our pages *don't* want a custom title, something will appear in the <title> tag.
@title = "My Default Title"
end
# For this route we set a custom title for the page using the @title instance variable
get '/some/path' do
@title = "Whatever"
# render &etc...
end
@jamesmartin
jamesmartin / flatten.js
Created November 18, 2015 22:09
A tail-recursive flatten function, apparently (found code)
exports.first = function(l) {
return l[0]
};
exports.rest = function(l) {
return l.reduce(function(accumulator, item, index) {
if(index > 0) {
accumulator.push(item);
}
return accumulator;
var Template = function () { return `<?xml version="1.0" encoding="utf-8" ?><document><head><style>.whiteText{color:rgb(255,255,255);}</style></head><catalogTemplate><banner><title>Store Catalog</title></banner><list><section><listItemLockup><title>Menswear</title><decorationLabel>30</decorationLabel><relatedContent><grid><section><lockup template="http://localhost:5000/templates/ShowProduct.xml.js?handle=joshua-sweatshirt-1"><img src="https://cdn.shopify.com/s/files/1/0104/8852/products/Soulland-PS16-Joshua-sweat-black-24476-center.jpg?v=1447668258" width="308" height="308"></img><title class="whiteText">Joshua Sweatshirt</title></lockup><lockup template="http://localhost:5000/templates/ShowProduct.xml.js?handle=thingtved-sweatshirt-1"><img src="https://cdn.shopify.com/s/files/1/0104/8852/products/Soulland-PS16-Thingtved-sweat-black-20286-Final-center.jpg?v=1445851408" width="308" height="308"></img><title class="whiteText">Thingtved Sweatshirt</title></lockup><lockup template="http://localhost:5000/template
@jamesmartin
jamesmartin / declarative-form.js
Created August 23, 2015 01:05
Stop writing custom forms
new DeclarativeForm({
method: 'POST',
action: '/my-server/does/things/with/forms',
localCsrfToken: 'a7u9k9j87o7kj9j89o9jjk7o',
components: {
name: {
type: 'text', placeHolder: 'Enter your name...', initialValue: '',
validations: { required: true, minLength: 10 }
},
dateOfBirth: {
@jamesmartin
jamesmartin / redis.log
Created July 17, 2015 04:30
Redis Recipe — sysctl Error
Recipe Compile Error in /etc/chef-custom/recipes/cookbooks/main/recipes/default.rb
================================================================================
NameError
---------
Cannot find a resource for sysctl on gentoo version 2.1
Cookbook Trace:
---------------
/etc/chef-custom/recipes/cookbooks/redis/recipes/default.rb:9:in `from_file'
@jamesmartin
jamesmartin / ci_artifacts.rb
Created July 8, 2015 00:50
Circle CI Build Artifact URIs for a given project/branch
require 'net/http'
require 'json'
if __FILE__ == $0
wanted_project = ARGV[0] || "my_project"
wanted_branch = ARGV[1] || "master"
puts "Latest CI Artifacts for #{wanted_project}:"
api_root = "https://circleci.com/api/v1/"
@jamesmartin
jamesmartin / app.rb
Last active August 29, 2015 14:22
Rack middleware to redirect HTTPS to HTTP
require 'sinatra'
require 'only_http'
use OnlyHttp
get '/' do
"We'll never see this over HTTPS!"
end
@jamesmartin
jamesmartin / application_controller.rb
Created May 25, 2015 01:47
Testing ApplicationController before_filter methods using RSpec's "anonymous" controller instance
class ApplicationController < ActionControllerBase
helper :do_something
def do_something
@from_do_something = params[:for_do_something]
end
end
@jamesmartin
jamesmartin / value_or_reference.rb
Created May 6, 2015 12:20
Ruby is pass reference by value
def value_and_identity(object)
"(Value: #{object.inspect}, ID: #{object.object_id})"
end
def increment(val)
puts "---> #increment received argument #{value_and_identity(val)}"
result = val += 1
puts "---> 'result' is #{value_and_identity(result)}"