Ruby / sinatra - How to ouput JSON - ruby

I'm trying to format my output as json. Here is a test code from http://www.sinatrarb.com/contrib/json.html :
require "sinatra"
require "sinatra/json"
# define a route that uses the helper
get '/' do
json :foo => 'bar'
end
# The rest of your classic application code goes here...
It shows me an "Application Error". Maybe it comes from the other files to launch the app. I'm using heroku(cloud). So I have a Gemfile :
source 'https://rubygems.org'
gem 'sinatra', '1.1.0'
gem 'thin'
Gemfile.lock :
GEM
remote: https://rubygems.org/
specs:
daemons (1.1.9)
eventmachine (1.0.3)
rack (1.5.2)
sinatra (1.1.0)
rack (~> 1.1)
tilt (~> 1.1)
thin (1.5.1)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
tilt (1.4.1)
PLATFORMS
ruby
DEPENDENCIES
sinatra (= 1.1.0)
thin
and Procfile :
web: bundle exec ruby web.rb -p $PORT
Did I miss something ?

You need to reference in your Gemfile json gem, e.g:
gem "json", "1.5.5"
And then you call .to_json on the object you want to output as JSON.

Related

Getting a Rack GemNotFound with Sinatra

This is my first attempt with a ruby stack. I'm stuck with the following error:
Could not find rack-1.6.4 in any of the sources (Bundler::GemNotFound)
I've successfully installed the following components:
ubuntu 14.04.3 LTS
rvm 1.26.11
ruby 2.1.6p336
nginx 1.8
* LOCAL GEMS *
bigdecimal (1.2.4)
bundler (1.10.6)
bundler-unload (1.0.2)
executable-hooks (1.3.2)
gem-wrappers (1.2.7)
io-console (0.4.3)
json (1.8.1)
minitest (4.7.5)
psych (2.0.5)
rack (1.6.4)
rack-protection (1.5.3)
rake (10.1.0)
rdoc (4.1.0)
rubygems-bundler (1.4.4)
rvm (1.11.3.9)
sinatra (1.4.6)
test-unit (2.1.6.0)
tilt (2.0.1)
* Gemfile *
gem 'sinatra', '1.4.6'
* Gemfile.lock *
GEM
remote: https://rubygems.org/
specs:
rack (1.6.4)
rack-protection (1.5.3)
rack
sinatra (1.4.6)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
tilt (2.0.1)
PLATFORMS
ruby
DEPENDENCIES
sinatra (= 1.4.6)
BUNDLED WITH
1.10.6
* config.ru *
require './app.rb'
run Sinatra::Application
* app.rb *
require 'bundler/setup'
require 'sinatra'
get '/' do
'hello world'
end
I'm using the default Nginx folder for the app. I was able to execute the following test in config.ru:
app = proc do |env|
[200, { "Content-Type" => "text/html" }, ["hello world"]]
end
But as soon I try to switch to Sinatra I get the error above.
Thanks in advance!
Ok I found the problem.
I followed the installation instruction here: https://www.phusionpassenger.com/library/install/nginx/install/oss/trusty/
But since I'm using rvm I had to change the passenger_ruby directive to point to the rvm wrapper: /usr/local/rvm/wrappers/ruby-2.1.6/ruby
Problem is here:
rack (1.6.4)
...
sinatra (1.4.6)
rack (~> 1.4)
You have a conflict in the rack version. you want both 1.6.4 and 1.4.x
How did rack 1.6.4 end up in the gemfile.lock?

Sinatra with Kaminari: cannot load such file -- kaminari/sinatra

I want to realize pagination in Sinatra with kaminari.
My Gemfile looks like this:
source "https://rubygems.org"
gem "sinatra"
gem "activerecord", :require => "active_record"
gem "mysql2"
gem "padrino-helpers"
gem "kaminari", :require => "kaminari/sinatra"
my Sinatra config.ru has a Bundler.require.
However, Passenger displays the following error: cannot load such file -- kaminari/sinatra
I don't know what's wrong. There is not much documentation about kaminari with Sinatra, and the only bit i found was: "You need padrino-helpers and require kaminari/sinatra". And that's what i did.
Sinatra (~> 1.4.0) depends on rack (1.5.2), while kaminari (~> 0.13.0) depends on rack (~> 1.2.1). And since kaminari only began experimental support for Sinatra since version 0.13.0, there appears to be an impasse.

JRuby "no such file to load"

Using jruby-1.6.7 via rvm and gem version 1.8.24 I created a new project containing a Gemfile:
source 'http://rubygems.org'
gem 'google-api-client'
and a main.rb:
require 'google/api_client'
After
gem install bundler
bundle install
gem list
I get
addressable (2.2.8)
autoparse (0.3.1)
bouncy-castle-java (1.5.0146.1)
bundler (1.1.4)
extlib (0.9.15)
faraday (0.8.1, 0.7.6)
ffi (1.0.11 java)
google-api-client (0.4.3)
jruby-launcher (1.0.14 java)
jruby-openssl (0.7.7)
json (1.7.3 java)
jwt (0.1.4)
launchy (2.1.0 java)
multi_json (1.3.6)
multipart-post (1.1.5)
rack (1.4.1)
rake (0.9.2.2)
rubygems-bundler (1.0.2)
rubygems-update (1.8.24, 1.7.2)
rvm (1.11.3.3)
signet (0.3.4)
spoon (0.0.1)
But when I run ruby main.rb, it results in an error:
LoadError: no such file to load -- google/api_client
require at org/jruby/RubyKernel.java:1033
(root) at main.rb:1
On MRI everything seems to be working fine. The problem does not seem bound to any specific gem.
Can someone help me with this?
Unless you have JRUBY_OPTS=--1.9 in your environment, or you pass the --1.9 flag on the command line, JRuby will default to 1.8 mode, which means that you explicitly have to require 'rubygems' in main.rb before you load your gems.

How to solve " uninitialized constant Clockwork (NameError)"

I successfully installed gem clockwork
C:\web>gem install clockwork
Successfully installed clockwork-0.2.3
1 gem installed
Installing ri documentation for clockwork-0.2.3...
Installing RDoc documentation for clockwork-0.2.3...
but when running simple script
require 'rubygems'
require 'clockwork'
include Clockwork
every(1.minute, 'custom.event.handler' ) do
puts "This event has its own handler - #{Time.new.strftime("%Y%m%d%H%M%S")}"
end
I get this error
C:\web>ruby clockwork.rb
./clockwork.rb:3: uninitialized constant Clockwork (NameError)
from C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require'
from C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
from clockwork.rb:2
Any suggestion how to make clockwork work on
Windows XP
ruby 1.8.7 (2010-08-16 patchlevel 302) [i386-mingw32]
gem 1.3.7
C:\web>gem list --local
*** LOCAL GEMS ***
aaronh-chronic (0.3.9)
activesupport (3.0.9)
backports (1.18.2)
clockwork (0.2.3)
daemons (1.1.0)
delayed_job (2.0.3)
eventmachine (0.12.10 x86-mswin32-60)
fastercsv (1.5.4)
haml (3.0.21)
i18n (0.6.0)
json (1.5.1)
mechanize (1.0.0)
monkey-lib (0.5.4)
nokogiri (1.5.0 x86-mingw32, 1.4.3.1 x86-mingw32)
rack (1.2.1)
rdiscount (1.6.8)
ruby-growl (3.0)
sinatra (1.2.6, 1.0)
sinatra-advanced-routes (0.5.1)
sinatra-reloader (0.5.0)
sinatra-sugar (0.5.1, 0.5.0)
thin (1.2.7 x86-mswin32)
tilt (1.3)
Let me give you a hint. The name of your own program is clockwork.rb, and in it you do:
require 'clockwork'
See the problem?
you need to include the following in your clock.rb
require './config/boot'
require './coonfig/environment'

Rake 0.8.7 and ActiveSupport 3.0.1

I am running Ruby 1.9.2. I have rake 0.8.7 installed. However running rake inside a Rails application gives me the following:
(in /usr/home/users/dimitar/Rails/spek)
Could not find activesupport-3.0.1 in any of the sources
Try running `bundle install`.
So I go ahead and run bundle install again and everything looks good:
Fetching source index for http://rubygems.org/
Using rake (0.8.7)
Using abstract (1.0.0)
Using activesupport (3.0.1)
Using builder (2.1.2)
Using i18n (0.4.1)
Using activemodel (3.0.1)
Using erubis (2.6.6)
Using rack (1.2.1)
Using rack-mount (0.6.13)
Using rack-test (0.5.6)
Using tzinfo (0.3.23)
Using actionpack (3.0.1)
Using mime-types (1.16)
Using polyglot (0.3.1)
Using treetop (1.4.8)
Using mail (2.2.7)
Using actionmailer (3.0.1)
Using arel (1.0.1)
Using activerecord (3.0.1)
Using activeresource (3.0.1)
Using bundler (1.0.3)
Using thor (0.14.3)
Using railties (3.0.1)
Using rails (3.0.1)
Using sqlite3-ruby (1.3.1)
Your bundle is updated! Use `bundle show [gemname]` to see where a bundled gem is installed.
But rake still gives me the same error message. Any suggestions?
Actually the problem seems to come from the config/boot.rb file:
# Set up gems listed in the Gemfile.
gemfile = File.expand_path('../../Gemfile', __FILE__)
begin
ENV['BUNDLE_GEMFILE'] = gemfile
require 'bundler'
Bundler.setup
rescue Bundler::GemNotFound => e
STDERR.puts e.message
STDERR.puts "Try running `bundle install`."
exit!
end if File.exist?(gemfile)
Do you have multiple Ruby interpreters installed (maybe via rvm or some other method)? If so, are you sure that the right one is active?
Things to look at:
which ruby
which rake
ruby -e 'puts $:' # To show the LOAD_PATH for gems
I had a similar problem using ruby-1.9.2 and fixed it by upgrading rubygems
sudo gem update --system
I had the same problem when I was using REE 1.8.7. I switched to 1.9.2 and did bundle install. Then my rake commands worked.
I had this issue when I mistakenly ran passenger with ruby 1.9.3 and the app used 1.8.7 in its .rvmrc. Fixed by using 1.9.3 (now I have encoding problems, but thats a different story).

Resources