Skip to content

Instantly share code, notes, and snippets.

Jamie Gaskins jgaskins

Block or report user

Report or block jgaskins

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@jgaskins
jgaskins / fetch.cypher
Created Nov 17, 2019
Fetching a remote user
View fetch.cypher
// The $url is passed in as the only parameter to this query. The query
// handles everything else.
CALL apoc.load.jsonParams($url, {Accept: 'application/json'}, null, null, {}) YIELD value AS data
// Upsert the person into the DB, using the :Person label in case they aren't
// a fully-fledged account yet
MERGE (person:Person { id: data.id })
ON CREATE SET
person.created_at = datetime()
@jgaskins
jgaskins / set_operations.rb
Created Oct 13, 2019
SQL result set operations for ActiveRecord
View set_operations.rb
module SetOperations
def union_scope(*scopes)
apply_operation :UNION, scopes
end
def intersect_scope(*scopes)
apply_operation :INTERSECT, scopes
end
def except_scope(*scopes)
View cluster.yaml
---
kind: Service
apiVersion: v1
metadata:
name: main-app
annotations:
service.beta.kubernetes.io/do-loadbalancer-protocol: "http"
service.beta.kubernetes.io/do-loadbalancer-algorithm: "least_connections"
service.beta.kubernetes.io/do-loadbalancer-tls-ports: "443"
service.beta.kubernetes.io/do-loadbalancer-certificate-id: "YOUR_CERT_ID_GOES_HERE"
@jgaskins
jgaskins / server.cr
Last active Sep 4, 2019
Serve a sample JSON payload in Crystal
View server.cr
require "http"
require "uuid"
require "uuid/json"
class App
include HTTP::Handler
def call(context)
Fiber.yield # Simulate getting data from the DB
response_payload.to_json context.response
@jgaskins
jgaskins / stream_each.rb
Last active Aug 22, 2019
Stream results from the database into Ruby objects
View stream_each.rb
require 'ostruct'
Post = Comment = User = OpenStruct
class PostsWithComments
def call
results = Model.connection.execute <<~SQL
SELECT
posts.id,
posts.title,
@jgaskins
jgaskins / app.cr
Created Aug 10, 2019
Roda-like web framework in Crystal
View app.cr
class App
# Provides the `call(context : HTTP::Server::Context)` interface
include HTTP::Handler
# Provides the `route` method that we pass the context to to be
# able to use routing and response data
include Route
def call(context : HTTP::Server::Context)
route context do |r, response, session|
@jgaskins
jgaskins / connection.cr
Last active Jun 23, 2019
Neo4j::Bolt::Connection#exec_cast with a block
View connection.cr
def exec_cast(query : String, parameters : Map, types : Tuple(*TYPES), &block) forall TYPES
send Commands::Run, query, parameters
send Commands::PullAll
result = read_raw_result
until result[1] != 0x71
# First 3 bytes are Structure, Record, and List
# TODO: If the RETURN clause in the query has more than 16 items,
# this will break because the List byte marker and its size won't be
# in a single byte. We'll need to detect this here.
@jgaskins
jgaskins / call_metrics.rb
Last active Mar 24, 2019
Trace metrics for methods defined in Ruby code
View call_metrics.rb
require 'pp'
require 'set'
module CountThings
COUNTS = Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = 0 } }
TIMINGS = Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = [] } }
TRACED_METHODS = Hash.new { |h, k| h[k] = Set.new }
module_function
@jgaskins
jgaskins / hooks.rb
Last active Feb 16, 2019
Hooks implementation for Clearwater
View hooks.rb
module Hooks
def self.component *attrs, &render
Class.new(Component) do
attr_reader *attrs
define_method :initialize do |**props|
super()
attrs.each { |attr| `self[#{attr}] = #{props[attr]}` }
end
@jgaskins
jgaskins / sort-videos.js
Created Dec 11, 2018
Sort Confreaks event videos by number of views
View sort-videos.js
[...document.querySelectorAll('.video-info')]
.sort((a, b) => (
a.querySelector('.video-views strong').innerText|0) >= (b.querySelector('.video-views strong').textContent|0)
? -1
: 1
).map(v => v.querySelector('.video-title').innerText + ' by ' + v.querySelector('.video-presenters').innerText)
You can’t perform that action at this time.