Skip to content

Instantly share code, notes, and snippets.

module Devise
module Models
# Registerable is responsible for everything related to registering a new
# resource (ie user sign up).
module Registerable
extend ActiveSupport::Concern
module ClassMethods
# A convenience method that receives both parameters and session to
# initialize a user. This can be used by OAuth, for example, to send
@harssh
harssh / warning.rb
Created February 28, 2024 20:40
warning.rb
# Define a "warnings" validation bucket on ActiveRecord objects.
#
# @example
#
# class MyObject < ActiveRecord::Base
# warning do |vehicle_asset|
# unless vehicle_asset.description == 'bob'
# vehicle_asset.warnings.add(:description, "should be 'bob'")
# end
# end
# requires :password_reset_token and :password_reset_token_expires_at fields
module PasswordResetable
extend ActiveSupport::Concern
included do
before_validation do
if password_reset_token_expires_at && password_reset_expires_at.past?
expire_password_reset!
end
end
@harssh
harssh / README.md
Created September 26, 2023 02:58 — forked from noelbundick/README.md
Optimizing Rust container builds

Optimizing Rust container builds

I'm a Rust newbie, and one of the things that I've found frustrating is that the default docker build experience is extremely slow. As it downloads crates, then dependencies, then finally my app - I often get distracted, start doing something else, then come back several minutes later and forget what I was doing

Recently, I had the idea to make it a little better by combining multistage builds with some of the amazing features from BuildKit. Specifically, cache mounts, which let a build container cache directories for compilers & package managers. Here's a quick annotated before & after from a real app I encountered.

Before

This is a standard enough multistage Dockerfile. Nothing seemingly terrible or great here - just a normal build stage, and a smaller runtime stage.

@harssh
harssh / rust_api_certificate.rs
Created August 30, 2023 13:55
Add certificate and key in Rust API request
//https://www.reddit.com/r/learnrust/comments/y5qm0y/certificates_in_reqwest_crate/
let mut headers = HeaderMap::new();
headers.insert(ACCEPT, "application/json".parse().unwrap());
headers.insert("X-Application", "someKey".parse().unwrap());
// Format body
let login_params = [("username", "myusername"), ("password", "password")];
// create a certificate identity
let mut buf = Vec::new();
@harssh
harssh / ractors.rb
Created August 26, 2023 19:54 — forked from Kukunin/ractors.rb
Ruby Ractors vs Threads benchmark
require 'benchmark'
require 'etc'
Ractor.new { :warmup } if defined?(Ractor)
def fibonacci(n)
return n if (0..1).include? n
fibonacci(n - 1) + fibonacci(n - 2)
end
@harssh
harssh / iam_fog.rb
Created August 17, 2023 18:40 — forked from zapnap/iam_fog.rb
Using Amazon IAM with Fog (example)
require 'fog'
username = 'testuser'
bucket = 'uniquebucketname1234'
aws_credentials = {
:aws_access_key_id => 'YOUR-ACCESS-KEY-ID',
:aws_secret_access_key => 'YOUR-SECRET-ACCESS-KEY'
}
@harssh
harssh / aws.rb
Created August 17, 2023 18:11 — forked from peterwells/aws.rb
This Gist shows how I was able to implement AWS assume role functionality within my application using the Ruby AWS SDK, Fog, and Carrierwave. Currently Fog has no concept of one role assuming another. They do, however, have an existing mechanism for re-negotiating soon-to-expire credentials. By monkey-patching this function, we can leverage the …
#resides in app/models/connectors/
class Connectors::Aws
attr_reader :aws_access_key_id, :aws_secret_access_key, :aws_security_token, :expires_at
def initialize
if ENV.has_key?('AWS_SECURITY_TOKEN') #localhost
@aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
@aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
@aws_security_token = ENV['AWS_SECURITY_TOKEN']
@expires_at = Time.now + 10.hours
@harssh
harssh / hash_except.md
Created August 2, 2023 17:31 — forked from O-I/hash_except.md
[TIx 4] Remove key-value pairs from a hash and return the hash

Note: As of Ruby 3.0.0, Hash#except is now [part][1] of the language. However, Ruby does not implement Hash#except!.

Sometimes I want to remove a specific key-value pair from a Ruby hash and get the resulting hash back. If you're using Rails or ActiveSupport you can accomplish this using Hash#except:

hash = { a: 1, b: 2, c: 3 }
hash.except(:a)     # => { b: 2, c: 3 }

# note, the original hash is not modified
hash # =&gt; { a: 1, b: 2, c: 3 }
@harssh
harssh / build_aws_lambda_static_and_dynamic_linking.rs
Created May 9, 2023 23:52
How to Build a Simple App and Deploy It to AWS Lambda static and dynamic linking
adevait.com
How to Build a Simple App and Deploy It to AWS Lambda | Adeva
by Chuma Umenze13 min read·
15–19 minutes
AWS Lambda is a serverless computing platform that lets you run code without provisioning or managing servers. The platform invokes your code in response to events such as HTTP requests. Currently, there's no official support for Rust programming language on Lambda.
To run Rust code on Lambda, we will have to build a custom runtime that will run our code.