Skip to content

Instantly share code, notes, and snippets.

@sephraim
sephraim / read_simlink.sh
Created January 15, 2023 18:48
[Read symlink location]
@sephraim
sephraim / docker-compose.sql
Created January 15, 2023 18:44
[PostgreSQL, Adminer, and Docker Compose]
TODO
@sephraim
sephraim / httparty_examples.rb
Last active December 13, 2022 06:52
[HTTParty examples]
# GET
HTTParty.get('www.example.com')
# POST
HTTParty.post(
'www.example.com',
headers: { 'Content-Type' => 'application/json' },
body: {
title: 'The Catcher in the Rye',
author: 'J. D. Salinger',
@sephraim
sephraim / stub_env.rb
Last active November 14, 2022 21:02
[Stub ENV variables] Stub environment variables in RSpec tests
before(:example) do
stub_const('ENV', ENV.to_hash.merge('FOO' => 'bar', 'BAZ' => 'blah'))
end
@sephraim
sephraim / install-wait-for-it.Dockerfile
Created October 24, 2022 21:45
[Install wait-for-it in Docker image]
RUN apt-get update -qq && \
apt-get install -y --no-install-recommends wait-for-it && \
rm -rf /var/lib/apt/lists/*
@sephraim
sephraim / anon_class.rb
Created October 17, 2022 22:41
[Create an anonymous class]
# Initialize
#
# @return [void]
def initialize
# Setup a simple logger for running locally (if full $logger not available)
$logger ||= Class.new do
# Print info message
#
# @param msg [String] Message to print
# @return [void]
@sephraim
sephraim / docker-compose-run.sh
Last active October 29, 2022 20:14
[Quickly run command inside Docker container] Create a temporary container from your docker-compose.yml setup and run a command in it. #docker
# Example 1
docker compose run --entrypoint 'bash -c' --rm web 'bundle exec rails {db:create,db:migrate}'
# Example 2
docker compose run --no-deps --rm web bundle install && docker compose build
# Example 3
docker compose run --no-deps --rm -e GEM_HOME=/home/ruby/gems <SERVICE_NAME> bundle install
@sephraim
sephraim / elasticsearch.rb
Created October 13, 2022 20:18
[Ruby Elasticsearch client examples]
# GET shards
num_shards_in_use = client.cluster.stats['indices']['shards']['total']
# GET docs
response = client.search(index: 'index_name', q: '!2022-10-11 AND !2022-10-12 AND !2022-10-13', df: 'date', _source_includes: ['name', 'date'])
docs = response['hits']['hits']
num_docs = response['hits']['total']['value']
# GET indices
all_indices = client.indices.stats['indices'].map(&:first)
@sephraim
sephraim / try_block.rb
Last active October 13, 2022 07:50
[Try blocks in Ruby] How to use a begin...rescue...else...ensure block for exception handling
# Max number of retries if error occurs while pruning index
MAX_NUM_RETRIES = 3
# Number of seconds to wait between retries
RETRY_TIMEOUT_SECONDS = 5
begin
retries ||= 0
# Do something
rescue StandardError => e
@sephraim
sephraim / get_web_page.rb
Last active October 4, 2022 19:36
[Get web page source] Get the contents of a web page using Ruby's built-in open-uri library
require 'open-uri'
url = 'https://www.google.com/'
file = URI.open(url)
contents = file.read
puts contents