Skip to content

Instantly share code, notes, and snippets.

@ayamomiji
ayamomiji / Gemfile
Created March 13, 2011 08:15
problem in testing async sinatra app in rails 3
gem 'thin'
gem 'sinatra', :require => 'sinatra/base'
gem 'async_sinatra', :require => 'sinatra/async'
group :development, :test do
gem 'rspec-rails'
end
require 'date'
class Date
def programmers_day?
self.yday == 0x100
end
end
Date.today.programmers_day? # => true if today is 9/14
module X
module Y
end
end
module X
module Z
Y # = X::Y
end
end
RSpec::Matchers.define :be_json do |expected|
match do |actual|
acturl_json =
ActiveSupport::JSON.decode(ActiveSupport::JSON.encode(actual))
expected_json =
ActiveSupport::JSON.decode(ActiveSupport::JSON.encode(expected))
acturl_json == expected_json
end
diffable
@ayamomiji
ayamomiji / deploy.rb
Created April 4, 2012 16:52
My capistrano + rainbows configure
# Check if remote file exists
def remote_file_exists?(full_path)
'true' == capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip
end
# Check if process is running
def remote_process_exists?(pid_file)
capture("ps -p $(cat #{pid_file}) ; true").strip.split("\n").size == 2
end
@ayamomiji
ayamomiji / net-http.rb
Created April 17, 2012 14:06
rest-core/app/net-http.rb
require 'rest-core/middleware'
require 'net/http'
class RestCore::NetHttp
include RestCore::Middleware
def call env
uri = URI.parse(request_uri(env))
payload = ::RestClient::Payload.generate(env[REQUEST_PAYLOAD])
client = Net::HTTP.new(uri.host, uri.port)
@ayamomiji
ayamomiji / json_column.rb
Created April 17, 2012 17:42
rails serialize column in json
class JsonColumn
def self.default_with(&block)
new(block)
end
def initialize(default)
@default = default
end
def dump(obj)
@ayamomiji
ayamomiji / rest.rb
Created April 27, 2012 07:55
rest http client idea
# client usage
client.resources('/articles').post # => POST /articles
client.articles.post # => POST /articles
client.article(1).get # => GET /articles/1
client.article(1).comments.get # => GET /articles/1/comments
client.admin # => client with admin support (for example, use http basic auth middleware)
client.admin.articles.get # => GET /admin/articles with http basic auth
# client define
@ayamomiji
ayamomiji / first.rb
Created May 15, 2012 07:33
resource loading patterns
class PostsController
before_filter :load_user
before_filter :load_post
def show
respond_with(@post)
end
def load_user
@user = User.find(params[:user_id])
def whatever
blabla
render nothing: true
end