Replies: 9 comments
-
#!/usr/bin/env ruby
# ==============================================================================
# obe - "Old Blue Eyes", a minimal and modern Sinatra console within IRB
#
# See: http://bit.ly/3IB4VVf for the reference...
#
# Author: Steve Shreeve (steve.shreeve@gmail.com)
# Date: Jan 12, 2023
# ==============================================================================
STDOUT.sync = true
require 'bundler/setup'
require 'irb'
require 'rack/test'
class Obe
attr_reader :app, :env
def initialize(file=nil)
file ||= ENV['RACK_CONFIG'] || 'config.ru'
@app = Rack::Builder.parse_file(file).first
@env = ENV['APP_ENV'] ||= ENV['RACK_ENV'] || 'development'
end
def self.instance
@instance ||= self.new
end
end
def app
@app ||= begin
app = Obe.instance.app.new!
app.app = app
app.extend Rack::Test::Methods
app.request = Sinatra::Request.new({})
app.response = Sinatra::Response.new
app
end
end
def env
Obe.instance.env
end
def display(list)
wide = list.map {|e| e[0].length }.max
list.each {|a, b| puts "%-*s %s" % [wide, a, b.inspect] }
nil
end
def routes
list = app.class.routes.except 'HEAD'
list = list.inject([]) do |list, (meth, hits)|
list += hits.map {|vals| [meth.downcase, vals[0].to_s] }
end
list = list.sort_by {|a, b| [b, a]}
display list
end
def props
skip = %w[ inline_templates methodoverride ]
list = app.methods(false) + Sinatra::Base.methods(false)
list = list.filter_map {|meth| meth[/^(.*?)=$/, 1] }.sort - skip
list = list.map {|meth| [meth, (app.class.send(meth) rescue $!.inspect)] }
display list
end
# launch IRB
IRB.setup nil
IRB.conf[:IRB_NAME] = "\e[36m#{env}\e[0m"
IRB::Irb.new.run IRB.conf |
Beta Was this translation helpful? Give feedback.
-
This seems to work pretty well! :-) Running This issue started as a question, but I wrote some code that seems to solve the problem I was looking to address. I might convert it into a little gem. |
Beta Was this translation helpful? Give feedback.
-
This is really neat!! Thank you for sharing!!! I'm not sure where it fits in but maybe something we can add to sinatra-contrib? |
Beta Was this translation helpful? Give feedback.
-
I updated the code a bit... now, I can:
Plus, all the interactions are using plain ol' IRB 1.6, so we get syntax highlighting, debugging, etc. |
Beta Was this translation helpful? Give feedback.
-
I updated this and made a little video... |
Beta Was this translation helpful? Give feedback.
-
#!/usr/bin/env ruby
# ==============================================================================
# rax - Half rat-pack, half tux... a minimal and modern Sinatra console in IRB
#
# Author: Steve Shreeve (steve.shreeve@gmail.com)
# Date: Jan 15, 2023
# ==============================================================================
# NOTE: Here are the goals...
#
# 1. call `app.get '/'` to trigger a request
# 2. easily call Sinatra helpers, such as `app.current_weather`
# 3. set ivars like `app.set(name: 'bob', age: 25)`
# 4. use templates like `app.erb :test` (using `name` from above)
# 5. set ivars like `@name='mike'`, then `app.erb :test, scope: self`w
# 6. normal access to database calls, like `Sponsor[2]` to show a record
# 7. call `routes` to show list sorted by route
# 8. call `props` to show application settings
# ==============================================================================
STDOUT.sync = true
require 'bundler/setup'
require 'irb'
require 'rack/test'
IRB::ExtendCommandBundle::HushAliasWarning = true
class Rax
attr_reader :env, :app
def initialize(file=nil)
@env = ENV['APP_ENV'] ||= ENV['RACK_ENV'] || 'development'
@app = Rack::Builder.parse_file(file || ENV['RACK_CONFIG'] || 'config.ru')[0]
end
def self.instance
@instance ||= new
end
def self.app
instance.app
end
end
def env
@env ||= Rax.instance.env
end
def app
@app ||= begin
app = Rax.app.new!
app.app = Rax.app
app.extend Rack::Test::Methods
app.request = Sinatra::Request.new({})
app.response = Sinatra::Response.new
app
end
end
def app!
@app = nil
app
end
def app.set(**vars)
vars.each {|k,v| instance_variable_set "@#{k}", v }
self
end
def display(list)
wide = list.map {|e| e[0].length }.max
list.each {|a, b| puts "%-*s %s" % [wide, a, b.inspect] }
nil
end
def routes
list = Rax.app.routes.except 'HEAD'
list = list.inject([]) do |list, (meth, hits)|
list += hits.map {|vals| [meth.downcase, vals[0].to_s] }
end
list = list.sort_by {|a, b| [b, a]}
display list
end
def props
skip = %w[ inline_templates methodoverride ]
list = app.methods(false) | Rax.app.methods(false) | Sinatra::Base.methods(false)
list = list.filter_map {|meth| meth[/^(.*?)=$/, 1] }.sort - skip
list = list.map {|meth| [meth, (Rax.app.send(meth) rescue $!.inspect)] }
display list
end
# launch IRB
IRB.setup nil
IRB.conf[:IRB_NAME] = "\e[36m#{env}\e[0m"
IRB::Irb.new.run IRB.conf |
Beta Was this translation helpful? Give feedback.
-
I've created a new gem called |
Beta Was this translation helpful? Give feedback.
-
Closing as there is a gem now at => https://github.com/shreeve/sinatra-rax |
Beta Was this translation helpful? Give feedback.
-
I've used
tux
for years as arails console
type of tool to interact with sinatra apps from a console.Is there a modern version, that doesn't use
ripl
and just uses the new/shinyirb
?It would be awesome to be able to launch
irb
and be able to require something likesinatra-console
(a putativetux
replacement) and be able to test right from the console.Anything?
Beta Was this translation helpful? Give feedback.
All reactions