Skip to content

Instantly share code, notes, and snippets.

View ismasan's full-sized avatar

Ismael Celis ismasan

View GitHub Profile
# Common pattern for including instance and class level modules
module SomeModule
def self.included(my_class)
my_class.extend ClassMethods
end
module ClassMethods
def some_class_method(*args)
# do something at class level
puts args.inspect
# Free RESTful interface for your Ruby objects with Rackable
# http://github.com/madx/rackable/tree/master
#
# + Free HTTP caching with rack-cache
# http://github.com/rtomayko/rack-cache/tree/master
require 'rackable'
require 'rack/cache'
APP_ROOT = File.dirname(__FILE__)
# This module allows you to prepend a version prefix to your Sinatra URLs
# Example:
#
# require 'rubygems'
# require 'sinatra/base'
#
# class App < Sinatra::Base
# register Versioned
#
# set :version, 'v1'
# Put this in an initializer (config/initializers/common_scopes.rb) or library file.
#
module CommonScopes
module ClassMethods
# Sort given fields ASC or DESC
#
# Example:
#
# class Post < ActiveRecord::Base
# USAGE ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# require 'rack/versioner'
# V1 = Proc.new do |env|
# body = "Hello world from V1"
# [200, {"Content-Type" => "text/html", "Content-Length" => body.size.to_s}, [body]]
# end
#
# V2 = Proc.new do |env|
# body = "Hello world from V2"
# Create a unique, non-numeric ID for your ActiveRecord objects
require 'uuid' # http://github.com/assaf/uuid/
class User < ActiveRecord::Base
sluggable_finder :hashed_slug #creates slug from custom attribute and stores it in "slug" field
def hashed_slug #we define the custom attribute
UUID.new.generate
end
/* Jquery plugin for quick Comet long polling connections
Nothing special about this (just a recursive Ajax call).
The key is having the server hold connections open until there's data to push back to the client.
This can be done with Nginx and the nginx_http_push_module (http://github.com/slact/nginx_http_push_module)
---------------------------------------------------------------------------*/
(function($){
$.comet = function(url, success_callback, error_callback){
error_callback = error_callback || function(a,b,c){alert('Error '+b)};
$.ajax({
type: "GET",
/*
var getName = function(){return 'My name is '+this.name};
var Person = function(name){
this.name = name;
};
var Joe = new Person('Joe');
var joeName = getName.bind(Joe);
<!DOCTYPE html>
<html>
<head>
<style type="text/css" media="screen">
body {
padding:1em;margin:0;
}
.line.close {
color:red;
}
require 'rubygems'
require 'sinatra/base'
# SINATRA APP :::::::::::::::::::::::::::::::::::::
#
class BooticProxy < Sinatra::Base
# POST /mi-tienda/to/tugar/product # => push new product/property to Tugar
# This would be a JSON representation of a Bootic product, something like:
# {