Inserting middleware in Padrino - ruby

I have a skeleton Padrino (0.10.7) project, with pretty much no code. I am trying to insert a middleware in boot.rb:
##
# Add your after (RE)load hooks here
#
Padrino.after_load do
DataMapper.finalize
Padrino.use MyClass #Line (1) added by me
end
Padrino.load!
In MyClass,
class MyClass
def initialize arg
#arg = arg
end
end
If I try to use thin server (1.5.x), I get this exception (only when I insert my middleware):
Uncaught exception: app required
Same works fine with builtin webrick.
Any idea on how to make it work with thin?

Never mind, found it. Basically, you need to define the call (env) method too, otherwise it wont even start the server. This is what minimum is required from a middleware:
class MyClass
def initialize app
#app = app
end
def call env
#app.call env
end
end

Related

Ruby Module Constant Set and Read

I'm developing a module that contains standard methods as well as CLI (Thor) and API (Sinatra). I've created a Submodule that includes a class that inherits Sinatra::Base:
module Monteverde
module Activity
def self.version
"1.0.1"
end
class API < Sinatra::Base
set :port, 22340
get "/version" do
Monteverde::Activity.version
end
run! if defined? Monteverde::OPERATION
end
end
end
This "Activity" is invokes from the CLI (Thor):
module Monteverde
class CLI < Thor
desc "api", "Start Activity API"
def api
Monteverde.const_set("OPERATION", "started")
Monteverde::Activity::API
end
desc "aversion", "Get Activity Version"
def aversion
puts Monteverde::Activity.version
end
end
end
If I don't add an "if" to Sinatra's run! it will run automatically and take over the rest of the methods in the module:
...
class API < Sinatra::Base
register Sinatra::DefaultRoutes
set :port, 22340
get "/version" do
Monteverde::Activity.version
end
run!
end
$ ruby monteverde.rb aversion
$ == Sinatra (v2.0.0) has taken the stage on 22340 for development with backup from Puma
$ ...
My issue is that the OPERATION constant is not recognized even though it's set right before the module is called.
Why isn't OPERATION being recognized and how can I get it to be?
Is there another more intuitive/useful way to get Sinatra not to fire when I call the module?
In your API class definition, the run! line happens as soon as you require that file. It doesn't run again if you reference the class, as you're trying to do with the last line of your def api method.
To get around this, you can move the run! command into a method, and call that from Thor:
class API < Sinatra::Base
# ... other stuff
def self.start
run! if defined? Monteverde::OPERATION
end
end
Then in the Thor file:
def api
Monteverde.const_set("OPERATION", "started")
Monteverde::Activity::API.start
end
You can deduce that run! is a class method since it's callable in the scope of the class definition (not in an instance method), and so I define a class method to call it.
It would be simpler, though, to not define def self.start, and instead just call the run! method from the Thor file directly:
def api
Monteverde::Activity::API.run!
end

How to access "global" (constant?) capistrano variables from classes? (ruby)

So my deploy.rb script in capistrano starts like this, which I guess is pretty normal:
require 'capistrano/ext/multistage'
require 'nokogiri'
require 'curb'
require 'json'
# override capistrano defaults
set :use_sudo, false
set :normalize_asset_timestamps, false
# some constant of mine
set :my_constant, "foo_bar"
Later, I can access my constant in functions or tasks within namespaces, like:
namespace :mycompany do
def some_function()
run "some_command #{my_constant}"
end
desc <<-DESC
some task description
DESC
task :some_task do
run "some_command #{my_constant}"
end
end
However, if I use the constant in a class, like this:
namespace :mycompany do
class SomeClass
def self.some_static_method()
run "some_command #{my_constant}"
end
end
end
It fails with:
/config/deploy.rb:120:in `some_static_method': undefined local variable or method `my_constant' for #<Class:0x000000026234f8>::SomeClass (NameError)
What am I doing wrong??
Thanks
The deploy.rb file is instance_evaled, this means it's being executed inside the context of an object, and as such anything you declare will be available until you leave that context. As soon as you create a class that provides a new context.
In order to access the original context you have to pass the object (self) to the class method.
namespace :mycompany do
class SomeClass
def self.some_static_method(cap)
run "some_command #{cap.fetch(:my_constant)}"
end
end
SomeClass.some_static_method(self)
end
Although I really don't understand why you are declaring a class like this, it's an odd place for it.

Access Sinatra settings from a model

I have a modular Sinatra app. I'm setting some custom variables in my configure block and want to access these settings in my model.
The problem is, I get a NoMethodError when I try and access my custom settings from MyModel. Standard settings still seem to work fine though. How can I make this work?
# app.rb
require_relative 'models/document'
class App < Sinatra::Base
configure do
set :resource_path, '/xfiles/i_want_to_believe'
end
get '/' do
#model = MyModel.new
haml :index
end
end
# models/my_model.rb
class MyModel
def initialize
do_it
end
def do_it
...
settings.resource_path # no method error
...
settings.root # works fine
end
end
i think that you should be able to access it via
Sinatra::Application.settings.documents_path
I ended up doing:
#document.rb
class Document
def self.documents_path=(path)
#documents_path = path
end
def self.documents_path
#documents_path
end
...
end
#app.rb
configure do
set :documents_path, settings.root + "/../documents/"
Document.documents_path = settings.documents_path
end
then just using Document.documents_path inside my find method.

In Sinatra(Ruby), how should I create global variables which are assigned values only once in the application lifetime?

In Sinatra, I'm unable to create global variables which are assigned values only once in the application lifetime. Am I missing something? My simplified code looks like this:
require 'rubygems' if RUBY_VERSION < "1.9"
require 'sinatra/base'
class WebApp < Sinatra::Base
#a = 1
before do
#b = 2
end
get '/' do
puts #a, #b
"#{#a}, #{#b}"
end
end
WebApp.run!
This results in
nil
2
in the terminal and ,2 in the browser.
If I try to put #a = 1 in the initialize method, I'm getting an error in the WebApp.run! line.
I feel I'm missing something because if I can't have global variables, then how can I load large data during application instantiation?
before do seems to get called every time there is a request from the client side.
class WebApp < Sinatra::Base
configure do
set :my_config_property, 'hello world'
end
get '/' do
"#{settings.my_config_property}"
end
end
Beware that if you use Shotgun, or some other Rack runner tool that reloads the code on each request the value will be recreated each time and it will look as if it's not assigned only once. Run in production mode to disable reloading and you will see that it's only assigned on the first request (you can do this with for example rackup --env production config.ru).
I ran into a similar issue, I was trying to initialize an instance variable #a using the initialize method but kept receiving an exception every time:
class MyApp < Sinatra::Application
def initialize
#a = 1
end
get '/' do
puts #a
'inside get'
end
end
I finally decided to look into the Sinatra code for initialize:
# File 'lib/sinatra/base.rb', line 877
def initialize(app = nil)
super()
#app = app
#template_cache = Tilt::Cache.new
yield self if block_given?
end
Looks like it does some necessary bootstrapping and I needed to call super().
def initialize
super()
#a = 1
end
This seemed to fix my issue and everything worked as expected.
Another option:
helpers do
def a
a ||= 1
end
end
Building on Theo's accepted solution, it is also possible to do:
class App < Sinatra::Application
set :blabla, ''
namespace '/b' do
get '/baby' do
# do something where bouh is assigned a value
settings.blabla = 'bouh'
end
end
namespace '/z'
get '/human' do
# settings.blabla is available here with newly assigned value
end
end
end
You could use OpenStruct.
require 'rubygems'
require 'sinatra'
require 'ostruct'
configure do
Struct = OpenStruct.new(
:foo => 'bar'
)
end
get '/' do
"#{Struct.foo}" # => bar
end
You can even use the Struct class in views and other loaded files.

Sinatra Set Settings (Ruby)

Using Sinatra in Ruby you can set the server's settings by doing:
set :myvariable, "MyValue"
and then access it anywhere in templates etc with settings.myvariable.
In my script I need to be able to re-set these variables falling back to a bunch of defaults. I figured the easiest way to do this would be to have a function that performs all the sets calling it at the start of the Sinatra server and when I need to make the alterations:
class MyApp < Sinatra::Application
helpers do
def set_settings
s = settings_from_yaml()
set :myvariable, s['MyVariable'] || "default"
end
end
# Here I would expect to be able to do:
set_settings()
# But the function isn't found!
get '/my_path' do
if things_go_right
set_settings
end
end
# Etc
end
As explained in the code above, the set_settings function isn't found, am I going about this the wrong way?
You're trying to call set_settings() inside the class scope of MyApp, but the helper method you used to define it only defines it for use inside that get... do...end block.
If you want set_settings() to be available statically (at class-load time instead of at request-process time), you need to define it as a class method:
class MyApp < Sinatra::Application
def self.set_settings
s = settings_from_yaml()
set :myvariable, s['MyVariable'] || "default"
end
set_settings
get '/my_path' do
# can't use set_settings here now b/c it's a class
# method, not a helper method. You can, however,
# do MyApp.set_settings, but the settings will already
# be set for this request.
end

Resources