Skip to content

Instantly share code, notes, and snippets.

@gkop
gkop / debug_json.rb
Last active August 29, 2015 13:57
Debug json API response with object in pry
#!/usr/bin/env ruby
# Usage:
# $ curl http://date.jsontest.com | ./debug_json.rb
r = $stdin.dup
input = r.read
$stdin.reopen("/dev/tty")
require 'json'
require 'recursive-open-struct'
require 'awesome_print'
require "net/http"
require "uri"
module Net
class HTTP
alias old_initialize initialize
def initialize(*args)
old_initialize(*args)
@open_timeout = @read_timeout = 1 # 1 second
class Node
attr_accessor :value, :next_node, :previous_node
def initialize(value, next_node, previous_node)
@value = value
@next_node = next_node
@previous_node = previous_node
end
end
class Node
attr_accessor :value, :next_node, :previous_node
def initialize(value, next_node, previous_node)
@value = value
@next_node = next_node
@previous_node = previous_node
end
end
Given the services are running # features/step_definitions/fig_steps.rb:3
Running: fig build && (fig up -d || true) && sleep 10
Process exited successfully
nginx version: nginx/1.7.9
And I run "nginx -v" on the "web" service # features/step_definitions/fig_steps.rb:19
Running: fig run -T web bash -c "sleep 1; nginx -v"
Process exited successfully
Then I should see "nginx" # features/step_definitions/cli_steps.rb:1
And I should see "1.7" # features/step_definitions/cli_steps.rb:1
@gkop
gkop / population_spec.rb
Created March 17, 2015 17:23
Spec for Population mixin that counts instances of class and its subclasses
class Animal
include Population
end
class Kangaroo < Animal
end
class CaptainKangaroo < Kangaroo
end
@gkop
gkop / pattern_match_on_struct_type.exs
Created April 1, 2015 23:34
Pattern match on struct type
defmodule Bar do
defstruct herp: "Derp"
end
defmodule Baz do
defstruct herp: "Derp"
end
defmodule Foo do
@gkop
gkop / seeds_spec.rb
Created June 11, 2015 19:57
seeds_spec.rb
require 'rails_helper'
describe "seeds.rb script" do
def count_all_tables
ActiveRecord::Base.descendants
.sort_by(&:to_s).map(&:count)
end
it "is idempotent" do
load Rails.root.join("db", "seeds.rb")
# inspired by http://faxon.org/2015/02/03/edit-rails-activerecord-json-attributes-in-html-forms
# NOTE requires `custom_fields` column!
#
# class Foo
# include HasCustomFieldsBackedByJSON
# custom_fields_keys :bar, :baz
# end
module HasCustomFieldsBackedByJSON
extend ActiveSupport::Concern
$ ruby click.rb
/home/test/.bundle/ruby/1.8/gems/selenium-webdriver-0.0.27/lib/selenium/webdrive
r/ie/bridge.rb:594:in `check_error_code': unable to find element by xpath using
"//input[@id='signInSubmit']" (17) (Selenium::WebDriver::Error::UnexpectedJavasc
riptError)
from /home/test/.bundle/ruby/1.8/gems/selenium-webdriver-0.0.27/lib/sele
nium/webdriver/ie/bridge.rb:315:in `findElementByXpath'
from /home/test/.bundle/ruby/1.8/gems/selenium-webdriver-0.0.27/lib/sele
nium/webdriver/ie/util.rb:48:in `create_element'
from /home/test/.bundle/ruby/1.8/gems/selenium-webdriver-0.0.27/lib/sele