Skip to content

Instantly share code, notes, and snippets.

View syntaxritual's full-sized avatar

Travis Anderson syntaxritual

View GitHub Profile
@wycats
wycats / 0_app.rb
Created April 19, 2012 10:22
Example of using a simple future library for parallel HTTP requests
class TicketsController < ApplicationController
def show
tickets = params[:tickets].split(",")
ticket_data = tickets.map do |ticket|
parallel { Faraday.get("http://tickets.local/#{ticket}") }
end
render json: { tickets: ticket_data.map(&:result) }
end
class ApplicationController < ActionController::Base
def self.present as, from, klass
if from.to_s.singularize == from.to_s
expose(as) { klass.new send(from) }
else
expose(as) { send(from).map { |orig| klass.new(orig) } }
end
end
end
@freegenie
freegenie / Gemfile
Created March 10, 2012 22:18
EventMachine + ZeroMQ Pipeline Pattern
# vim: ft=ruby
source :rubygems
gem 'eventmachine'
gem 'em-zeromq', :path => '~/Work/em-zeromq'
gem 'sinatra'
gem 'zmq'
@pepijndevos
pepijndevos / vocoder.clj
Created July 4, 2011 19:48
Overtone Vocoder
(ns test
(:use overtone.live
;overtone.inst.synth
overtone.inst.io))
(def a (buffer 2048))
(def b (buffer 2048))
(definst vocoder [freq 432]
(let [input (in 8)
module Padrino
# Minimal version of `decent_exposure`[1], in keeping with Padrino's
# simplicity.
#
# [1] https://github.com/voxdolo/decent_exposure
#
# == Usage
#
# The first step is to register the plugin with your application:
#
# I was asked for an example of code that was DRY, but still had an
# instance of Connascence of Algorithm going on.
#
# Consider the following code. I think most people would agree that
# the code is fairly DRY in the sense that there is no duplicated
# code.
#
require 'digest/sha1'
@matthuhiggins
matthuhiggins / after.rb
Created November 9, 2010 17:05
matter of style
class ProfileController < ActionController::Base
private
def current_user
@current_user ||= User.find_by_username(session[:username]) if session[:username]
end
helper_method :current_user
end
require 'sinatra'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => 'mysql',
:encoding => 'utf8',
:database => 'foo',
:username => 'bar',
:password => 'baz',
:host => 'localhost'
desc "Apply Google translate to auto translate all texts in locale ENV['FROM'] to locale ENV['TO']"
task :google => :environment do
raise "Please specify FROM and TO locales as environment variables" if ENV['FROM'].blank? || ENV['TO'].blank?
# Depends on httparty gem
# http://www.robbyonrails.com/articles/2009/03/16/httparty-goes-foreign
class GoogleApi
include HTTParty
base_uri 'ajax.googleapis.com'
def self.translate(string, to, from)
module Awesomeness
extend ActiveSupport::Concern
included do
scope :awesome, where(:favorite_language => 'ruby')
end
module ClassMethods
def make_everyone_awesome
update_all(:favorite_language => 'ruby')