Skip to content

Instantly share code, notes, and snippets.

@harlow
harlow / booking_payments_controller.rb
Last active December 15, 2015 17:09 — forked from jordelver/booking_payment_controller.rb
Use ActiveModel::Model for validations on non-AR objects
class BookingPaymentsController < ApplicationController
def create
booking_payment = BookingPayment.new(booking_payment_params)
booking_payment.pay
respond_with payment, location: balance_trip_path
end
private
@harlow
harlow / rspec_spies.rb
Created April 6, 2013 16:53
using spies with rspec-mocks
require 'spec_helper'
describe Importer, '#import' do
it 'creates a record for each row' do
contact = double(create: true)
csv_parser = double(rows: [:row1, :row2])
Importer.new(contact, csv_parser).import
expect(contact).to have_received(:create).with(:row1)
# ------------------------------------------------------------------
# Desigining "trending topics in 24 hours sliding window" with Redis
# ------------------------------------------------------------------
redis-cli del tophashes:2010-12-07-08-00
redis-cli del tophashes:2010-12-07-09-00
redis-cli del tophashes:current
echo '=== 8:00 AM ==='
class TaskCompletion
include ActiveModel::Model
attr_accessor :task, :complete
validates :complete, inclusion: { in: [true, false] }
def self.for_task(task, params={})
new(params).tap do |completion|
completion.task = task
end
{
"_links": {
"self": { "href": "/orders" },
"next": { "href": "/orders?page=2" },
"find": { "href": "/orders{?id}", "templated": true }
},
"_embedded": {
"orders": [{
"_links": {
"self": { "href": "/orders/123" },
#!/bin/bash
# from here: http://www.codingsteps.com/install-redis-2-6-on-amazon-ec2-linux-ami-or-centos/
# and here: https://raw.github.com/gist/257849/9f1e627e0b7dbe68882fa2b7bdb1b2b263522004/redis-server
###############################################
# To use:
# wget https://raw.github.com/gist/2776679/04ca3bbb9f085b192f6aca945120fe12d59f15f9/install-redis.sh
# chmod 777 install-redis.sh
# ./install-redis.sh
###############################################
echo "*****************************************"
backend default {
.host = "127.0.0.1";
.port = "8000";
}
# We go BACK to varnish to get it to generate an ESI template that
# generates a JSON-P response.
backend jsonp_template_backend {
.host = "127.0.0.1";
.port = "8070";
@harlow
harlow / app.rb
Last active December 30, 2015 17:59
Refactoring ideas for https://github.com/JamesChevalier/hashtagged/blob/master/app.rb#L17-L27. Extract an object. Encapsulate the interaction with the Twitter client. Extract named methods for looping logic.
require 'sinatra'
require 'twitter'
class App < Sinatra::Base
get '/' do
erb :index
end
post '/hashtags' do
@user_name = user_name
require 'spec_helper'
describe UsersController, '#create' do
let(:group_id) { double(:group_id) }
let(:group) { double(:group) }
let(:user) { double(:user) }
before do
Group.should_receive(:find).with(group_id).and_return(group)
group.should_receive(:create_user).with(attributes).and_return(users)