Skip to content

Instantly share code, notes, and snippets.

@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 # => { 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.
@harssh
harssh / ruby_ocr.rb
Created April 27, 2023 02:28 — forked from dshorthouse/ruby_ocr.rb
OCR Image-based PDF in ruby
require 'parallel'
require 'rtesseract'
require 'mini_magick'
source = "/MyDirectory/my.pdf"
doc = {}
pdf = MiniMagick::Image.open(source)
Parallel.map(pdf.pages.each_with_index, in_threads: 8) do |page, idx|
tmpfile = Tempfile.new(['', '.tif'])
MiniMagick::Tool::Convert.new do |convert|
@harssh
harssh / user_observer.rb
Created March 27, 2023 17:45 — forked from jhjguxin/user_observer.rb
cache devise's current_user method, speed up page's load, used for guanxi_cms, Suitable for devise 2.x - 3.x, Rails 3.x
class UserObserver < Mongoid::Observer
def after_save(record)
expire_cache(record)
end
private
def expire_cache(record)
Rails.cache.delete("user:#{record.id}")
end
@harssh
harssh / ruby_post.rb
Created March 25, 2023 01:31
Send POST request in RUBY
Post some JSON
require 'net/http'
require 'uri'
require 'json'
uri = URI.parse("http://localhost:3000/users")
header = {'Content-Type': 'text/json'}
user = {user: {
@harssh
harssh / clean_code.rake
Created February 23, 2023 03:42 — forked from thorstenspringhart/clean_code.rake
extended rake tasks to run rubocop only on uncommitted files / diff with master or entire project
namespace :clean_code do
# rubcop only available in dev
if Rails.env.development?
require 'rubocop/rake_task'
def diff_files
cmd = %q( git diff --name-only --diff-filter=ACMRTUXB \
$(git merge-base HEAD origin/master) \
| egrep '\.rake$|\.rb$' )
diff = `#{cmd}`
diff.split("\n")
@harssh
harssh / git diff comma separated files.sh
Created February 23, 2023 01:17 — forked from marcelblijleven/git diff comma separated files.sh
Comma separated files with git diff
git --no-pager diff --name-only develop | tr '\n' ',' | sed 's/\(.*\),/\1 /'
@harssh
harssh / query.rb
Created February 15, 2023 14:35 — forked from mamantoha/query.rb
PostgreSQL: query to select records from last week on weekdays between 9:00 and 18:00
Model
.where(
"EXTRACT(dow FROM log_in) IN (1,2,3,4,5)"
)
.where(
"log_in::time BETWEEN '9:00' AND '18:00'"
)
.where(
"log_in BETWEEN now()::timestamp - (interval '1 week' AND now()::timestamp)"
)
@harssh
harssh / experience.rb
Created February 15, 2023 03:56 — forked from mamantoha/experience.rb
Rails API Filtering and Sorting
# app/models/experience.rb
#
# == Schema Information
#
# Table name: experiences
#
# id :integer not null, primary key
# title :string
# description :text
# created_at :datetime not null