ruby 1.9, rvm, sinatra and rackup - ruby

Gemfile
source :rubygems
gem 'sinatra'
config.ru
require 'app'
run App
app.rb
require 'bundler/setup'
require 'sinatra'
class App < Sinatra::Base
get '/' do
'hello world'
end
end
rackup failes with
.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': no such file to load -- app (LoadError)
Works with ruby 1.8 . Why?

I think it's because 1.9.2 no longer includes '.' in the load path by default.
See the this question for more: Why does Ruby 1.9.2 remove "." from LOAD_PATH, and what's the alternative?

Some notes:
Gemfile, I use gem 'sinatra', :require => 'sinatra/base' to load a Modular Sinatra App.
Config.ru, usually I set Bundler on it, not in app.rb, leaving app.rb clean to my app.
require 'bundler/setup'
Bundler.require(:default)

Related

Sinatra: NoMethodError undefined method `slim'

What am I missing? I've included slim in my Gemfile & 'required' it.
config.ru:
require './sinatra.rb'
run Sinatra::Application
Gemfile:
source 'https://rubygems.org'
gem 'rack'
gem 'sinatra'
gem 'slim'
sinatra.rb:
require 'sinatra'
require 'slim'
get '/' do
slim :index
end

Why heroku detects my sinatra app as rails and fails?

# Gemfile:
source 'https://rubygems.org'
ruby '1.9.3'
gem 'sinatra'
gem 'figaro'
gem 'octokit'
# app.rb
require 'sinatra'
require 'json'
require 'cgi'
require 'octokit'
require 'figaro'
class Application < Sinatra::Base
get '/' do
'Hi'
end
end
# config.ru
require './app'
$stdout.sync = true
run Application
When push:
-----> Ruby/Rails app detected
However, if I remove figaro gem and repush it works fine.
You answered it yourself:
figaro depend on Rails (see the gemspec: https://github.com/laserlemon/figaro/blob/master/figaro.gemspec). Heroku detect rails application by looking if their Gemfile.lock contains the Railties gem, which is a dependency of rails.
So: yourapp -> figaro -> rails -> railties.
Hence the identification of your gem as a Rails App. Why are you using Figaro if not to help configure a Rails application anyway?

Ruby no such file to load

I'm trying to use httparty
require 'httparty'
but I'm getting
no such file to load -- httparty (LoadError)
I'm using Ruby 1.8.7 on OSX
On Ruby 1.8, you have to do require "rubygems" before you can require any Gems in your code. So:
require "rubygems"
require "httparty"
# Your code here.

Why do my rspec tests run slower in Ruby 1.9.2 than 1.8.7?

I'm using spork to test a Sinatra application and with Ruby 1.9.2 the tests run in about 3.5 seconds but in Ruby 1.8.7 they average 1.2 seconds. I did try Ruby 1.9.3 and even JRuby but they had some errors with the gems I'm using. Is there a way to bring Ruby 1.9.2's rspec performance up to 1.8.7's level?
My Gemfile:
source :rubygems
gem 'sinatra', '1.3.1'
gem 'thin', '1.3.1'
gem 'haml', '3.1.4'
gem 'datamapper', '1.2.0'
gem 'dm-postgres-adapter', '1.2.0'
gem 'carrierwave', '0.5.8'
gem 'carrierwave-datamapper', '0.2.0'
group :test do
gem "dm-sqlite-adapter"
gem "spork"
gem "rspec"
gem "rack-test"
end
spec_helper.rb:
require 'rubygems'
require 'spork'
require 'sinatra'
require 'rack/test'
require 'rspec'
require File.join(File.dirname(__FILE__), '..', 'app.rb')
require File.join(File.dirname(__FILE__), '..', 'model/model.rb')
Spork.prefork do
set :environment, :test
set :files, "test_files"
end
Spork.each_run do
RSpec.configure do |config|
config.before(:each) { DataMapper.auto_migrate! }
config.after(:all) do
FileUtils.rm_rf(Dir["#{settings.root}/public/test_files"])
end
end
end
thanks!
There was a problem in the way ruby 1.9.2 require'd files during startup:
http://rhnh.net/2011/05/28/speeding-up-rails-startup-time
1.9.3 has a partial fix for this IIRC.
Not according to rspec's own test: https://gist.github.com/939865. It is supposed to be faster. It could be something slower in your stack.

Sinatra and Bundler

I'm trying to get Bundler setup so I can deploy my Sinatra app to server with all the correct gems.
I've created my Gemfile
source :gemcutter
gem 'sinatra', '1.0'
gem "nokogiri", "1.4.2"
gem "rack", "1.1.0"
gem "dm-core", "1.0.0"
gem "dm-migrations", "1.0.0"
gem "dm-sqlite-adapter", "1.0.0"
gem "pony", "1.0"
Next I created a Config.ru
require 'rubygems'
require 'bundler'
Bundler.setup
require 'sinatra'
require 'dm-core'
require 'dm-migrations'
require 'dm-sqlite-adapter'
require 'open-uri'
require 'nokogiri'
require 'csv'
require 'pony'
require 'parsedate'
require 'digest/md5'
require 'MyApp'
run MyApp
So far so good, so next I ran bundle install and got 'Bundle Complete' so now all I need to do is just Rackup
Then I get:
config.ru:18: undefined local variable or method `MyApp' for #<Rack::Builder:0x1227350 #ins=[]> (NameError)
from /usr/local/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/builder.rb:46:in `instance_eval'
from /usr/local/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/builder.rb:46:in `initialize'
from config.ru:1:in `new'
from config.ru:1
Here's a simple MyApp.rb which will trigger the same error
get '/' do
erb :index
end
What's going wrong? :(
If you tell Rack to run MyApp, you need to define class MyApp first (which you're supposed to do inside MyApp.rb). Derive your class from Sinatra::Base to make it a Sinatra-Rack-App that can be run from config.ru:
require 'sinatra/base'
class MyApp < Sinatra::Base
get '/' do
erb :index
end
end
See also Sinatra's README about modular Sinatra apps (search for the paragraph named "Modular Apps" on http://github.com/sinatra/sinatra/)
Additionally you may have your my_app.rb as follows:
require 'rubygems'
require 'bundler'
Bundler.setup
require 'sinatra'
require 'dm-core'
require 'dm-migrations'
require 'dm-sqlite-adapter'
require 'open-uri'
require 'nokogiri'
require 'csv'
require 'pony'
require 'parsedate'
require 'digest/md5'
And your config.ru like this:
require './my_app'
run Rack::URLMap.new '/' => Sinatra::Application
Hope this helps.
Best Regards
ED
As an alternative to creating a modular app (wrapping your Sinatra methods in a class extending Sinatra::Base), you can use:
run Sinatra::Application
in the config.ru file in place of
run MyApp
This might be a better option if you want to keep the simple Sinatra code.
See the docs for more info.

Resources