Skip to content

Instantly share code, notes, and snippets.

View iamricks's full-sized avatar
🎯
Focusing

Richard Santos iamricks

🎯
Focusing
View GitHub Profile
@iamricks
iamricks / jwkmaker.rb
Created January 25, 2023 22:28
Generate a JSON web key set in Ruby from a .pem cert
require 'openssl'
require 'base64'
require 'json'
# Open certificate file
cert = OpenSSL::X509::Certificate.new(File.read('certificate.pem'))
modulus = cert.public_key.n.to_s(2)
modulus_b64 = Base64.urlsafe_encode64(modulus, padding: false)
@iamricks
iamricks / faliure_rate_calc.rb
Created December 16, 2022 22:50
Endpoint Faliure Rate Calculator
# This script helps calculate the total number of requests given a failure rate
# Example: 1,000,000 requests with a 33% failure rate will result in 492,536 retries
# This means it would take 1,492,536 requests to update 1M objects in an endpoint
# that has a 33% failure rate.
total_retries = 0
requests = 1_000_000
faliure_rate = 0.33
attempt = 1
@iamricks
iamricks / best_bridge.py
Last active July 13, 2022 23:18
best bridge solution
# Write a function, best_bridge, that takes in a grid as an argument.
# The grid contains water (W) and land (L). There are exactly two islands in the grid.
# An island is a vertically or horizontally connected region of land.
# Return the minimum length bridge needed to connect the two islands.
# A bridge does not need to form a straight line.
from collections import deque
def explore_first_island(r, c, grid):
rows = len(grid)
cols = len(grid[0])
@iamricks
iamricks / fib.py
Last active March 11, 2022 19:09
Fast Fib
seen = {}
def fib(n) -> int:
if n <= 2:
return 1
if n not in seen:
seen[n] = fib(n - 1) + fib(n - 2)
return seen[n]
@iamricks
iamricks / amz_sp_api.rb
Created January 11, 2022 15:15
Overide some class methods and attributes on amz-sp-api gem to allow for grantless operations
AmzSpApi::SpApiClient.class_eval do
def request_lwa_access_token
newself = self.dup
newself.config = config.dup
newself.config.host = 'api.amazon.com'
data, status_code, headers = newself.super_call_api(:POST, '/auth/o2/token',
:header_params => {
'Content-Type' => 'application/x-www-form-urlencoded'
@iamricks
iamricks / gist:0d00dbc8b736974fb250d92d101247a9
Last active November 22, 2021 12:47
Simple tracking number class in ruby
# Simple tracking number class just to streamline scraped numbers and user created tracking
class Tracking
include ActiveModel::Model
validates :carrier, presence: true
validates :tracking_number, presence: true
validates :ship_date, presence: true
attr_accessor :carrier
attr_accessor :tracking_number
@iamricks
iamricks / smol_linter.rb
Last active January 19, 2022 17:16
Find syntax errors before pushing to git, good for avoiding a push to Heroku that takes down production ;)
# Find files modified
# Only works before git add
modified_file_paths = %x(git diff --name-only).split("\n")
# Make sure zeitwerk doesn't surprise you
print(%x(bin/rails zeitwerk:check))
# Loop through files and check syntax, print status
modified_file_paths.each do |path|
next if !path.include?('.rb')
@iamricks
iamricks / walmart_multipart_feed_post_request.rb
Created July 29, 2021 20:41
Walmart API multipart feed submission in ruby
# Submits a feed to Walmart
# @param feed_data [Hash] data that will be submited with the feed
# @param type [String] Enum: "item" "RETIRE_ITEM" "MP_ITEM" "MP_WFS_ITEM" "MP_ITEM_MATCH" "MP_MAINTENANCE" "SKU_TEMPLATE_MAP" "SHIPPING_OVERRIDES"
def submit_feed(feed_data, type)
# To add a param to a multipart POST request you need to append the params to the URL
endpoint = "https://marketplace.walmartapis.com/v3/feeds?feedType=" + type
headers = self.api_client.headers.with_indifferent_access
uri = URI(endpoint)