Skip to content

Instantly share code, notes, and snippets.

View koos's full-sized avatar
💭
Coding

Jan koos

💭
Coding
View GitHub Profile
function init() {
var allHTMLTags=document.getElementsByTagName('td');
getElem('id','notMandatoryRow',0).style.display="none";
var allHTMLTags=document.getElementsByTagName('td');
for (i=0; i<allHTMLTags.length; i++) {
if (allHTMLTags[i].className=="label") {
var e = allHTMLTags[i].innerHTML;
if(e.match(/\*/) != null) {
allHTMLTags[i].innerHTML = e.replace(/\*/,"");
validates_each :iban do | record, attr, value |
record.errors.add attr, 'IBAN is mandatory' and next if value.blank?
# IBAN code should start with country code (2letters)
record.errors.add attr, 'Country code is missing from the IBAN code' and next unless value.to_s =~ /^[A-Z]{2}/i
iban = value.gsub(/[A-Z]/) { |p| (p.respond_to?(:ord) ? p.ord : p[0]) - 55 }
record.errors.add attr, 'Invalid IBAN format' unless (iban[6..iban.length-1].to_s+iban[0..5].to_s).to_i % 97 == 1
end
@koos
koos / gist:1165987
Created August 23, 2011 17:51
fried egg with applesauce and mashed potatoes
{
"recipe": {
"id": "",
"category": "eggs",
"nerd_level": 1,
"photo_url": "",
"preparation": "",
"name": "fried egg with applesauce and mashed potatoes",
"created_at": "2011-08-22",
"created_by": "koos",
@koos
koos / gist:1176845
Created August 28, 2011 16:08
api example in rails 2.0
class UsersController < ApplicationController::Base
def index
@users = User.all
respond_to do |format|
format.html
format.xml { render :xml => @users }
format.json { render :json => @users }
end
end
@koos
koos / gist:1176847
Created August 28, 2011 16:10
api example in rails 3.0
class UsersController < ApplicationController::Base
respond_to :html, :xml, :json
def index
respond_with(@users = User.all)
end
def create
@user = User.create(params[:user])
@koos
koos / gist:1176858
Created August 28, 2011 16:18
as_json
render :json => { :success => true, :user => @user.as_json(:only => [:email]) }
@koos
koos / gist:1180699
Created August 30, 2011 11:29
app.rb rabl
# app/app.rb
get "/posts", :provides => [:json, :xml] do
@user = current_user
@posts = Post.order("id DESC")
render "posts/index"
end
@koos
koos / gist:1180701
Created August 30, 2011 11:29
index.rabl
# app/views/posts/index.rabl
collection @posts
attributes :id, :title, :subject
child(:user) { attributes :full_name }
node(:read) { |post| post.read_by?(@user) }
The result of this is a JSON (or XML) looking like this:
[{ post :
{
@koos
koos / gist:1180702
Created August 30, 2011 11:29
json schema controller
class Api::UsersController < ApiController
def show
respond_with(object_as_json(User.find_by_slug(params[:id])))
end
end
@koos
koos / gist:1180703
Created August 30, 2011 11:30
json schema
{
"type": "object",
"title": "user",
"description": "a user",
"properties": {
"id": {
"type": "string",
"description": "The User Id",
"identity": true,
"required": true,