Skip to content

Instantly share code, notes, and snippets.

@mayfer
mayfer / player.rb
Created March 6, 2015 22:51
Introduction to Objects and Classes
class Person
attr_accessor(:name, :email)
# class variable
@@emails = []
@@population = 0
def initialize(name, email)
@mayfer
mayfer / actions.rb
Created March 11, 2015 18:27
Simple Ajax Sinatra example
require 'sinatra/json'
def get_click_data
{
count: Click.count,
last_timestamp: Click.all.order('created_at DESC').first.created_at
}
end
get '/' do
@mayfer
mayfer / gamblerat.html
Created April 7, 2015 18:31
jQuery examples (event propagation, dom manipulation, form submission, input validation)
<html>
<head>
<title>Intro to Javascript</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<style>
body { background: #358; color: #fff; padding: 50px; font-family: sans-serif; }
.row {
background: rgba(255, 255, 255, 0.5);
@mayfer
mayfer / venue.rb
Created April 14, 2015 17:53
Simple ORM livecoding example
require 'pg'
$conn = PG::Connection.open(:dbname => 'lighthouse')
class Venue
attr_accessor :name, :capacity, :id
def initialize(name, capacity)
@id = nil
@name = name
@mayfer
mayfer / orm.rb
Created April 14, 2015 17:54
Generic ORM example, that works for any class that matches a table.
class ORM
def save
if defined? @id
sql_update
else
sql_insert
end
end
@mayfer
mayfer / http.rb
Created April 17, 2015 21:57
Demonstrating HTTP concepts with sinatra
require 'sinatra'
post '/' do
response.set_cookie("username", params[:username])
redirect '/'
end
get '/logout' do
response.set_cookie("username", value: "test", expires: Time.now )
@mayfer
mayfer / algorithms.rb
Created April 20, 2015 20:14
algorithms and Data structures examples
def find_index(numbers, number)
numbers.each_with_index do |n, i|
if n == number
return i
end
end
return nil
end
@mayfer
mayfer / actions.rb
Created April 23, 2015 18:34
Sinatra login/logout
# Homepage (Root path)
enable :sessions
helpers do
def current_user
begin
@current_user = User.find(session["user_id"].to_i)
# @current_user = User.find(response.cookies["user_id"].to_i)
rescue
@current_user = nil
@mayfer
mayfer / sesson.rb
Created April 23, 2015 18:38
Mock implementation of sessions
class Session
def [](key)
cookie = request.cookies["rack.session"]
output = decrypt(cookie)
return output["user_id"]
end
html, body {
}
.error {
background: #faa;
color: #a00;
}
.container {