Skip to content

Instantly share code, notes, and snippets.

View reggieb's full-sized avatar

Rob Nichols reggieb

View GitHub Profile
require 'elasticsearch'
# Set up client bypassing Elasticsearch's Faraday imported proxy settings (which ignore system's no_proxy)
# So transport_options needed to access local elasticsearch instance
client = Elasticsearch::Client.new log: true, transport_options: { proxy: '' }
# Retrieve data - setup instructions here:
# https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-elasticsearch-on-ubuntu-16-04
client.get index: 'tutorial', type: 'helloworld', id: 1
def glyphicon(name)
content_tag(
'span', '', class: "glyphicon glyphicon-#{name}", 'aria-hidden' => 'true'
)
end
@reggieb
reggieb / array_to_h.rb
Created March 13, 2017 12:24
Add a `to_h` method to pre 2.1 ruby
unless [].respond_to? :to_h
class Array
def to_h
Hash[self]
end
end
end
#!/bin/bash
###
# Script for downgrading Elasticsearch to version 1.3.9
#
# Before running this script, please reset your dependency cache in Project Settings > Admin.
#
# Add the line below to your Setup commands in Project Settings (without the # at the beginning):
#
# wget https://gist.githubusercontent.com/reggieb/c16ce698f482904ad0a8f81bd1fe7af4/raw/1fc5a9883ee85f74b6fc8415213deac69b002e55/elasticsearch-downgrade-semaphore.sh ; sudo bash elasticsearch-downgrade-semaphore.sh
@reggieb
reggieb / picking_serializer.rb
Created May 24, 2017 12:46
Playing with ActiveModelSerializers to generate a redux friendly data structure.
class PickingSerializer < ActiveModel::Serializer
attribute :data do
{
result: object.id,
entities: {
orders: { object.id => OrderSerializer.new(object) },
line_items: object.line_items.each_with_object({}) do |line_item, h|
h[line_item.id] = LineItemSerializer.new(line_item)
end
@reggieb
reggieb / rspec_spy_example.rb
Created July 10, 2018 08:51
Using spys in rspec to mock connections to services
# Set up the spy object
let(:chargebee_site) { spy(ChargebeeSite) }
# Hook it into the work flow
before do
allow(ChargebeeSite).to receive(:new).and_return(chargebee_site)
end
# use `have_received` to test connection
it 'creates a chargebee coupon' do
@reggieb
reggieb / application.html.erb
Last active September 20, 2018 10:41
Add generic page titles
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title><%= page_title %></title>
@reggieb
reggieb / identify_applicant.rb
Created November 6, 2018 14:46
Example of service to gather data from TrueLayer with token
module TrueLayer
class IdentifyApplicant
TRUE_LAYER_URL = 'https://api.truelayer.com'.freeze
attr_reader :token, :error
def initialize(token)
@token = token
end
@reggieb
reggieb / salary_pattern_generator.rb
Created June 4, 2019 13:20
Salaray Pattern Generator: Tool for creating sets of data for salaries paid in different patterns (weekly, two weekly, four weekly and monthly)
require 'active_support'
require 'active_support/core_ext/integer/time'
class SalaryPatternGenerator
def self.generate(*args)
new(*args).generate
end
attr_reader :period, :salary, :day_offset, :salary_offset, :start_at
@reggieb
reggieb / x_builder.rb
Last active October 11, 2019 09:48
XBuilder - A simple tool to build complicated XML objects from nested components
require "active_support/core_ext"
class XBuilder
attr_reader :data, :namespace, :root
def initialize(data)
@namespace = data.delete(:_namespace)
@root = data.delete(:_root)
@data = data
end