Skip to content

Instantly share code, notes, and snippets.

View nathan-appere's full-sized avatar

Nathan Appere nathan-appere

View GitHub Profile
@nathan-appere
nathan-appere / dry-types.rb
Created September 11, 2019 12:19
dry-types test
require 'dry-types'
require 'pry'
module Types
include Dry.Types
end
ProtocolType = Types::Symbol.enum(:http, :async, :websockets)
HttpVerb = Types::Symbol.enum(:get, :post)
@nathan-appere
nathan-appere / sig.rb
Created October 1, 2019 15:18 — forked from havenwood/sig.rb
An example for Nathanael7 on #ruby IRC
def sig(method_name)
meth = method(method_name)
parameters = meth.parameters.map do |type, name|
case type
when :req
name
when :rest
"*#{name}"
when :opt
# Unknown default argument.
@nathan-appere
nathan-appere / kit-contracts.rb
Last active October 11, 2019 10:48
Example of ruby contracts
module RGB
# First step -----------------------------------------------------------------
before ->(r:, g:, b:) { (0..255) === r && (0..255) === g && (0..255) === b }
after [
->(result:) { result.is_a?(String) },
->(result:) { result.start_with?('#') },
->(result:) { result.length.in?([4, 7]) },
]
module Test
module RGB
include Kit::Contract
before ->(r:, g:, b:) { (0..255) === r && (0..255) === g && (0..255) === b }
after [
->(result:) { result.is_a?(String) },
->(result:) { result.start_with?('#') },
->(result:) { result.length.in?([4, 7]) },
]
config_default: &config_default
adapter: <%= ENV['DATABASE_ADAPTER'] || 'postgresql' %>
encoding: utf8
pool: <%= (ENV['DATABASE_POOL'] || ENV['MAX_THREADS'] || 5) %>
# Readonly mode | expected privileges: [CONNECT, SELECT]
config_default_readonly: &config_default_readonly
<<: *config_default
url: <%= ENV['DATABASE_URL_READONLY'] %>
@nathan-appere
nathan-appere / routes.txt
Last active February 13, 2020 08:28
Routing & URL
RAILS WAY FOR REFERENCE -------------------------------------------------------
@note This was designed "web client" first, not really good for APIs.
HTTP Verb Path Action Used for
GET /photos photos#index display a list of all photos
GET /photos/new photos#new return an HTML form for creating a new photo
POST /photos photos#create create a new photo
GET /photos/:id photos#show display a specific photo
GET /photos/:id/edit photos#edit return an HTML form for editing a photo
@nathan-appere
nathan-appere / copy-checklist.js
Last active May 8, 2020 07:18 — forked from niallsmart/copy-checklist.js
Copy Trello checklist to clipboard
copy($(".checklist-item:not(.checklist-item-checked)").map(function() {
var $el = $(this),
item = $el.find(".checklist-item-details-text").text();
if ($el.hasClass("checklist-item-state-complete")) {
item = "- [x] " + item;
} else {
item = "- [ ] " + item;
}
return item;
}).get().join("\n"));
@nathan-appere
nathan-appere / analytics.rb
Last active February 2, 2022 16:51
Quick segment wrapper for Rails
# Wrapper for Segment.
module Services::Analytics
PREVENT_EXCEPTION = true
def self.backend_default
key = ENV['SEGMENT_SOURCE_SERVER_KEY']
if Rails.env.production? || (key && !key.blank?)
@backend_default ||= Segment::Analytics.new(
@nathan-appere
nathan-appere / rspec_deterministic.rb
Created February 6, 2022 10:41
RSpec helper for deterministic specs
require 'factory_bot_rails'
RSpec.configure do |config|
config.before do
Random.srand(config.seed)
Faker::Config.random = Random.new(config.seed)
allow(SecureRandom).to(receive(:uuid).and_wrap_original { |*| Faker::Internet.uuid })
end
@nathan-appere
nathan-appere / clone-heroku-app
Created July 5, 2023 08:27 — forked from pcreux/clone-heroku-app
Script to clone a heroku app (including buildpacks, users, add-ons, environment variables, lab features)
#!/usr/bin/env ruby
# Copy a heroku app (buildpacks, add-ons, labs, config, users).
# This script is idempotent so it can run against an existing app.
#
# Usage:
# $> clone-heroku-app source-app target-app
require 'json'