Skip to content

Instantly share code, notes, and snippets.

View sanbiv's full-sized avatar
🏠
Working from home

Santi Bivacqua sanbiv

🏠
Working from home
View GitHub Profile
@sanbiv
sanbiv / telegram_webapp.go
Last active December 6, 2024 07:49
Telegram WebApp: validate initData
package telegram
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/url"
@sanbiv
sanbiv / generate-pushid.js
Created October 4, 2017 09:29 — forked from mikelehen/generate-pushid.js
JavaScript code for generating Firebase Push IDs
/**
* Fancy ID generator that creates 20-character string identifiers with the following properties:
*
* 1. They're based on timestamp so that they sort *after* any existing ids.
* 2. They contain 72-bits of random data after the timestamp so that IDs won't collide with other clients' IDs.
* 3. They sort *lexicographically* (so the timestamp is converted to characters that will sort properly).
* 4. They're monotonically increasing. Even if you generate more than one in the same timestamp, the
* latter ones will sort after the former ones. We do this by using the previous random bits
* but "incrementing" them by 1 (only in the case of a timestamp collision).
*/
require 'date'
class FirebasePush
PUSH_CHARS = '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'
def initialize
@prev_ts = 0
@rand_chars = Array.new(12)
end
@sanbiv
sanbiv / url_shortener.rb
Created July 20, 2016 13:28 — forked from pkieltyka/url_shortener.rb
Encoding/decoding functions for a URL Shortner -- in Ruby
module UrlShortener
class << self
CODESET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
BASE = CODESET.length # 62
def encode(id)
hash = ''
while id > 0
hash = CODESET[id % BASE].chr + hash
@sanbiv
sanbiv / ssl_puma.sh
Created July 13, 2016 10:01 — forked from tadast/ssl_puma.sh
localhost SSL with puma
# 1) Create your private key (any password will do, we remove it below)
$ cd ~/.ssh
$ openssl genrsa -des3 -out server.orig.key 2048
# 2) Remove the password
$ openssl rsa -in server.orig.key -out server.key
@sanbiv
sanbiv / application.rb
Created October 15, 2015 08:46
Rails multi-tenant / multi-database with subdomains using a control database
# config/application.rb
module MyApp
class Application < Rails::Application
def config.database_configuration
config = super
ActiveRecord::Base.establish_connection(config["control"])
@sanbiv
sanbiv / application_controller.rb
Created October 15, 2015 08:46
Rails multi-tenant / multi-database with subdomains using database.yml
before_filter :select_database
def select_database
if Rails.env == "production"
unless request.subdomain.empty?
begin
subdomain = request.subdomains.first
config = ActiveRecord::Base.configurations[subdomain]
if config
ActiveRecord::Base.establish_connection(config)
@sanbiv
sanbiv / css_resources.md
Created January 20, 2014 20:10 — forked from jookyboi/css_resources.md
CSS libraries and guides to bring some order to the chaos.

Libraries

  • 960 Grid System - An effort to streamline web development workflow by providing commonly used dimensions, based on a width of 960 pixels. There are two variants: 12 and 16 columns, which can be used separately or in tandem.
  • Compass - Open source CSS Authoring Framework.
  • Bootstrap - Sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development.
  • Font Awesome - The iconic font designed for Bootstrap.
  • Zurb Foundation - Framework for writing responsive web sites.
  • SASS - CSS extension language which allows variables, mixins and rules nesting.
  • Skeleton - Boilerplate for responsive, mobile-friendly development.

Guides

@sanbiv
sanbiv / javascript_resources.md
Created January 20, 2014 20:10 — forked from jookyboi/javascript_resources.md
Here are a set of libraries, plugins and guides which may be useful to your Javascript coding.

Libraries

  • jQuery - The de-facto library for the modern age. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers.
  • Backbone - Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.
  • AngularJS - Conventions based MVC framework for HTML5 apps.
  • Underscore - Underscore is a utility-belt library for JavaScript that provides a lot of the functional programming support that you would expect in Prototype.js (or Ruby), but without extending any of the built-in JavaScript objects.
  • lawnchair - Key/value store adapter for indexdb, localStorage
@sanbiv
sanbiv / rails_resources.md
Created January 20, 2014 20:10 — forked from jookyboi/rails_resources.md
Rails-related Gems and guides to accelerate your web project.

Gems

  • Bundler - Bundler maintains a consistent environment for ruby applications. It tracks an application's code and the rubygems it needs to run, so that an application will always have the exact gems (and versions) that it needs to run.
  • rabl - General ruby templating with json, bson, xml, plist and msgpack support
  • Thin - Very fast and lightweight Ruby web server
  • Unicorn - Unicorn is an HTTP server for Rack applications designed to only serve fast clients on low-latency, high-bandwidth connections and take advantage of features in Unix/Unix-like kernels.
  • SimpleCov - SimpleCov is a code coverage analysis tool for Ruby 1.9.
  • Zeus - Zeus preloads your Rails app so that your normal development tasks such as console, server, generate, and specs/tests take less than one second.
  • [factory_girl](h