scrapi doesn't see tidy libraries - ruby

I have a simple ruby file that scraps a price off of walmart's site. I did a gem install scrapi and a gem install tidy. When I run my code on my windows 7 box I get the following error:
Error
c:/Ruby193/lib/ruby/gems/1.9.1/gems/scrapi-2.0.0/lib/scraper/reader.rb:216:in `r escue in parse_page': Scraper::Reader::HTMLParseError: didn't find tidy libs on your system. Please install tidy (http://tidy.sourceforge.net/) (Scraper::Reader ::HTMLParseError)
It appears that it can't find the tidy libraries, so I put it in my path:
Path=C:\Ruby193\lib\ruby\gems\1.9.1\gems\tidy_ffi-0.1.5\lib;C:\Ruby193\lib\ruby\gems\1.9.1\gems\tidy-1.1.2\lib
but it still throws the same can't find tidy libs error.
Thanks in advance for your help.
Code
require 'rubygems'
require 'scrapi'
require 'tidy'
scraper = Scraper.define do
process "div.firstRow div.priceAvail>div>div.PriceCompare>div.BodyS", :price => :text
result :price
end
url = URI.parse("http://www.walmart.com/search/search-ng.do?search_constraint=0&ic=48_0&search_query=LOST+third+season&Find.x=17&Find.y=1&Find=Find")
puts scraper.scrape(url)

I have exactly the same problem, but on Linux.
It is something to do with the find_tidy function in reader.rb - somehow it is not able find the libraries. I just went and commented that line, and it is working fine now.
# Make sure the Tidy path is set and always apply the default
# options (these only control things like errors, output type).
# find_tidy
options = (options || {}).update(TIDY_OPTIONS)
Try it if you are sure you have the tidy libraries in your system.

I had the same problem on a mac.
The offensive code is on line 204 of the reader.rb file. Open it in your favorite editor:
atom /usr/local/lib/ruby/gems/2.2.0/gems/scrapi-2.0.0/lib/scraper/reader.rb
Just comment it out like the previous answer said. Works great after that.

Related

CLI tool built using commander gem doesn't execute correctly when used after being installed

So, I have a CLI tool I'm building using the commander gem.
The executable successfully executes correctly when used directly from the bin folder (bin/dynamised), but when I install the gem locally and then run it from the command line (dynamised) it doesn't seem to do anything.
If I add puts 'WORKING' to the top of the file, I see that but nothing else.
EDIT:
output of puts [$0, __FILE__].inspect:
from bin:
["bin/dynamised", "bin/dynamised"]
from installed gem:
["/Users/---------/.rbenv/versions/2.3.0/bin/dynamised", "/Users/---------/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/Dynamised-0.1.4/bin/dynamised"]
Link to gist containing executable.
Not quite sure what's wrong.
Change the very last line of your script to:
Dynamised::CLI.new.run if File.basename($0) == File.basename(__FILE__)
or simply remove this redundant check:
Dynamised::CLI.new.run

What is a good way to see output from my ruby gem?

I want to know a good way to run code in my ruby gem (and not just through tests).
That means I want to run ruby lib/{gemname}.rb on the terminal and be able to see some output
So I have this line in my base file:
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
and then I load a file called debugger.rb by requiring it at the bottom of the base file. This file then gives me the output I need.
This works but this kind of clutters my code and I don't want to accidentally commit it and watch it break in production.
So what's a good way of doing this?
My approach for developing and debugging gems has two parts:
1) Always use require_relative to include needed "internal" gem related files. This allows the gem to be loaded up normally when installed as a gem and also in my development environment (ignoring any versions of the gem that may already be installed.)
2) Then use the following snippet of code (usually) at the bottom of the main file, to activate debugging features when the base my_gem.rb file is run explicitly on the command line.
if __FILE__ == $0
#debugging code goes here!
end
With this strategy there's no need to worry about yanking debug code before releasing the gem.

RubyGems - require, file location and (load error) complications

very new to coding so, having exhausted Google and Stack Overflow, would really appreciate some advice...
I am currently building a web-scraper to get familiar with CMD vs Sublime Text, feeling Ruby in action; So i am working my way through this tutorial
After having actioned in CMD
C:\gem install HTTParty
SUBLIME TEXT - starts with this code:
require_relative 'HTTParty'
require_relative 'Nokogiri'
etc
But before i can get to anything more from CMD, i hit web_scraper.rb and it returns with:
C:/Users/ATH18/Desktop/nokogiri_tutorial/web_scraper.rb:1:in `require_relative': cannot load such file -- C:/Users/ATH18/Desktop/nokogiri_tutorial/httparty (LoadError)
from C:/Users/ATH18/Desktop/nokogiri_tutorial/web_scraper.rb:1:in `<main>'
[Finished in 0.1s with exit code 1]
I think this has to be due to one of the following:
i) maybe gems have to have their actual files dragged into whatever folder you're creating a new program in?
ii) i'm missing another piece of information that would let it run properly?
iii) perhaps there's another way to tell CMD/ruby that the "require"d gem is not in the current folder (I read this somewhere but their advice didnt seem to work either).
NOTE - i have done gem install xxxxxx in both the C:\ directory and C:\users\desktop\projectFolder\
Help?
You have to use require instead of require_relative. The difference between both a is explained here: https://stackoverflow.com/a/3672600/92049
Use require 'GEMNAME' for gems installed with gem install GEMNAME; use require_relative 'PATH' to require a file relative to the file containing require_relative. (Most often you will find yourself using require.)
To come back to your question: As it says in the tutorial, you have to write require 'HTTParty' instead of require_relative 'HTTParty'.
Does this answer your original question?

Sinatra cannot find views on Ruby 1.9.2-p0

I'm quite new to Ruby language (up to now I developed in Groovy + Grails) but since I was curious about it I wanted to try Sinatra on Ruby 1.9.2-p0.
I have a trivial website that is contained in /mywebpage and has 2 files:
# blog.rb
get '/' do
'Hello World!'
end
get '/impossible' do
haml :index
end
and
#config.ru
path = File.expand_path "../", __FILE__
$LOAD_PATH << (File.expand_path ".") + "/views"
require 'haml'
require 'sinatra'
require "#{path}/blog"
run Sinatra::Application
then in the same folder I have a /views/ folder that contains index.haml.
I try to run the server with rackup -p8080 but when I try to get /impossible I receive the following error:
Errno::ENOENT at /impossible
No such file or directory - /home/jack/mywebpage/<internal:lib/rubygems/views/index.haml
By searching over internet it seems that this maybe caused by "." not being included in $LOAD_PATH so I tried to add it or add directly views ./views so that actually $LOAD_PATH.inspect gives me correct path: ..., "/home/jack/mywebpage/views"]
But still it doesn't seem to work. Being quite new to the framework and the language I was wondering if I'm doing something wrong. any clues?
Running Sinatra with Ruby 1.9.2 the template directory is no longer implicitly 'views', you need to set it yourself.
set :views, File.dirname(__FILE__) + "/views"
Note that currently Ruby also has Kernel#__dir__() method that is equivalent to File.dirname(__FILE__).
This, and other issues with 1.9, will be have been solved in Sinatra 1.1. You could use this fork: http://github.com/rkh/sinatra/tree/1.1
I ran into a similar problem, and solved it like this. I didn't dig into the problem, but this is what I found and it works. It'll supposedly be fixed in the next version of Sinatra (which they should really get out the door, just to fix these few 1.9.2 bugs).
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
enable :run
get '/' do
"Hello, world!"
end
Edit: It seems there are multiple bugs with Sinatra on 1.9.2. This one will fix Sinatra apps not starting on 1.9.2. I don't use a views directory (I like to keep my apps single-file), so I didn't run into your particular problem. This fix most likely won't help you at all. I probably should have read your problem more closely..
gem install sinatra --pre
I ran into that last week and didn't find a suitable fix on the Sinatra site short of tweaking the sinatra code. I'm using rvm for my development and switched to try sinatra on Ruby 1.8.7 and it works fine again, so that's where I left it.
Oh, since you're new to Ruby, you might not know about rvm, so here's the lowdown. RVM is Mac only and highly recommended for managing your Ruby version and gems. It makes it trivial to have multiple Ruby versions and alternate groups of gems for development and testing. Everything is stored in your ~/.rvm directory so it's easy to blow it all away if you need to.
http://rvm.beginrescueend.com/
I just looked at the Sinatra site again about the problem to see if there was anything new, but it appears they consider the following to be an acceptable fix:
http://github.com/sinatra/sinatra/issues/#issue/50
I'm a bit adverse to having to edit the source of Sinatra as recommended by issue #50, but it's not real hard to do. I'd like to see them put out an update so we'd have an official fix but I haven't seen anything yet:
gem env will tell you the "GEM PATHS". Sinatra's gem will be in one of those. The line mentioned in issue #50 goes into base.rb. On my machine it's something like ...gems/ruby-1.9.2-p0/gems/sinatra-1.0/lib/sinatra/base.rb.
Insert:
/<internal:/, # ruby 1.9.2-p0 hacks
at line 1020.
Save the file and you should be good to go.

Ruby "No Such File To Load - sqlite3" on OS X

I was trying to create a quick little script that would insert data into an SQLite db for me but I can't get past the first few steps.
I have done "sudo gem install sqlite3-ruby", my Ruby version is 1.8.7 (I used the Ruby one click installer from rubyforge.org).
My script is incredibly simple. It looks like this:
#!/usr/bin/ruby -w
require "csv.rb"
require "sqlite3"
begin
CSV.open('updateddata.sql', 'r') do |row|
p row
end
rescue => err
puts "Exception : #{err}"
err
end
It never makes it past the line require "sqlite3". It just errors and tells me that it can't find that file to load.
I don't understand how it won't work even after using the One click installer (which is supposed to have SQLite built into the install).
I'm not even sure where to go from here.
I am not a Ruby developer at all, I just wanted to use it as a learning experience and to quickly complete this task for myself.
Dunno if Ruby in OS X behaves differently, but normally you need to do require 'rubygems' before requiring any gems.
You can also add
export RUBYOPT=rubygems
to your profile.
http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/43fc65132487f98e/?pli=1 using sqlite3-ruby gem solved my issue on this

Resources