Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
JoshCheek / example.js
Last active August 29, 2015 13:58
JS function that returns a function in order to bind an attribute.
function markRowHidden(dataAttribute) {
return function() {
var tr = $(this).closest('tr');
tr.data(dataAttribute, true);
tr.hide();
}
}
$("tr form.type1").on("submit", markRowHidden("attribute1"));
$("tr form.type2").on("submit", markRowHidden("attribute2"));
@JoshCheek
JoshCheek / testing_a_client_for_a_backend_app.rb
Last active August 29, 2015 14:00
How to test a client for a backend app. Creates a simple app, simple client, consumes it with rack-test, starts it up on a server, consumes it with restclient.
# backend app
require 'sinatra/base'
require 'json'
class UsersController < Sinatra::Base
get '/users/:id' do
JSON.dump id: params[:id].to_i, name: "Josh"
end
end
@JoshCheek
JoshCheek / explanation-of-easter.md
Last active August 29, 2015 14:00
Explanation of Easter.

For those of you who were confused about why all the Christians you knew were in a tizzy yesterday, it's not because they were celebrating April 20, rather they were celebrating a Christian holiday known as Easter. Allow me to explain:

Christians believe there is an entity known as "god". It's not clear what makes this entity special, some think it created everything (though it's unclear what created the entity). Some think it is the most powerful entity that exists, and that gives it special rights, like the right to determine what is morally permissible. This belief is important to Easter, as you'll see below. Also, god is a boy, even though it is usually considered noncorporeal (no chromosomes, no penis), so we will refer to it as a he from now on.

God made you perfectly, with full knowledge of everything you would do. For some reason, you don't live up to his standards, though. Not living up to his standard is called a "sin". Because of your sin

@JoshCheek
JoshCheek / stripe.md
Created April 29, 2014 16:40
Is this how stripe works?

I think this is how Stripe works

  1. There is a form on my page which collects credit card information
  2. The user fills this out, it is sent via JavaScript to Stripe
  3. Stripe returns a status code and if it worked, a token
  4. The callback then submits the token to your server where you associate it to that user by passing it as the card when you create a customer via their api
  5. The customer has a customer id, which you need to associate to your user
  6. You can then charge the customer through the api using the customer id
  7. Presumably reponse has a payment id or something which you can then save in your db to record that the user has paid you
@JoshCheek
JoshCheek / you_know_we_have_a_subdomain_now_middleware.rb
Last active August 29, 2015 14:00
A middleware I wrote to redirect requests to the herokuapp domain over to apply.turing.io
class YouKnowWeHaveASubdomainNowMiddleware
MovedPermanentlyCode = 301
def initialize(app, attributes)
self.app = app
self.from = attributes.fetch :from
self.to = attributes.fetch :to
end
def call(env)
@JoshCheek
JoshCheek / mandatory_keyword_args.rb
Created May 2, 2014 15:45
Are mandatory keyword args a thing? I find myself wanting to do this a lot. UPDATE: answer is yes, see https://gist.github.com/JacobNinja/a38d0f4b02759c910dc3
def mandatory
required = method(caller.first[/(?<=`).*(?=')/] # => "crazy_arglist!"
) # => #<Method: Object#crazy_arglist!>
.parameters # => [[:req, :a], [:opt, :b], [:rest, :c], [:key, :d], [:key, :e], [:keyrest, :f], [:block, :g]]
.select { |type, name| type == :key } # => [[:key, :d], [:key, :e]]
.map { |type, name| name } # => [:d, :e]
raise ArgumentError, "Must provide #{required.join ", "}" # ~> ArgumentError: Must provide d, e
end
def crazy_arglist!(a, b=1, *c, d: mandatory, e: mandatory, **f, &g)
@JoshCheek
JoshCheek / example_spec.rb
Created May 4, 2014 19:06
Using poltergeist to navigate the web.
require 'capybara/rspec'
require 'capybara/poltergeist'
Capybara.javascript_driver = Capybara.current_driver = :poltergeist
describe 'google.com', type: :feature do
specify "I can search for and find testdouble's website" do
page.visit 'http://duckduckgo.com/'
page.fill_in 'q', with: 'test double'
page.click_on 'search_button_homepage'
@JoshCheek
JoshCheek / gist:a55eadccf20b683be83a
Created May 5, 2014 16:12
example of secrets.yml overriding with ENV
development:
stripe_publishable_key: pk_test_abc123
stripe_secret_key: sk_test_def456
test:
stripe_publishable_key: STRIPE_PUBLISHABLE_KEY
stripe_secret_key: STRIPE_SECRET_KEY
production:
stripe_publishable_key: <%= ENV["STRIPE_PUBLISHABLE_KEY"] %>
Prefix Verb URI Pattern Controller#Action
root GET / courses#index
course GET /courses/:id(.:format) courses#show
invitation_required GET /invitation-required(.:format) site#invitation_required
enrollment POST /enrollment(.:format) enrollment#create
payment_plan GET /enrollment/payment-plan(.:format) enrollment#edit_payment_plan
enrollment_payment_plan PUT /enrollment/payment-plan(.:format) enrollment#create_payment_plan
PATCH /enrollment/payment-plan(.:format) enrollment#create_payment_plan
enter_payment_information GET /enrollment/payment-info(.:format) enrollment#enter_payment_information
enrollment_payment_info POST /enrollment/payment-info(.:format) enrollment#create_payment_information
@JoshCheek
JoshCheek / deploy.rake
Created May 8, 2014 18:51
Uhh... Is this how other people are dealing with private dependencies?
namespace :deploy do
desc 'Deploy the app to Heroku'
task :heroku do
inform = lambda { |message| puts "\e[37;44m#{message}\e[0m" }
deploy_branch = 'deploy'
current_branch = %x"git rev-parse --abbrev-ref HEAD".chomp
if current_branch == deploy_branch
inform.call "You're in the #{deploy_branch} branch, which we need to reset, please go elsewhere ;)"
exit 1