Skip to content

Instantly share code, notes, and snippets.

@JohnMurray
Created May 31, 2012 15:41
Show Gist options
  • Save JohnMurray/2844251 to your computer and use it in GitHub Desktop.
Save JohnMurray/2844251 to your computer and use it in GitHub Desktop.
An example console from my geofence-server-example project
#!/usr/bin/env ruby
# require all the necessary files
$: << ::File.expand_path('../src', __FILE__)
require 'app'
require 'geofence'
# at this point bundler should be setup and the :development group
# should have been included. However, just to be sure, I'm going to
# include bundler again and require the console group.
require 'bundler/setup'
Bundler.require(:console)
# specify some sample fences to play with
#
# format:
# [
# [:lon, :lat],
# ...
# ]
fence_1 = [
[0, 0],
[3, 0],
[4, 2],
[6, 3],
[6, 6],
[5, 7],
[3, 5],
[2, 4],
[0, 1]
]
fence_2 = [
[5, 5],
[7, 5],
[3, 2],
[10, 2],
[12, 7],
[12, 10],
[15, 10],
[15, 15],
[12, 15],
[10, 18],
[8, 15],
[7, 15],
[6, 13],
[5, 10]
]
# patch pretty-print to use a smaller width for looking at our fences
class PP
class << self
alias_method :old_pp, :pp
def pp(obj, out = $>, width = 40)
old_pp(obj, out, width)
end
end
end
# include helpers to play around with Sinatra application
include Rack::Test::Methods
self.instance_eval do
@app = App.new
def app; @app; end
end
# add ability to reload console
def reload
reload_msg = '# Reloading the console...'
puts CodeRay.scan(reload_msg, :ruby).term
Pry.save_history
exec('./console')
end
g = Class.new {
def method_missing(m, *args, &block)
Geofence.send(m, *args, &block)
end
}.new
# start the console! :-)
system('clear')
welcome = <<eos
# This is my interactive playground. You can call the Sinatra methods in
# the same way you would in RSpec (get, post). There are also a couple of
# test fences defined for you (fence_1, fence_2). I also monkey-patched
# 'pp' to print short arrays nicer.
#
# Everything you would want to play with (for this project) should be in here
#
eos
puts CodeRay.scan(welcome, :ruby).term
Pry.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment