Skip to content

Instantly share code, notes, and snippets.

View rinaldifonseca's full-sized avatar

Rinaldi Fonseca rinaldifonseca

View GitHub Profile
@rinaldifonseca
rinaldifonseca / project.md
Created July 28, 2024 23:25 — forked from ybur-yug/project.md
a genstage project tutorial

GenStage Tutorial

Introduction

So what is GenStage? From the official documentation, it is a "specification and computational flow for Elixir", but what does that mean to us?
There is a lot to something that can be described as that vague, and here we'll take a dive in and build something on top of it to understand its goals. We could go into the technical and theoretical implications of this, but instead lets try a pragmatic approach to really just get it to work.

First, Let's imagine we have a server that constantly emits numbers. It starts at the state of the number we give it, then counts up in one from there onward. This is what we would call our producer.

@rinaldifonseca
rinaldifonseca / random.md
Created July 2, 2024 01:17 — forked from joepie91/random.md
Secure random values (in Node.js)

Not all random values are created equal - for security-related code, you need a specific kind of random value.

A summary of this article, if you don't want to read the entire thing:

  • Don't use Math.random(). There are extremely few cases where Math.random() is the right answer. Don't use it, unless you've read this entire article, and determined that it's necessary for your case.
  • Don't use crypto.getRandomBytes directly. While it's a CSPRNG, it's easy to bias the result when 'transforming' it, such that the output becomes more predictable.
  • If you want to generate random tokens or API keys: Use uuid, specifically the uuid.v4() method. Avoid node-uuid - it's not the same package, and doesn't produce reliably secure random values.
  • If you want to generate random numbers in a range: Use random-number-csprng.

You should seriously consider reading the entire article, though - it's

@rinaldifonseca
rinaldifonseca / example run
Last active February 24, 2024 02:59 — forked from MachinesAreUs/example run
Demonstration of trapping exits in Elixir
iex(2)> pid = spawn Traps, :init, []
waiting to die...
#PID<0.92.0>
Nobody tried to kill me! ^_^
waiting to die...
Nobody tried to kill me! ^_^
waiting to die...
iex(3)> Process.exit pid, :normal
true
(⊙_⊙') somebody tried to kill me! {:EXIT, #PID<0.88.0>, :normal}
@rinaldifonseca
rinaldifonseca / box.ex
Created February 10, 2024 20:13 — forked from teamon/box.ex
Define elixir structs with typespec with single line of code
defmodule Box do
defmacro __using__(_env) do
quote do
import Box
end
end
@doc """
Define module with struct and typespec, in single line
@rinaldifonseca
rinaldifonseca / README.md
Created January 27, 2021 21:19 — forked from jesster2k10/README.md
JWT Auth + Refresh Tokens in Rails

JWT Auth + Refresh Tokens in Rails

This is just some code I recently used in my development application in order to add token-based authentication for my api-only rails app. The api-client was to be consumed by a mobile application, so I needed an authentication solution that would keep the user logged in indefinetly and the only way to do this was either using refresh tokens or sliding sessions.

I also needed a way to both blacklist and whitelist tokens based on a unique identifier (jti)

Before trying it out DIY, I considered using:

<!DOCTYPE html>
<html>
<head>
<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons' rel="stylesheet">
<link href="https://unpkg.com/vuetify/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
</head>
<body>
<div id="app">
<v-app id="inspire">
require 'logger'
require 'byebug'
require 'dry/monads'
require 'dry/monads/do'
require 'dry/matcher/result_matcher'
module Dry
module Transaction
module Steps
@rinaldifonseca
rinaldifonseca / your_application.rb
Created June 4, 2020 00:11 — forked from nicholasjhenry/your_application.rb
PayRoll application, embedded in Rails, borrowing from Use Case Driven Architecture and DCI
## PayRoll gem
# lib/pay_roll.rb
module PayRoll
class << self
attr_accessor :employee_directory
def config
yield self
end
end
@rinaldifonseca
rinaldifonseca / readme.md
Created February 10, 2020 19:10 — forked from maxivak/readme.md
Integrating Gem/Engine and Main Rails App
@rinaldifonseca
rinaldifonseca / rails_engine_setup.md
Created January 10, 2020 12:35 — forked from livecodelife/rails_engine_setup.md
Rails Engine Setup with RSpec, Capybara, FactoryGirl, and DatabaseCleaner
  1. rails plugin new <project_name> -T -d postgresql --dummy-path=spec/dummy --skip-turbolinks --skip-spring --mountable
  • -T skips the creation of the test directory and the use of Test::Unit
  • --dummy-path=spec/dummy sets up your dummy app in a spec directory
  • --skip-turbolinks & --skip-spring creates a project that does not use turbolinks or spring
  • --mountable creates a "mountable" and namespace-isolated engine.
  1. In Gemfile:
  • gem 'rspec-rails' - user rspec in place of minitest (docs)
  • gem 'capybara' - act as a user navigating your site in tests (docs)
  • gem 'factory_girl_rails' - easily generate database objects without repetitive code in tests (docs)
  • gem 'database_cleaner' - clean your database between tests (docs)