I am trying to access the insides of an ODT file. I'll run it through IRB and it will work perfectly fine but when I try and write a script to do it, it fails with this error:
./replace_odf.rb:3:in `require': no such file to load -- rubygems (LoadError)
from ./replace_odf.rb:3
Here is my code when ran through IRB. As you can see towards the end, it can access the file.
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'zip/zip'
=> true
irb(main):003:0> odt = Zip::ZipFile.open('java.odt')
=> java.odt
irb(main):004:0> odt.entries.each do |entry|
irb(main):005:1* puts entry.name
irb(main):006:1> end
mimetype
Configurations2/statusbar/
Configurations2/accelerator/current.xml
Configurations2/floater/
... etc
Here is my script code. When ran, it gives the error posted above.
require 'rubygems'
require 'zip/zip'
require 'rexml/document'
odt = Zip::ZipFile.open('java.odt')
file1 = odt.entries[0]
odt.entries.each do |entry|
puts entry.name if entry.name =~ /\.xml$/
end
puts odt.read("mimetype")
xml = odt.read("content.xml")
doc = REXML::Document.new(xml)
doc.root.each_element do |o|
o.each_element do |i|
puts i
end
end
Related
I have the following code, which creates a file with the content from a crawled site:
require 'rubygems'
require 'anemone'
require 'nokogiri'
require 'open-uri'
Anemone.crawl("http://www.findbrowsenodes.com/", :delay => 3) do |anemone|
anemone.on_pages_like(/http:\/\/www.findbrowsenodes.com\/us\/.+\/[\d]*/) do | page |
doc = Nokogiri::HTML(open(page.url))
node_id = doc.at_css("#n_info #clipnode").text unless doc.at_css("#n_info #clipnode").nil?
node_name = doc.at_css("#n_info .node_name").text unless doc.at_css("#n_info .node_name").nil?
node_url = page.url
open("filename.txt", "a") do |f|
f.puts "#{node_id}\t#{node_name}\t#{node_url}"
end
end
end
Now I want to create not one but various files named node_id. I tried this:
page.each do |p|
p.open("#{node_id}.txt", "a") do |f|
f.puts "#{node_id}\t#{node_name}\t#{node_url}"
end
end
but got this:
undefined method `value' for #<Nokogiri::XML::DTD:0x51c089a name="html"> (NoMethodError)
then tried this:
page.open("#{node_id}.txt", "a") do |f|
f.puts "#{node_id}\t#{node_name}\t#{node_url}"
end
but got this:
private method `open' called for #<Anemone::Page:0x91472e8> (NoMethodError)
What's the right way of doing this?
File.open("#{node_id}.txt", "w") do |f|
f.puts "stuff"
end
How you make the assignment to node_id is up to you.
I am new to Ruby and currently working on a JAVA project that has its behavioural test cases written in ruby(CUCUMBER BDD).When I tried to take a build of my project through TeamCity it shows 6 test case failures with
"Watir::Exception::NoMatchingWindowFoundException: Unable to locate window,"
or
"Spec::Expectations::ExpectationNotMetError:"
errors.No changes have been made to these test cases .Any suggestions as to where I should look ?
THis is my env.rb file ..do I need to change anything here .
require 'watir'
require 'open-uri'
require 'spec'
require 'win32ole'
require 'rake'
require 'cucumber/rake/task'
require 'net/http'
require 'uri'
require 'mysql'
require 'active_record'
require 'features/db/mysqldb'
require 'features/db/order'
require 'features/db/listener_state'
def clear_all_mock_requests
open 'http://xxxxxx:8080/httpmockserver/soapResponse!clearAllRequests.action'
end
Mysqldb.connect("xxx", "xxx", "xx", "xxx", "xxxxx")
clear_all_mock_requests()
Watir::Browser.default = ENV['browser'] == 'firefox' ? 'firefox' : 'ie'
#Watir::Browser.default = 'firefox'
BROWSER = Watir::Browser.new
WIN32OLE.class_eval do ||
def visible?;
return false if self.style.invoke('display').downcase == 'none';
return true;
end
def type
{:text_field=>'text', :radio=>'radio', :select_list => 'select-one', :checkbox => 'checkbox'}.each do |method, name|
return name if BROWSER.method(method).call(:name, self.name).exist?
end
return nil
end
def exist?
return true
end
end
NilClass.class_eval do ||
def exist?
return false
end
end
at_exit do
BROWSER.close
end
Watir::Exception::NoMatchingWindowFoundException: Unable to locate window usually means that you're trying to latch onto a browser that does not exist. Check your browser.open functions.
I have a method that zips up files I pass in.
require 'zip/zip'
def zipup(aname, aloc="/tmp/")
Zip::ZipFile.open "#{aloc}"+File.basename(aname)+".zip", Zip::ZipFile::CREATE do |zipfile|
zipfile.add File.basename(aname), aname
end
end
I need to get a string object or array object from this method that has the archive.zip name of every file that has been compressed.
rubyzip does have a to_s method all though I have failed in getting the syntax correct.
http://rubyzip.sourceforge.net/classes/Zip/ZipEntry.html#M000131
thanks from a new rubyist.
Welcome Joey, do you use the 'zip/zip' gem or just 'zip' ? If you require something, better add it to the question next time. This gem needs some extra documentation and methods it seems to me.
This works
require 'zip' #or 'zip/zip' both work
def zip_list(filename)
zipfile = Zip::ZipFile.open(filename)
list = []
zipfile.each { |entry| list << entry.name }
list
end
puts zip_list("c:/temp/zip1.zip")
another way
require 'zip/zip'
Zip::ZipFile.open("c:/temp/zip1.rb.zip") do |zipfile|
zipfile.entries.each do |entry|
puts entry.name
end
end
I am looking for a way to read and parse locally a remote CSV (hosted on a particular website).
I found on the Internet a couple of interesting examples that make use of FasterCSV, that in ruby 1.9.2 has been merged into CSV. I found that you can read a remote CSV using the gems 'csv' and 'open-uri' this way:
require 'csv'
require 'open-uri'
def read(url)
open(url) do |f|
f.each_line do |l|
CSV.parse(l) do |row|
puts row
end
end
end
end
But when I call this function, I get an exception:
ERROR IOError: closed stream
Anyone can explain me why? Is there anything wrong? Should I choose another approach for reading remote CSV's?
Update
The best solution I've found till now is this:
def read(url)
data = []
begin
open(url) do |f|
data = CSV.parse f
end
rescue IOError => e
# Silently catch the exception ...
end
return data
end
but it somewhat seems not so clean. I really do not like silently catching an exception where it shouldn't be ...
Update 2
I can reproduce the error using both
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0]
and
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]
This is the code from my test.rb file:
require 'rubygems'
require 'open-uri'
require 'csv'
def read(url)
data = []
begin
open(url) do |f|
data = CSV.parse f
end
end
puts data
end
read("http://www.euribor-ebf.eu/assets/modules/rateisblue/processed_files/myav_EURIBOR_2011.csv")
And this is the output of the ruby test.rb command
/Users/marzu/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/open-uri.rb:152:in `close': closed stream (IOError)
from /Users/marzu/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/open-uri.rb:152:in `open_uri'
from /Users/marzu/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/open-uri.rb:671:in `open'
from /Users/marzu/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/open-uri.rb:33:in `open'
from test.rb:8:in `read'
from test.rb:16:in `<main>'
I am using rvm 1.6.9 on Mac OS X 10.6.7.
Any suggestions?
On Mac OS X 10.6.7, using ruby r1.9.2, I get the same error as displayed above. But using the following code to read CSV files works for the example URL provided:
require 'rubygems'
require 'open-uri'
require 'csv'
def read(url)
CSV.new(open(url), :headers => :first_row).each do |line|
puts line
puts line[0]
puts line['FEB11']
end
end
read("http://www.euribor-ebf.eu/assets/modules/rateisblue/processed_files/myav_EURIBOR_2011.csv")
I'm sure that I am missing something. Basicaly I want to monitor a logs IO and if a FATAL ERROR is logged to send an email with the error enclosed.
#!/usr/bin/ruby -w
require 'rubygems'
def mailer(line)
date = `date +%D-%T`
f = File.open("/root/error.mail", "w")
f.puts("Subject: Fatal Error on SERVER #{date}\n\n#{line}")
f.close
system("sendmail guy#foo.com.com < /root/error.mail")
end
def fatal_check(file, pattern)
f = File.open(file, "r")
f.seek(0,IO::SEEK_END)
while true do
select([f])
line = f.gets
mailer("#{line}") if line=~pattern
#system("./mailer.rb #{line}") if line=~pattern
end
end
fatal_check("/root/test.log", /FATAL ERROR/)
How about this. You'll need a couple of gems:
gem install file-tail
gem install pony
And then your script:
require 'rubygems'
require 'pony'
require 'file/tail'
def fatal_check(file, pattern)
File::Tail::Logfile.open(file, :backward => 0) do |log|
log.tail do |line|
date = `date +%D-%T`
Pony.mail(:to => 'you#example.com', :from => 'me#example.com', :subject => "There was a nasty error on #{date}", :body => line)
end
end
end
fatal_check(File.dirname(__FILE__) + "/test.log", /FATAL/)