Skip to content

Instantly share code, notes, and snippets.

@joshmcarthur
joshmcarthur / api.rb
Created May 29, 2013 08:42
A one-file adapter for any HTTP basic RESTful API. Uses Faraday for making HTTP requests, and dotenv for keeping your API tokens in a file named '.env'. Simply require this file, and start making requests!
# api.rb
#
# Requirments:
# As a service resource
# I want to be able to make API requests in a consistent manner
# So that results are predicatble and easy to handle.
#
# Synopsis:
# This file sets up a Faraday engine to make HTTP requests with,
# and defines a number of helper methods to make API requests easy.
@joshmcarthur
joshmcarthur / nginx.conf
Created May 30, 2013 23:17
Sample nginx configuration for streaming to Unicorn
upstream unicorn {
server unix:/tmp/unicorn.inquest.sock fail_timeout=0;
}
server {
listen 80 default deferred;
root /home/inquest/inquest/public;
try_files $uri/index.html $uri @unicorn;
location @unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
@joshmcarthur
joshmcarthur / unicorn.rb
Created May 30, 2013 23:17
Sample Unicorn configuation file for talking to Nginx
working_directory "/home/inquest/inquest"
pid "/home/inquest/inquest/tmp/pids/unicorn.pid"
stderr_path "/home/inquest/inquest/log/unicorn.log"
stdout_path "/home/inquest/inquest/log/unicorn.log"
listen "/tmp/unicorn.inquest.sock"
worker_processes 5
timeout 30
preload_app true
@joshmcarthur
joshmcarthur / travis_database.yml
Created June 19, 2013 01:54
Travis CI example database.yml
test:
host: localhost
adapter: postgresql
database: fishtale_test
@joshmcarthur
joshmcarthur / introducable.rb
Last active December 19, 2015 01:09
Validate the length (in words) of a string
# Short introduction may be blank, but may not be longer than 250 words
validates :short_introduction, length: {maximum: 250, tokenizer: -> (str) { str.scan(/\w+/) } }
@joshmcarthur
joshmcarthur / controller_spec.rb
Created July 2, 2013 22:41
Stubbing out a controller instance variable
before do
controller.instance_variable_set("@interactive_content_item", resource)
controller.stub(:set_interactive_content_item).and_return(true)
end
@joshmcarthur
joshmcarthur / indexable.rb
Created July 3, 2013 22:16
Paginate ElasticSearch results with Kaminari and Tire
tire.search :page => params[:page]
@joshmcarthur
joshmcarthur / example_controller.js.coffee
Created July 6, 2013 08:11
An AngularJS service for finding a user's current location and street address (Using navigator.geolocation and OSM)
ExampleController = ($scope, LocationService) ->
LocationService.locate().then (position) ->
$scope.position = position
ExampleController.$inject = ["$scope", "LocationService"]
YourAngularApp.controller("ExampleController", ExampleController)
@joshmcarthur
joshmcarthur / method_spec.rb
Last active December 19, 2015 19:08
Testing the effects of a method call
describe "#perform" do
subject { -> { model.perform }.call }
it { expect { subject }.to change(Vehicle, :count).by(1) }
it { expect { subject }.to change(Registration, :count).by(1) }
it { expect { subject }.to change(AuditLog, :count).by(5) }
end
@joshmcarthur
joshmcarthur / controller_spec.rb
Created July 17, 2013 04:48
How to send a file object in a POST request with RSpec and Rack::Test
post :create,
splash_screen: {
image: Rack::Test::UploadedFile.new(Rails.root.join("spec/assets/example_splash_screen.jpg"), 'image/jpg')
}