Problems implementing ruby geocoder using Sinatra - sinatra

According to the ruby geocoder documentation (rubygeocoder.com), it's possible to use the geocoder gem with a Sinatra app, but I'm running into issues getting it to work and haven't been able to find any working examples or related stackoverflow issues either. I think the problem is due to the fact that it's a Sinatra app and not a full rails app.
My Gemfile:
source "https://rubygems.org"
ruby '2.1.2'
gem 'dotenv', '~> 0.10.0'
gem 'pg', '~> 0.17.1'
gem 'rack-flash3'
gem "sinatra"
gem "activerecord"
gem "sinatra-activerecord"
gem "geocoder"
gem 'omniauth'
gem 'omniauth-google-oauth2'
gem "sqlite3"
gem "shotgun"
The model (which has float latitude and longitude columns) that I want to search by:
class Item < ActiveRecord::Base
extend Geocoder::Model::ActiveRecord
attr_accessor :latitude, :longitude
belongs_to :profile
has_and_belongs_to_many :categories
has_many :reports, dependent: :destroy
}
Here's the app.rb code with the '/' route:
class FL < Sinatra::Base
get '/' do
#items = Item.near('Detroit, MI, US')
puts "ITEMS ARE:"
pp #items
erb :index
end
...
end
Here's the relevant app.rb contents:
require 'rubygems'
require 'sinatra/base'
require 'sinatra/flash'
require 'sinatra/activerecord'
require 'geocoder'
require 'omniauth'
require 'sinatra/flash'
require 'json'
require 'pp'
require 'rack-flash'
require './models/model_init'
require './helpers/helper'
require './auth'
require './admin'
require './api'
class FL < Sinatra::Base
set :root, File.dirname(__FILE__)
enable :logging
enable :sessions
#set :logging, true
register Sinatra::ActiveRecordExtension
register Sinatra::Flash
set :show_exceptions, true if ENV['RACK_ENV'] == 'development'
use Rack::Session::Cookie, :secret => ENV['RACK_COOKIE_SECRET']
end
Finally, here is the error i receive:
NoMethodError - undefined method `near' for #<Class:0x0000010750d0b8>:
/Users/bob/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.0/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
/Users/bob/rails_projects/fl2/api.rb:21:in `block in <class:FL>'
/Users/bob/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1603:in `call'
/Users/bob/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1603:in `block in compile!'
/Users/bob/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:966:in `[]'
/Users/bob/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:966:in `block (3 levels) in route!'
Looking at the stacktrace, it looks like its not finding the geocoder package for some reason. I'm not sure if if it's a simple configuration that I'm missing, or if what I'm attempting isn't possible without some gem customization (something beyond my understanding at this point). Any insight, suggestions to try, or examples would be much appreciated. thanks!
Adding my config.ru:
config.ru
require 'bundler/setup'
Bundler.require(:default)
use Bundler.setup(:default) #added this by suggestion
require 'logger'
use Rack::Deflater
Dotenv.load
require "./fl_app"
run FL

I found a solution to this issue, but can't explain why it works. I had to add the line:
reverse_geocoded_by :latitude, :longitude
to my Item model. Once I added that line I was able to use geocoder's 'near' method.
Even though I'm not actually doing any geocoding (converting lat/long to addresses, or vice versa), it still appears to be necessary for the library to work correctly. Thanks #iain for all your helpful advice.

Related

cucumber test gives error - Selenium::WebDriver::Error::JavascriptError

i have a tinymca's iframe inside my page and i want to fill_in that tinymca editor with the cucumber test. whenever i run my test it gives me error that jQuery not defined
in my Gemfile
source 'http://rubygems.org'
ruby '2.0.0'
gem 'capybara'
gem 'capybara-mechanize', :git => 'git://github.com/JerryWho/capybara-mechanize.git', :branch => 'relative-redirects'
gem 'rspec'
gem 'cucumber'
gem 'launchy'
gem 'pry'
gem 'selenium-webdriver'
here is my scenario
Scenario: admin puts all the valid and require data
when Job added with all the valid and require data
Then I should see the success message.
and here is the steps for that test
Given(/^I am logged in as a company admin$/) do
visit('/')
fill_in "log", :with => "admin#email.com"
fill_in "pwd", :with => "password"
click_button "submit"
end
When(/^Job added with all the valid and require data$/) do
visit('/site/admin/posts/add/')
within_frame 'questiontextarea_ifr' do
page.execute_script("jQuery(tinymce.editors[0].setContent('my content hersssssssssse'))")
end
click_button "Save"
end
Then(/^I should see the success message\.$/) do
page.should have_content('Success Your post has been successfully added.')
end
but it gives me error jQuery is not defined (Selenium::WebDriver::Error::JavascriptError)
It's most likely because "jQuery is not defined", as the message suggested.
Please try call without jQuery, but with native TinyMCE API:
page.execute_script("tinyMCE.activeEditor.setContent('my content hersssssssssse')")
Further reading: Test WYSIWYG editors using Selenium WebDriver in pure Ruby (not Capybara)

Sinatra: Template engine not found: prawn

I'm a newbie with Sinatra and prawn. I succeeded with erb and xmm/builder templates. Now trying to get pdf generation with prawn working.
Error received: Template engine not found: prawn
Code:
require 'rubygems'
require 'sinatra'
require 'sinatra/prawn'
set :prawn, { :page_layout => :portrait }
get '/pdf' do
content_type 'application/pdf'
prawn :pdf1
end
Thanks.
Found it: The gem from sbfaulkner doesn't work with current version of Sinatra.
Fix: install the forked gem from danielberkompas as follows:
gem install dberkom-sinatra-prawn -s http://gems.github.com
See issue 6
Then the above code works fine.

Padrino + sinatra-assetpack not working

I'm trying to combine Padrino with Sinatra-Assetpack, without success.
This is my Gemfile:
source :rubygems
gem 'rake'
gem 'sinatra-flash', :require => 'sinatra/flash'
# Component requirements
gem 'haml'
# Assets requirements
gem 'sinatra-assetpack', :require => 'sinatra/assetpack'
# Test requirements
# Padrino Stable Gem
gem 'padrino', '0.10.6'
in my app/app.rb file I set:
require 'sinatra/assetpack'
class Coffee < Padrino::Application
register Padrino::Rendering
register Padrino::Mailer
register Padrino::Helpers
register Sinatra::AssetPack
assets {
serve '/js', from: '/app/assets/javascripts'
serve '/css', from: '/app/assets/stylesheets'
css :main, ['/css/main.css']
js :application, ['/js/application.js']
}
enable :sessions
end
my javascript files are in /app/assets/javascripts and css files in /app/assets/stylesheets, but Padrino respond with a 404 for both /css/main.css and /js/application.js
Any ideas?
Thanks
Figured out the issue, in my application anyway, but from the looks of your app.rb code it's probably the same for you;
Assetpack serves files from the directories you specify in your serve calls, relative to your application's root. In padrino, the application root is yourapplication/app, so if you tell assetpack to serve css from /app/assets/stylesheets for instance, its really looking for the files in yourapplication/app/app/assets/stylesheets.
The second part of the problem was that in the AssetPack docs, it shows the code
set :root, File.dirname(__FILE__)
before the register Sinatra::AssetPack line, which I assume is setting the application's root directory properly so that AssetPack will look in the root application directory instead of app. However, even If I modified that call to set to go up one directory from the app.rb file (since it sits in the app dir in Padrino), it didn't seem to have any effect on AssetPack.
In short, modifying the from: paths in the `serve' calls to be relative to your app directory should fix the problem. In your case, they should be:
serve '/js', from: '/assets/javascripts'
serve '/css', from: '/assets/stylesheets'

Pickle with Sinatra?

I'm super new to ruby web development and have been experimenting with Sinatra. I like what I've seen of Cucumber/Pickle for BDD style testing. I've got Cucumber configured and working but am now stumped on integrating Pickle. Is it compatible with Sinatra? All the examples I've seen on the web are focused on Rails mainly.
You could grab the templates the pickle rails generator uses and tailor them to your needs.
From what the generator does, you'd need a features/support/pickle.rb file like this:
require 'pickle/world'
# Example of configuring pickle:
#
# Pickle.configure do |config|
# config.adapters = [:machinist]
# config.map 'I', 'myself', 'me', 'my', :to => 'user: "me"'
# end
# require 'pickle/path/world'
# require 'pickle/email/world'
And you'd need to pull in the pickle_steps.rb into your steps directory. There's also templates for paths & for email specific steps.
Does that help?

Sinatra and Haml: strange behaviour after gem update

I've made a site using Haml and Sinatra. After the update (I guess it was after that) the site didn't work any more; here is a minimal example:
/app.rb:
require 'rubygems' if RUBY_VERSION < '1.9'
require 'sinatra'
require 'haml'
get "/" do
haml :index
end
/views/layout.haml
!!!
%html{ :xmlns => "http://ww.w3.org/1999/xhtml", :lang => "en", "xml:lang" => "en" }
%head
%title test
%body
= yield
/view/index.haml
%p test
and it throws me the following exception:
/usr/lib/ruby/gems/1.9.1/gems/tilt-1.3.2/lib/tilt/template.rb in initialize
raise ArgumentError, "file or block required" if (#file || block).nil?
/usr/lib/ruby/gems/1.9.1/gems/sinatra-1.3.0.a/lib/sinatra/base.rb in new
template.new(path, 1, options)
/usr/lib/ruby/gems/1.9.1/gems/sinatra-1.3.0.a/lib/sinatra/base.rb in block in compile_template
template.new(path, 1, options)
/usr/lib/ruby/gems/1.9.1/gems/tilt-1.3.2/lib/tilt.rb in fetch
#cache[key] ||= yield
/usr/lib/ruby/gems/1.9.1/gems/sinatra-1.3.0.a/lib/sinatra/base.rb in compile_template
template_cache.fetch engine, data, options do
/usr/lib/ruby/gems/1.9.1/gems/sinatra-1.3.0.a/lib/sinatra/base.rb in render
template = compile_template(engine, data, options, views)
/usr/lib/ruby/gems/1.9.1/gems/sinatra-1.3.0.a/lib/sinatra/base.rb in haml
render :haml, template, options, locals
I haven't really found a way to fix it, does anyone know how to interpret it?
I get the same error with Sinatra 1.3.0.a (the version you're using, which I assume is a release candidate and not a full release), but updating to the current latest (1.3.0.e) fixes it, as does downgrading to the latest stable release (1.2.6). So your answer is upgrade or downgrade.
You can load a specific version of a gem using:
gem "sinatra", "=1.2.6"
before you call require "sinatra", or you could look into using Bundler (which uses the same syntax).