I have shoes raisins (0.r1134) [i686-darwin8.9.1] +video
I'm trying to set up a Shoes.setup block like this:
Shoes.setup do
gem 'mini_exiftool'
gem 'xml-simple > 1.0'
require "mini_exiftool"
require 'xmlrpc/client'
require 'xmlsimple.rb'
require "my_webservice_api_wrapper"
mwa = MyWebserviceApiWrapper.new
mwa.login # problems...
end
All works fine until we get to mwa.login, which takes us off to my XML-RPC wrapper API that will do a secure login. I get the error:
undefined method `closed?' for #
If I fire up irb and load "my_webservice_api_wrapper.rb" it all works fine, so I'm thinking maybe I misunderstood what should be loaded in Shoes and when.
Any help appreciated. Shoes looks really cool.
I think you need to break that up into two separate blocks:
Shoes.setup do
gem 'mini_exiftool'
gem 'xml-simple > 1.0'
end
require "mini_exiftool"
require 'xmlrpc/client'
require 'xmlsimple.rb'
require "my_webservice_api_wrapper"
Shoes.app do
mwa = MyWebserviceApiWrapper.new
mwa.login # problems...
end
I hope your login module doesn't use HTTPS as I don't think that is supported in Shoes yet.
Related
Can't get Kaminari to work with Sinatra and Mongoid. I'm getting this error:
NoMethodError at /api/events
undefined method `page' for #<Mongoid::Criteria:0x007fccb7828c38>
Here is minimal code to get the error:
Gemfile
source "https://rubygems.org"
gem 'mongoid'
gem 'sinatra'
gem 'kaminari-mongoid'
gem 'kaminari-sinatra'
server.rb
require 'mongoid'
require 'sinatra'
class Event
include Mongoid::Document
end
get '/events' do
Event.desc(:id).page(params[:page]).per(10)
end
I have tried require 'kaminari', require 'kaminari-sinatra', require 'kaminari-mongoid', all to no avail (I get LoadErrors). I've also tried register Kaminari::Helpers::SinatraHelpers as mentioned here, which also failed.
I've followed the instructions in detail, and have scoured Google and StackOverflow to no avail. This answer didn't work. I can't help thinking I'm missing something easy; I'm not a Ruby veteran. My hunch is it's something with Bundler. Any idea?
I ran into the problem as well. Unfortunately, kaminari-mongoid has a rails dependency (you can look in the gemspec file here: https://github.com/kaminari/kaminari-mongoid/blob/master/kaminari-mongoid.gemspec). Therefore, it is not possible to use both kaminari-sinatra and kaminari-mongoid.
This solved my problem. https://github.com/ajsharp/mongoid-pagination. Add it to your Gemfile and install with Bundler.
In your app.rb file, require 'mongoid-pagination'
I am trying to run some older ruby script (with old ruby version) from within a ruby script. Here is a program:
old_ruby187.rb
#!/usr/ruby/1.8.7/bin/ruby
puts "Hello"
new_ruby230.rb
#!/usr/ruby/2.3.0/bin/ruby
require "rubygems"
require "bundler"
Bundler.setup # Code works if I comment this line
puts `old_ruby187.rb`
I get bundler load error. If I execute ./new_ruby230.rb, it gives an error for the last puts command line.:
'require': no such file to load -- rubygems (LoadError)
If I comment just Bundler.setup and run it, it works fine. Not sure if Bundler.setup tries to load something for system call. I need bundler for other gems used in new_ruby230.rb script.
Any help is appreciated.
Update (02/22/2018):
I ended up using ssh when calling old ruby script. Something like:
new_ruby230.rb
#!/usr/ruby/2.3.0/bin/ruby
require "rubygems"
require "bundler"
require "socket"
Bundler.setup
puts `ssh #{Socket.gethostname} old_ruby187.rb` # this worked!
Pretty sure you're not supposed to require rubygems like that. Bundler is there to manage your gems. Remove that require statement (require "rubygems") and you should be good to go.
I've been working on a web application in Ruby using Sinatra. While in development, I really never had a lot of issues with sessions. However, now i'm ussing passenger to actually deploy the application I have quite a lot of issues regarding that session data keeps getting 'reset'.
I've seen other stack overflow questions related to this problem but no answer has yet fixed it for me. I've tried a couple of things:
Putting 'use Rack::Session::Pool' inside config.ru as suggested here
Used Rack::Session::Pool like explained here
Tried Memcache as explained here
Tried Redis-Rack (gives me an error: Exception NoMethodError in Rack application object (undefined method `foreign_key' for nil:NilClass))
Nothing seems to be helping really... I either end up having an error or my session gets reset each request. I know that passenger probably uses multiple threads and that that is the cause for the sessions to not be working, but I cannot seem to find a solution to the problem.
Am I missing something obvious here? Any suggestions?
Gemfile
source "https://rubygems.org"
gem 'mongo', '1.8.6'
gem 'sinatra', '1.4.8'
gem 'mongo_mapper'
gem 'bson_ext'
gem 'active_model_serializers'
gem 'activemodel-serializers-xml'
gem 'sinatra-flash'
gem 'sinatra-param', require: 'sinatra/param'
gem 'rack-recaptcha2', git: 'https://github.com/nicolas-simplex/rack- recaptcha'
gem 'mail'
gem 'slugify'
gem 'biz'
gem 'bcrypt'
gem 'redis-rack'
group :development do
gem 'mailcatcher', '~> 0.6.4'
end
config.ru
It had different forms depending on if I used just Rack::Session::Pool, Memcache or Redis. This one was the one I used for redis:
require 'rubygems'
require 'sinatra'
require File.expand_path '../app.rb', __FILE__
require 'rack'
require 'rack/session/redis'
require_relative './app'
app = App.new
sessioned = use Rack::Session::Redis.new(app)
run sessioned
App.rb
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require 'slugify'
class App < Sinatra::Base
use Rack::Protection
set :bind, '0.0.0.0'
end
require_relative './config/init' # Initialize configuration files
require_relative './helpers/init' # Initialize helpers
require_relative './routes/init' # Initialize routes
require_relative './models/init' # Initialize models
require_relative './util/tokens' # Token utility
... # Some database seeding, basic setup for some data I use
TL;DR
Sinatra and Passenger together resets sessions in production, while this doesn't happen in development.
After some more researching I found the configuration option passenger_sticky_sessions. Because in my web application session storage is quite important, this works fine for me. However, the use of this is not recommended in all use cases because all the clients sends will be routed to the same originating application process.
Documentation: https://www.phusionpassenger.com/library/config/nginx/reference/#passenger_sticky_sessions
With this configuration parameter 'on' in my conf file in nginx/sites-enabled, I was able to just use the simple rack session pool:
use Rack::Session::Pool, path: '/', expire_after: 2592000
I'm putting some shared models for a rails app inside it's own gem. It's just models, so I'm not using an engine. Getting it set up seemed to work fine until I added the "acts_as_list" gem.
# gem - domain.gemspec
spec.add_dependency "acts_as_list"
# gem - lib/domain.rb
require "acts_as_list"
# gem - lib/domain/models/page.rb
acts_as_list scope: [:ancestry]
This works fine in the console for my gem, I can run methods specific to acts_as_list as usual. However, when I add my gem to another project, it gives me an error.
# project - Gemfile
gem "www_domain", path: "../www_domain"
Bundler::GemRequireError: There was an error while trying to load the gem 'domain'.
NoMethodError: undefined method `acts_as_list' for #<Class:0x0055da70121ab0>
/home/shaun/sites/demo/domain/lib/domain/models/page.rb:32:in `<class:Page>'
Is there something special I have to do in this case to access the acts_as_list method because my model is inside a gem?
Update: Here is my complete lib/domain.rb file for the gem:
require "yaml"
require "active_record"
require "acts_as_list"
require "ancestry"
# rbfiles = File.join(File.dirname(__FILE__), "lib", "**", "*.rb")
# Dir.glob(rbfiles).each do |file|
# require file.gsub("lib/", "")
# end
module Domain
# Your code goes here...
def self.root
File.dirname(__dir__)
end
def self.db_config
YAML.load_file("#{Domain.root}/db/config.yml")["development"]
end
end
require "domain/version"
require "domain/models/page"
require "domain/models/menu"
require "domain/models/article"
require "domain/models/page_part"
I can use acts_as_list and ancestry methods in the console of my gem (running bin/console from the gem directory). But the project console (running bundle exec rails console from the project directory) will not start because of the gem error I mentioned.
This might result from a load order issue if the model being required before the require :acts_as_list statement. Check to see if the gem specifies the load order. If not, you could try something like:
# gem - lib/domain.rb
require "acts_as_list"
require "models/page"
If the load order is unclear, I find it helpful to simply add a
puts __FILE__
at the top of the relevant source files.
Here is what I have in my rack app
#rb file
require 'pry'
class .....
#GemFile
group :development do
gem "pry"
gem "pry-nav"
end
Of course, in production it causes an error. How do make a kind of "require if"?
require 'pry' if ENV['RACK_ENV'] == 'development'
May be you can embed it inside a if block
according to docs Sinatra provides a environment variable
http://www.sinatrarb.com/intro#Environments
if development?
require 'pry'
end
wherever you need to use it.
this may not be the exact solution you may be looking for just a wild guess
I suggest to write such method in Object or Kernel in your app:
def require_pry
require 'pry' if ENV['RACK_ENV'] == 'development'
end
After that you can call require_pry if you need it in your code. But I have doubts why it cannot be handled by Bundler, Bundle.require will require all gems needed for environment.