Getting Rails to play with Hpricot - ruby-on-rails

I'm trying to get Hpricot working with Rails on my dev machine.
I've installed Hpricot [0.8.1] using the standard 'gem install hpricot' and confirmed it works fine with my standard Ruby installation [1.8.7]; however when I try the same with my Rails [2.1.0] installation, I get an error -
TypeError: superclass mismatch for class BogusETag from /usr/lib/ruby/1.8/hpricot/tag.rb:130
Seems like there's some kind of conflict, but googling the error hasn't turned up any useful information.
Any ideas ? Thanks in advance.

If you're free to choose your HTML parsing library, switch it.
Why, the creator of Hpricot, recently posted that you should better use Nokogiri instead of HPricot, nowadays.

This error occurs because there is no compiled library for the platform. To solve this for your current hpricot version, go to your rails directory dir and do the following (this assumes you are using an unpacked gem - this problem wouldn't arrise otherwise, unless your OS has been upgraded since installing the gem):
cd vendor/gems/hpricot-0.6/ext/hpricot_scan/
ruby extconf.rb
make
Then copy the compiled library to the correct platform dir for your system. Each version of OS X has a slightly different platform name, so mine (Snow Leopard) appears as:
ruby-1.8.6-p383 > RUBY_PLATFORM
=> "i686-darwin10.0.0"
This means I needed to copy the file "hpricot_scan.bundle" (OS X uses bundles for shared libraries, on Linux that would be "hpricot_scan.so") to the following directory, creating it if necessary:
vendor/gems/hpricot-0.6/lib/i686-darwin10.0.0/
Substitute the appropriate platform name (puts RUBY_PLATFORM from irb) for your machine.

Try using Hpricot in a irb session, because i don't think this should generally happen
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'hpricot'
=> true
irb(main):003:0> doc=Hpricot.parse("<html><head><title>test</title></head><body> Wooo Hooo </body></html>")
=> # {elem {elem "test" } } {elem " Wooo Hooo " } }>
irb(main):004:0> doc.search('title')
=> # "test" }]>
irb(main):005:0> doc.search('title').text
=> "test"
That means your Hpricot is working fine. And the problem is in way you are using it in Rails. ( It would be easier if you could paste your code ).
I had few issues with Hpricot(0.8.1) earlier, had to switch back to 0.6.164 version. you can try that if you want.
HTH

Execute Below command and its works
gem install hpricot --platform=mswin32

Related

An error occured while installing wkhtmtopdf-heroku

Heroku doesn't allow me to put airbrake and wkhtmlpdf-heroku gems together. My previous push works fine without airbrake. If I put airbrake again in my gemfile, the error below results:
Bundler::GemspecError: Could not read gem at /tmp/build_li34yprz7arq/vendor/bundle/ruby/1.9.1/cache/wkhtmltopdf-heroku-1.0.0.gem. It may be corrupted.
An error occurred while installing wkhtmltopdf-heroku (1.0.0), and Bundler
cannot continue.
Do you know any reason why that's happening? Thanks!
The wkhtmltopdf-heroku gem just drops a wkhtmltopdf binary in your project and sets up configs for common 'client' gems. You can just do that yourself manually. I say this because I had some other odd issues tied to that gem that went away mysteriously when I just did setup for myself.
So try just dropping wkhtmltopdf-amd64 in your projects bin directory and do your own config. In my case, for wicked_pdf, that meant a wicked_pdf.rb in /initializers containing
WickedPdf.config = { :exe_path => (Rails.env.test? || Rails.env.development? ? 'c:\Program Files\wkhtmltopdf\wkhtmltopdf.exe' : Rails.root.join('bin', 'wkhtmltopdf-amd64').to_s),
:page_size => 'Letter',
:outline => {:outline => true, :outline_depth => 4}}
Just to clarify, that is pointing to a windows binary on my C drive for test and production, and otherwise (meaning production) pointing to the amd-64 binary (for Heroku) which I have in the project's bin directory.

Problem with Ruby + rtaglib gem

I need to read ID3 tags from MP3 files, so i did some research and found that rtaglib is the way to go. The other plugins seems outdated, i tried them anyways but none of them work for me.
After installed the rtaglib gem (the ruby binding for TagLib http://developer.kde.org/~wheeler/taglib.html#bindings) i just can't make it work.
I tried adding both the gem to the gemfile and install it with sudo gem install rtaglib. I get the response like it's installed, but after that when i tried to require any of the two files i get:
?> require 'tagfile/tagfile'
LoadError: no such file to load -- tagfile/tagfile
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:239:in require'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:239:inrequire'
Or if i tried TagLib:
require 'TagLib'
=> nil
I have tried to move the files manually from the zip file but that doesn't zip to work neither.
Thank you.
If you are using Ubuntu you must install libtagc0-dev first, THEN install the gem.
At first I made the mistake of installing libtag1-dev, which gave me the same problem as you have.
Make sure that you are including rubygems as well.
If you are doing this from a Rake task or ruby script:
require 'rubygems'
require 'tagfile/tagfile'
I am doing this on OSX with the gem and taglib installed via homebrew and it works fine.

Can't activate gem: how do I figure out which gem has a dependency on this one?

I'm getting the following error:
Gem::Exception: can't activate hpricot (= 0.6.161, runtime),
already activated hpricot-0.8.3
0.6 is installed locally, 0.8.3 is frozen in my app.
This is my "stacktrace":
Loading production environment (Rails 2.3.10)
/software/ruby-ror-gem-1.3.1/lib/rubygems.rb:149:in `activate':Gem::Exception: can't activate hpricot (= 0.6.161, runtime), already activated hpricot-0.8.3
/e/app/www.example.com/rails/releases/20101117142713/vendor/rails/railties/lib/console_with_helpers.rb:5:NameError: uninitialized constant ApplicationController
How do I figure out which gem or library has a dependency on hpricot 0.6?
I've added the output of user438962's command below:
{"daemons-1.0.9"=>[],
"scgi_dp_rails-0.0.5"=>["preforkdp", "daemons"],
"rails-2.3.10"=>[],
"rwfd-0.1.0"=>[],
"nokogiri-1.3.2"=>["racc", "rexical", "rake-compiler", "hoe"],
"activesupport-2.3.10"=>[],
"rack-1.0.1"=>
["test-spec",
"camping",
"fcgi",
"memcache-client",
"mongrel",
"ruby-openid",
"thin"],
"rack-1.1.0"=>
["test-spec", "camping", "fcgi", "memcache-client", "mongrel", "thin"],
"preforkdp-0.1.2"=>["rwfd"],
"activerecord-2.3.10"=>[],
"hpricot-0.6.161"=>[],
"cgi_multipart_eof_fix-2.5.0"=>[],
"fastthread-1.0.1"=>[],
"gem_plugin-0.2.3"=>[],
"activeresource-2.3.10"=>[],
"ferret-0.11.6"=>["rake"],
"mysql-2.7"=>[],
"actionmailer-2.3.10"=>[],
"actionpack-2.3.10"=>[],
"hpricot-0.8.3"=>[],
"mongrel_upload_progress-0.2.2"=>["mongrel", "gem_plugin"],
"mongrel-1.1.3"=>
["gem_plugin", "daemons", "fastthread", "cgi_multipart_eof_fix"],
"mongrel_cluster-1.0.5"=>["gem_plugin", "mongrel"],
"rake-0.8.4"=>[],
"haml-2.0.9"=>[],
"remvee-mini_magick-1.2.3.4.0"=>[]}
If you use Bundler, you avoid this problem and you have the really great command : bundle viz
This Command generate a graph with all dependencies.
require 'rubygems'
require 'pp'
h = {}
Gem.source_index.each{|g, spec| h[g] = spec.dependencies.map{|d| d.name} }
pp h
I found that rfeedparser is the gem that uses hpricot 0.6.
The problem is that this version (0.6) works with rfeedparser, but emits the warning "Passing no parameters to XML::SaxParser.new is deprecated."
Well, with newer versions of hpricot (at least 0.8.2), this warning seems to have taken effect, because now having that version of hpricot or newer causes "ArgumentError: wrong number of arguments (1 for 0)" when calling FeedParser.parse(url).
Unfortunately, I've found that the only way to make this work with legacy Rails (we have a 2.2.2 app) is to uninstall any hpricot version that's not 0.6.
The issue will be that one of the gems you use depends on hpricot version 0.6.161
and so tries to load it, but that you already have hpricot-0.8.3 loaded. The more recent version might be loaded if you're using hpricot yourself, and have required hpricot for use without specifying a version. If that's the case, you can change your own require to be the same version as the other gem uses (0.6.161).
The other reason the more recent version of hpricot is being loaded, could be that another gem you use depends on that version of hpricot, meaning you can't use the specific versions of those two gems you have because they have clashing requirements for their hpricot version.
Use the methods other users have posted to help you track down the dependencies of the gems you are using.
You could try grepping your load path:
$:.each do |dir|
cmd = %(grep -r hpricot #{dir})
puts cmd
puts `#{cmd}`
end

uninitialized constant on class from ruby gem

I am trying to implement a gem called stanfordparser which can be found here: http://stanfordparser.rubyforge.org/
It is a ruby wrapper for a java natural language parser
I am developing in netbeans using ruby on rails / jruby on a windows 7 machine. My web app works fine otherwise, but when I try to add the parser wrapper it breaks.
Here is the code that is causing a problem:
gem 'stanfordparser'
def show
parser = StanfordParser::LexicalizedParser.new
#words = parser.apply("This is a sentence.")
end
this is in the taskscontroller
and when I go to tasks/show (which, if i remove this code, works fine) I get the following error
uninitialized constant TasksController::StanfordParser
I have made sure the gem is installed in netbeans
I am very new to ruby on rails, and teaching myself, so it may be something obvious
Thanks!
EDIT: I checked my glassfish server logs and it says
SEVERE: Missing these required gems:
stanfordparser
which is weird because I've installed the gem using netbeans, I've done rake gems:install and netbeans says the gem is installed. I've checked in netbeans gems folder and the gem is installed there.
EDIT 2:
So, after a lot of research and head banging, I've decided to simplify things a bit by just trying to use jruby to implement the java classes, now I need to figure out how to import the stanfordparser java classes (there are at least 50), I think I need to compress all the classes into a jar so that jruby can load it. maybe.
If you are using Rails 3 then the gem 'stanfordparser' statement needs to be specified in Bundler's Gemfile within the project's root. Otherwise, for Rails 2.x you need a config.gem 'stanfordparser' statement within config/environment.rb.
I was able to solve my problem the following way:
instead of using the stanfordparser ruby wrapper (which implements java ruby bridge to connect the java stanford parser to pure ruby), I use jruby to just implement the java from the stanford parser.
the code that ended up working:
include Java
require 'C:\\Stanford-Parser\\Current\\stanford-parser.jar'
require 'rubygems'
include_class 'edu.stanford.nlp.parser.lexparser.LexicalizedParser'
lp = LexicalizedParser.new(args) #args is the arguments, not copied here

How do I get an installed ruby gem included in Rails?

I am attempting to get a gem I installed working in a Rails application. I can require the gem just fine in a Ruby program that I run from the command line using:
require 'nokogiri'
But when I attempt to do the same in one of my Rails controllers it errors saying "no such file to load -- nokogiri".
I tried using the full path to the lib/nokogiri.rb file, but that fails because it cannot find "nokogiri/native".
Better, place the following in your environment.rb file:
Rails::Initializer.run do |config|
...
config.gem :nokogiri
...
end
This will tell Rails that you depend on that particular gem. It also allows you to specify particular versions, and it will automatically keep all your gems synched, or unpack them into vendor/gems if you so wish.
I had a similar error but simply forgot to put the following in my environment.rb file: (note the quoted "nokogiri")
Rails::Initializer.run do |config|
...
config.gem "nokogiri"
...
end
Ok I figured it out. This is going to sound pretty stupid...but oh well...
It turns out I had two installations of ruby on my machine. I use InstantRails to serve my test applications and it comes prepackaged with an installation of ruby. I had another installation however outside of this and it was here that nokogiri had been installed, not in the installation in InstantRails.
In any case they were looking in different spots for the gems.
Try the following
require 'rubygems'
gem 'nokogiri'
If you are on some form of *nix then did you get any errors when you installed the gem, particularly errors stating that the gem was not on the path. This may happen if you have installed the gem as yourself rather than as root and you do not have your personal gem library in your gem path.
If you always install your gems using
sudo gem install some_gem_name
then you should not get that problem.

Resources