Skip to content

Instantly share code, notes, and snippets.

@kadru
kadru / compare_request_methods.rb
Last active May 25, 2026 03:25 — forked from keithrbennett/compare_request_methods.rb
Illustrates getting responses from multiple HTTPS resources in Ruby using conventional, thread, and fiber approaches.
#!/usr/bin/env ruby
# frozen_string_literal: true
# IMPORTANT: You may need to increase the number of available file handles available to this process.
# The number should be greater than the maximum number of requests you want to make, because each process
# opens at least 3 file handles (stdin, stdout, stderr), and other files may be opened during the program's
# run (e.g. for the logger).
# ulimit -n 300 && scripts/compare_request_methods.rb
require 'async/http/internet' # gem install async-http if necessary
@kadru
kadru / read_pdf.ts
Last active March 7, 2026 15:20
opencode tool for readpdfs
import { tool } from "@opencode-ai/plugin"
import { existsSync } from "fs"
export default tool({
description: "Extract and read text content from PDF files",
args: {
filePath: tool.schema.string().describe("Path to the PDF file"),
offset: tool.schema.number().optional().describe("Page number to start reading from"),
limit: tool.schema.number().optional().describe("Maximum number of pages to read"),
},
@kadru
kadru / agreggation.rb
Last active April 10, 2025 20:57
inheritance example
class Driver
attr_reader :name
def initialize(name)
@name = name
end
end
class Car
def initialize(model:, driver:)
# frozen_string_literal: true
# Runs with
# ruby 3.3
# webmock 3.23
# minitest 5.24.1
require 'uri'
require 'net/http'
require 'json'
require 'webmock/minitest'
@kadru
kadru / lvh_ssl.md
Created September 6, 2019 15:43 — forked from dagjaneiro/lvh_ssl.md
lvh.me ssl

Install nginx

$ brew install nginx

Edit nginx.conf

$ vim /usr/local/etc/nginx/nginx.conf
@kadru
kadru / order-rails-controller-callbacks.md
Created April 6, 2019 01:52 — forked from WaKeMaTTa/order-rails-controller-callbacks.md
Order of Rails Controller Callbacks

Order of Rails Controller Callbacks

Rails 4.x

Started GET "/" for 127.0.0.1 at 2017-05-19 14:17:18 +0200
  Processing by WelcomeController#index as HTML
    prepend_around_action
    prepend_before_action
 before_action
require 'net/ldap'
module Ldap
LDAP_SERVER_IP = 'buzon.uach.mx'.freeze
#LDAP_USERNAME = 'ldap_username'
#LDAP_PASSWORD = 'ldap_password'
#Obtiene el nombre mediante su cuenta de usuario
@kadru
kadru / ror_date_time.rb
Last active January 21, 2019 04:01
Ruby on Rails Dates and Times cheat sheet credits to https://www.varvet.com/blog/working-with-time-zones-in-ruby-on-rails
#the do´s
2.hours.ago # => Thu, 27 Aug 2015 14:39:36 AFT +04:30
1.day.from_now # => Fri, 28 Aug 2015 16:39:36 AFT +04:30
Time.zone.parse("2015-08-27T12:09:36Z") # => Thu, 27 Aug 2015 16:39:36 AFT +04:30
Time.current # => Thu, 27 Aug 2015 16:39:36 AFT +04:30
Time.current.utc.iso8601 # When supliyng an API ("2015-08-27T12:09:36Z")
Time.strptime("2015-08-27T12:09:36Z", "%Y-%m-%dT%H:%M:%S%z").in_time_zone # If you can’t use Time.zone.parse (Thu, 27 Aug 2015 16:39:36 AFT +04:30)
Date.current # If you really can’t have a Time or DateTime for some reason (Thu, 27 Aug 2015)
Date.current.in_time_zone # If you have a date and want to make the best out of it (Thu, 27 Aug 2015 00:00:00 AFT +04:30)
#the don´ts
@kadru
kadru / db_rake.sh
Created December 18, 2018 15:59
All Rails db Rake Tasks
db:create #Creates the database for the current RAILS_ENV environment. If RAILS_ENV is not specified it defaults to the development and test databases.
db:create:all #Creates the database for all environments.
db:drop #Drops the database for the current RAILS_ENV environment. If RAILS_ENV is not specified it defaults to the development and test databases.
db:drop:all #Drops the database for all environments.
db:migrate #Runs migrations for the current environment that have not run yet. By default it will run migrations only in the development environment.
db:migrate:redo #Runs db:migrate:down and db:migrate:up or db:migrate:rollback and db:migrate:up depending on the specified migration. I usually run this after creating and running a new migration to ensure the migration is reversable.
db:migrate:up #Runs the up for the given migration VERSION.
@kadru
kadru / transactions.markdown
Created December 16, 2018 04:32 — forked from jcasimir/transactions.markdown
Transactions

Transactions

As your business logic gets complex you may need to implement transactions. The classic example is a bank funds transfer from account A to account B. If the withdrawal from account A fails then the deposit to account B should either never take place or be rolled back.

Basics

All the complexity is handled by ActiveRecord::Transactions. Any model class or instance has a method named .transaction. When called and passed a block, that block will be executed inside a database transaction. If there's an exception raised, the transaction will automatically be rolled back.

Example