Skip to content

Instantly share code, notes, and snippets.

View alexdunae's full-sized avatar

Alex Dunae alexdunae

View GitHub Profile
@yahonda
yahonda / ruby31onrails.md
Last active April 9, 2024 20:46
Ruby 3.1 on Rails

Ruby 3.1 on Rails

Actions required to use Ruby 3.1.0 with Rails

Rails 7.0.Z

  • Rails 7.0.1 is compatible with Ruby 3.1.0.
  • Rails 7.0.1 addes net-smtp, net-imap and net-pop gems as Action Mailbox and Action Mailer dependency, you do not need to add them explicitly in your application Gemfile anymore.
  • thor 1.2.1 has been released. You will not see DidYouMean::SPELL_CHECKERS.merge deprecate warnings anymore.

Rails 6.1.Z

  • Use Rails 6.1.5 to support database.yml with aliases and secrets.yml with aliases.

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

@eltercero
eltercero / klass.sublime-snippet
Created July 17, 2015 13:05
Snippet for creating a class with its attributes
<!--
Easily creates something like:
class Object
attr_reader :attr1, :attr2, :attr3
def initialize attr1, attr2, attr3
@attr1 = attr1
@attr2 = attr2
@attr3 = attr3
# config/routes.rb
resources :documents do
scope module: 'documents' do
resources :versions do
post :restore, on: :member
end
resource :lock
end
end
@icyleaf
icyleaf / ar_migrate.rb
Last active October 13, 2023 03:21
ActiveRecord type of integer (tinyint, smallint, mediumint, int, bigint)
# activerecord-3.0.0/lib/active_record/connection_adapters/mysql_adapter.rb
# Maps logical Rails types to MySQL-specific data types.
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
return super unless type.to_s == 'integer'
case limit
when 1; 'tinyint'
when 2; 'smallint'
when 3; 'mediumint'
when nil, 4, 11; 'int(11)' # compatibility with MySQL default
@viruthagiri
viruthagiri / wp_plugin_stub.php
Created February 16, 2012 21:36 — forked from mattwiebe/wp_plugin_stub.php
A good starting place for a static class-based WordPress plugin
<?php
/*
Plugin Name: A Plugin Name
Plugin URI: http://somadesign.ca/
Description: Be descriptive.
Version: 0.1
Author: Soma Design
Author URI: http://somadesign.ca/
License: GPL v2
@saranyan
saranyan / active_merchant.rb
Created December 20, 2011 16:38
PayPal IPN using Active merchant
if Rails.env.production?
PAYPAL_ACCOUNT = 'production@gmail.com'
else
PAYPAL_ACCOUNT = 'development@gmail.com'
ActiveMerchant::Billing::Base.mode = :test
end
@guilleiguaran
guilleiguaran / jruby.txt
Created August 20, 2011 06:29
JRuby: From source to RbEnv (1.7.0dev)
% git pull origin master
% ant dist
% tar zxvf dist/jruby-bin-1.7.0.dev.tar.gz -C ~/.rbenv/versions
% cd ~/.rbenv/versions/jruby-1.7.0.dev/bin
% rm -f *.exe *.dll *.bat jruby.sh
% sed -i "" 's/^#!\/bin\/bash/#!\/usr\/bin\/env bash/' jruby
% ln -fs jruby ruby