Skip to content

Instantly share code, notes, and snippets.

View j4zzcat's full-sized avatar

Sharon Dagan j4zzcat

View GitHub Profile
@j4zzcat
j4zzcat / rack-rest-api-example.rb
Created January 2, 2017 11:24 — forked from peter/rack-rest-api-example.rb
An example of how to write a simple JSON REST API with a simplistic router directly on top of Rack (i.e. without the use of a framework like Sinatra).
#################################################################
#
# File: lib/api.rb
#
#################################################################
require 'user' # ActiveRecord model
# Connect to the db
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'])
load GEMSPEC_FILE
ObjectSpace.each_object Gem::Specification do | instance |
if instance.name == GEMSPEC_FILE
gemspec = instance
break
end
end
require 'rake'
rakefile = 'some-rake-file'
rake = Rake.application
rake.init
rake.add_import rakefile
rake.load_rakefile
rake[ :taskname ].invoke
TOPLEVEL_BINDING.eval( 'def task( *args, &block ); end' )
load 'Rakefile'
#! /bin/bash
# Create a temp file
program=$(mktemp /tmp/XXXX)
# Get the line number of the last match of __PROGRAM_START__
# Just grep-ing the file to find the marker will match the grep itself
cut_pos=$(cat ${0} | grep -n '__PROGRAM_START__' | tail -1 | awk -F ':' '{print $1}')
# Get the docker image tag to run the program in
file utils.rb:
--------------
require 'webrick'
module Utils
WEBrick::HTTPStatus::StatusMessage.each do | k, v |
self.const_set "HTTP_#{k}_#{v.upcase.tr ' -', '_'}".to_sym, k
end
end
r = %r{^/user/(?<user_id>.*)/login}
r.match '/user/1234/login' # => #<MatchData "/user/1234/login" user_id:"1234">
@j4zzcat
j4zzcat / pixie.sh
Created December 25, 2019 21:28
Dockerise
#! /usr/bin/env bash
PROGRAM_HOME=$(realpath ${0} 2>/dev/null || cd "$(dirname "${0}")" ; pwd -P)/..
RUNTIME_TAG=pixie-runtime:1.0
function run_script {
CUT_LINE=$(cat ${0} | grep -n '#! dockerise ' | tail -1 | awk -F ':' '{print $1}')
tail +${CUT_LINE} ${0} | tail -n +2 | \
docker run --rm -i \
-v /var/run/docker.sock:/var/run/docker.sock \
@j4zzcat
j4zzcat / pixie
Last active January 19, 2020 20:49
Dockerise a Ruby Gem
#!/usr/bin/env bash
GEM_NAME='pixie'
#
# Generic stuff
#
function fail {
local MESSAGE=${1}
@j4zzcat
j4zzcat / squigly_heredoc.rb
Created January 28, 2020 08:38
Remove white space from HEREDOC
class Subscription
def warning_message
<<~HEREDOC
Subscription expiring soon!
Your free trial will expire in #{days_until_expiration} days.
Please update your billing information.
HEREDOC
end
end