Skip to content

Instantly share code, notes, and snippets.

View krisleech's full-sized avatar

Kris Leech krisleech

View GitHub Profile
@krisleech
krisleech / defaults.md
Last active June 27, 2019 10:59
dry-struct defaults

As a default in the model

class MyModel < Sry::Struct
  attribute :created_at, ::Types::Strict::Time.default(-> _ { Time.now }.freeze)
  # attribute :created_at, ::Types::Strict::Time.default(Proc.new { Time.now }.freeze)
  # attribute :created_at, ::Types::Strict::Time.default(lambda { |_| Time.now }.freeze)
end
@krisleech
krisleech / events.rb
Created June 25, 2019 19:34
Repo / Model with Sequel, SQLite and Dry-Struct #ruby
require 'app'
require 'dry-struct'
require 'types'
class Events
def self.migrate!
return if db.table_exists?(table_name)
db.create_table table_name do
String :project, null: false

Using rsync from cron on server A to pull files from server B via ssh.

Server A has a passwordless private key, the public key is in authorized_keys on Server B.

I can ssh from Server A to Server B.

To prevent use of ForwardAgent specific no configuration file:

ssh -F /dev/null -v admin@servera
@krisleech
krisleech / how_to_copy_files_to_another_server.md
Last active June 19, 2019 08:53
[Ansible] Copying file to another server

Create some files (via templating) on the server

- name: Create backup configuration files
  template:
    src: "{{ item.type }}.yml.j2"
    dest: "/tmp/{{ item.project }}_{{ item.env }}_{{ item.type }}.yml"
  with_items:
    - "{{ backup }}"
@krisleech
krisleech / sort_and_drop.rb
Created June 18, 2019 07:29
sort, drop, take #ruby
[1,2,3,4,5,6,7].drop(2) # => [3,4,5,6,7]
[1,2,3,4,5,6,7].take(2) # => [1,2]
[1,2,3,4,5,6,7].reverse.take(2) # => [5,7]
[1,2,3,4,5,6,7].reverse.drop(2) # => [5, 4, 3, 2, 1]
# sorting asc and desc
@krisleech
krisleech / call_and_curry.rb
Created June 17, 2019 17:05
curry and callables
add = Proc.new { |a,b| a+b }
add.call(1,2) # => 3
add_one = add.curry.call(1)
add_one.call(2) # => 3
# with a method
def subtract(a,b); b-a; end
@krisleech
krisleech / ruby_system_pipes.md
Created June 12, 2019 13:37
Exit code with pipes
system("bash -c 'set -o pipefail; mysqldump #{mysql_auth(configuration)} --force #{configuration[:database][:name]} | gzip -9 > #{tmp_file}'")

pipefail is not avalible with sh only bash.

The exit code will be non-zero if an error occurs with mysqldump, however the pipe will still be processed.

@krisleech
krisleech / app.rb
Created June 12, 2019 10:21
Minimal Rack Application
require 'bundler/setup'
env = ENV.fetch('RACK_ENV', 'production')
Bundler.require(:default, env)
require 'hanami/router'
require 'hanami/controller'
require 'hanami/validations'
require 'haml'
@krisleech
krisleech / s3.rb
Last active June 6, 2019 17:29
Example S3 in Ruby
client = Aws::S3::Client.new(region: 'eu-west-2',
access_key_id: '...',
secret_access_key: '...')
# buckets are top-level, there are no sub-buckets and must be unique
client.create_bucket(bucket: 'acme-test', acl: 'private')
client.delete_bucket(bucket: 'acme-test')
@krisleech
krisleech / main.rb
Created June 3, 2019 10:56
Start of a Ruby Application
require "bundler"
Bundler.setup
require 'pathname'
AppRoot = Pathname.new(__dir__)
require 'yaml'