Skip to content

Instantly share code, notes, and snippets.

View krisleech's full-sized avatar

Kris Leech krisleech

View GitHub Profile
@krisleech
krisleech / SECURE_GEM_DEV.md
Created August 29, 2019 09:20
Best practices for making gem development secure

Sign Commits

You can use your existing GPG key, or generate a new one.

Make sure you have a backup of the private key.

Publish the public key to the internet so it can be downloaded. This could be your own website, a gist, keybase.io or a GPG server.

Link to the URL of the public key in the README.

@krisleech
krisleech / building_a_string.md
Last active August 28, 2019 10:06
building a string

tap

'a'.tap { |it| it << 'b' } # => 'ab'

We can't use += as this is assignment and creates a new object which is not returned:

'a'.tap { |it| it += 'b' } # =&gt; 'a'
@krisleech
krisleech / HOWTO.md
Last active August 14, 2019 10:30
Fixing boot disk on AWS
  • Detach boot disk from problematic instance
  • Create new instance (on free tier)
  • Attach boot disk to instance
  • ssh in to new instance and mount disk
lsblk
sudo mkdir /vol_1
@krisleech
krisleech / HOWTO.md
Last active August 6, 2019 11:55
Installing an old version of ElasticSearch 1.7 and Java 8

Install Java 8

To install without clobbering your existing version use sdkman.

export SDKMAN_DIR="$HOME/bin/sdkman"
curl -s "https://get.sdkman.io" | bash
exec $SHELL
@krisleech
krisleech / in_memory_has_many.md
Created July 31, 2019 15:24
Hardcoded has_many in ActiveRecord
add_column :recipients, :rejection_cause_id, :string
class Recipient < ActiveRecord::Base
  def rejection_cause
    RejectionCauses.find(rejection_cause_id)
  end
end
@krisleech
krisleech / define_method versus def.md
Created July 18, 2019 09:36
Which is easier to read / maintain?

I need some methods to return true/false if a attribute had a value. So we have a date attribute #invited_on and I want an #invited? method to return true if the date is present.

There are a few dates for which I need this. My first attempt:

%w(invited_on selected_on confirmed_on ready).each do |attr|
  define_method attr.gsub('_on', '') + '?' do
    send(attr).present?
  end
end
@krisleech
krisleech / event_bus.rb
Last active July 16, 2019 14:49
Simple Event Bus based on Wisper
class EventBus
def initialize
@publisher = Class.new do
include Wisper::Publisher
public :broadcast
end.new
end
def subscribe(listener, options = {})
@publisher.subscribe(listener, options)
@krisleech
krisleech / HOWTO.md
Last active July 10, 2019 10:09
in-memory belongs_to model

model

class Study < ActiveRecord::Base
  def site
    @site ||= PrefixedSites.find(site_identifier)
  end

  def site=(new_site)
    @site = new_site
@krisleech
krisleech / HOWTO.md
Created July 2, 2019 17:43
tar and gzip

Size of directory:

du -h --summarize /archive/production/files/document_store/2019.07.02.11.58.28

92GB

Archive and compress:

@krisleech
krisleech / rocketchat_webhook_script.js
Last active July 4, 2019 12:44
Sentry and Rocketchat
class Script {
process_incoming_request({ request }) {
if(request.content.event.request == undefined) { url = 'n/a' } else { url = request.content.event.request.url }
return {
content:{
text: "[" + request.content.message + "](" + request.content.url + ")",
attachments: [
{
collapsed: false,