Sinatra: Template engine not found: prawn - sinatra

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.

Related

Installing php extension for SQL Anywhere on server with CentOS, Plesk, php-fpm

we got a new WebServer with CentOS, Plesk and php-fpm.
Now we don't get the php extension running.
What we did:
installed latest SQL Anywhere 17 (ebf29577)
copied php-7.3.0_sqlanywhere_r.so to /opt/plesk/php/7.3/lib64/php/modules/
loading php-7.3.0_sqlanywhere_r.so via /opt/plesk/php/7.3/etc/php.d/sqlanywhere.ini
this leads to an error:
WARNING: [pool plesk-php73-fpm.plesk-service.localdomain] child 11616 said into stderr: "NOTICE: PHP message: PHP Warning: request_startup() for sqlanywhere module failed in Unknown on line 0"
(This leads to php 7.3 fpm is not loading at all)
next try was to switch from FPM to CGI: same error
module is loadable itself within shell if LD_LIBRARY_PATH was enhanced with path /opt/sqlanywhere17/lib64/
some tries getting the cause by strace did not help
Has anybody an idea or maybe even successfully installed php extension.
Thanks
Florian
after a lot of tests we found a solution:
in Plesk Domain php settings we had to add:
[php-fpm-pool-settings]
env[LD_LIBRARY_PATH]="/opt/sqlanywhere_v2/lib64:$LD_LIBRARY_PATH"
Now all libs are found and it works
For me solution was, creating custom handler:
plesk bin php_handler --add -displayname "PHP SQLAnywhere 7.4" -path /opt/php-custom-handlers/7.4/php-sqlanywhere.fcgi -clipath /opt/plesk/php/7.4/bin/php -phpini /opt/plesk/php/7.4/etc/php.ini -type fastcgi
php-sqlanywhere.fcgi contents:
#!/bin/bash
LD_LIBRARY_PATH=/opt/sqlanywhere17/lib64
export LD_LIBRARY_PATH
exec /opt/plesk/php/7.4/bin/php-cgi "$#"
And finally using this handler on domain php settings and adding on additional directives:
extension=sqlanywhere.so

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)

Problems implementing ruby geocoder using 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.

How to configure and use Mailgun's SDK with composer-php?

I installed Composer and a SDK for Mailgun's service. These are the steps i followed:
# current directory
cd ~
# Install Composer
curl -sS https://getcomposer.org/installer | php
# Add Mailgun as a dependency
php composer.phar require mailgun/mailgun-php:~1.7
According to the instructions, all I did after that was (index.php):
<?php
require 'vendor/autoload.php';
use Mailgun\Mailgun;
# First, instantiate the SDK with your API credentials and define your domain.
$mg = new Mailgun("key-my-key-goes-here-987654321");
$domain = "somedomain.com";
Then, I tried to get the list of bounced emails:
$data = $mg->get("$domain/bounces", array('limit' => 15, 'skip' => 0));
var_dump($data);
...and I'm getting this error:
Warning: require(vendor/autoload.php): failed to open stream: No such
file or directory in /var/www/html/index.php on line 2 Fatal error:
require(): Failed opening required 'vendor/autoload.php'
(include_path='.:/usr/share/pear:/usr/share/php') in
/var/www/html/index.php on line 2
So I'm guessing it has something to do with composer's installation/configuration perhaps? Thanks for any help...
The way you programmed it, you must have the following files all in the same directory:
composer.json
index.php (your test script)
And you must have run the composer require command while being inside this directory. This will also create a directory named vendor here, and add plenty of files, amongst them vendor/autoload.php.
If however your test script isn't in this location, the require call will not find the file where you tell PHP to find it. This isn't any failure of Composer, but simply the fact that you have to include that file according to your situation, not by copy&paste code. If you change the path of your test script, you have to change the path of the vendor directory as well.

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).