Skip to content

Instantly share code, notes, and snippets.

View atheiman's full-sized avatar
😬

Austin Heiman atheiman

😬
View GitHub Profile
# this is totally untested...
require 'rest-client'
require 'json'
PAGE_REGEX = /page=(?<page>\d+)/
def get_paginated(base_url, length=30)
base_url += "?per_page=#{length}"
items = []
[chefvm stage_dr] simple-passenger-cookbook $ cat Berksfile
source 'https://supermarket.chef.io'
metadata
cookbook 'apt', '~> 4.0'
cookbook 'build-essential', '= 2.4.0'
[chefvm stage_dr] simple-passenger-cookbook $ rm Berksfile.lock
[chefvm stage_dr] simple-passenger-cookbook $ bundle exec berks install
Resolving cookbook dependencies...
irb(main):001:0> def equals(a, b='B', c='C'); p a; p b; p c; end
=> :equals
irb(main):002:0> def colon(a, b: 'B', c: 'C'); p a; p b; p c; end
=> :colon
irb(main):003:0> equals('A', c='see')
"A"
"see"
"C"
=> "C"
irb(main):004:0> colon('A', c: 'see')
require 'net/http'
require 'rspec'
class ApiClient
def initialize(server_url, username, password)
@server_url, @username, = server_url, username
authenticate(password)
end
def authenticate(password)
@atheiman
atheiman / java_properties.rb
Last active January 29, 2017 01:00
Ruby hash to Java properties file
class Hash
def to_java_properties_hash(prefix='')
properties = {}
self.each do |property, value|
new_prefix = prefix.empty? ? property.to_s : prefix + '.' + property.to_s
if value.respond_to? :to_java_properties_hash
properties.merge!(value.to_java_properties_hash(new_prefix))
else
properties[new_prefix] = value.to_s
output = 'peer count: 6
ip address: 1.2.3.4
status: up
client version: 1.2.3
ip address: 1.2.3.5
status: up
client version: 1.3.0
a = b
a.b = B
c.e = true
1234=1234
@atheiman
atheiman / erb_example.rb
Created April 17, 2017 19:48
simple ERB example
~ $ cat tmp/erb_example.rb
#!/usr/bin/env ruby
require 'erb'
template = "number: <%= @number %>\nstring: <%= @string %>"
@number = 3.4
@string = 'blue'
puts ERB.new(template).result
^\w+(\.\w+)*@(\w+\.)*\w+\.\w+$
# given a list of hashes:
foo = [
{ a: whatever, b: whatever, c: whatever },
{ a: whatever, b: whatever, c: whatever },
# ...
]
bar = { b: whatever, y: whatever, z: whatever }