trying to include sunlight congress gem in my project - ruby

So I am simply trying to run this code below with sunlight congress gem, but I seem to be doing something wrong. I am trying to include the gem in the project however something is off.
I am trying to implement this using RubyMine 7.1.2(if version matters), Ruby 2.2.2.
sunlight congress can be found here: https://github.com/sunlightlabs/ruby-sunlight.
Also, I am working on windows 8.1.
require 'csv'
require 'sunlight-congress'
require 'erb'
Sunlight::Congress.api_key = "e179a6973728c4dd3fb1204283aaccb5"
def neat_zip(zipcode)
zipcode.to_s.rjust(5,"0")[0..4]
end
def legislator_by_zip(zipcode)
Sunlight::Congress::Legislator.by_zipcode(zipcode)
end
def save_thank_you_letters(id,form_letter)
Dir.mkdir("output") unless Dir.exists?("output")
filename = "output/thanks_#{id}.html"
File.open(filename,'w') do |file|
file.puts form_letter
end
end
puts "EventManager initialized."
contents = CSV.open 'event_attendees.csv', headers: true, header_converters: :symbol
template_letter = File.read "form_letter.erb"
erb_template = ERB.new template_letter
contents.each do |row|
id = row[0]
name = row[:first_name]
zipcode = neat_zip(row[:zipcode])
legislators = legislator_by_zip(zipcode)
form_letter = erb_template.result(binding)
save_thank_you_letters(id,form_letter)
end
I am getting this error:
C:\Ruby22\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:/Ruby22/event_manager/sunlight-congress-master/lib/event_manager.rb
C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- sunlight-congress (LoadError)
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/event_manager/sunlight-congress-master/lib/event_manager.rb:2:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
Do i simply have the path wrong? gem isnt installed?

You need to gem install sunlight-congress.
Update: Resources to install gems in Windows / RubyMine
Installing gems with RubyMine
Installing Ruby Gem in Windows
http://rubyinstaller.org/

Related

`require': cannot load such file -- curses (LoadError)

I am a ruby beginner and trying to understand and learn ruby. I came across this error while trying to run a ruby file which uses "require curses" but I am getting the following error while doing so. Any help or hint is appreciated.
desktop$ ruby curses.rb
/desktop/jsipp-master$ gem list
*** LOCAL GEMS ***
bigdecimal (1.2.8)
CFPropertyList (2.2.8)
curses (1.2.4)
did_you_mean (1.0.0)
enumerate (0.0.7)
enumerated (1.0.1)
enumeration (1.3.3)
ffi (1.9.25)
ffi-rzmq (2.0.6)
ffi-rzmq-core (1.0.6)
io-console (0.4.5)
json (1.8.3)
libxml-ruby (2.9.0)
minitest (5.8.5)
net-telnet (0.1.1)
nokogiri (1.5.6)
power_assert (0.2.6)
psych (2.1.0)
rake (10.4.2)
rdoc (4.2.1)
sqlite3 (1.3.11)
test-unit (3.1.5)
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- curses (LoadError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from curses.rb:3:in `<main>'
jsipp-master$ ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [universal.x86_64-darwin17]
#!/usr/bin/ruby
require 'curses'
require 'ffi-rzmq'
require 'enumerator'
POSITIONS = "%15s %8s %8s %12s %12s %12s\n"
POSITIONS2 = "%-38s %-38s\n"
POSITIONS3 = "%15s %-12s%%-10s%%-10s%%-10s%%-15s\n"
OUT_ARROW = "-------->"
IN_ARROW = "<--------"
PAUSE = "[%6dms]"
class MinuteLongBuffer
def initialize
#values = []
end
def add v
#values << [Time.new.to_f, v]
end
def count
#values = #values.keep_if {|v| v[0] > (Time.new.to_f - 1)}
#values.length
end
end
class Scenario
def parse_scenario_desc desc
#strings = []
#msg_counts = []
parts = desc.split(";")
parts.each do |part|
type, value = part.split(":")
if type == "IN"
#strings << (POSITIONS3 % [value, IN_ARROW])
elsif type == "OUT"
#strings << (POSITIONS3 % [value, OUT_ARROW])
elsif type == "PAUSE"
#strings << (POSITIONS3 % [(PAUSE % value), ""])
end
#msg_counts << 0
#unexpected_msg_counts << 0
#timeout_counts << 0
end
end
def inc_msg idx
#msg_counts[idx.to_i] += 1
if idx.to_i == 0
#new_calls.add 1
end
end
def inc_unexpected idx
#unexpected_msg_counts[idx.to_i] += 1
end
def inc_timeout idx
#timeout_counts[idx.to_i] += 1
end
def update
Curses.clear
Curses.addstr(POSITIONS % ["Call-rate", "Length", "Port", "Total-time", "Total-calls", "Remote-host"])
Curses.addstr(POSITIONS % [("?cps"), "? ms", "????", ("%.2fs" % (Time.new.to_f - #start)), "?", "??? (???)"])
Curses.addstr("\n")
Curses.addstr(POSITIONS2 % ["%d new calls during 1.000s period" % #new_calls.count, "?ms scheduler resolution"])
Curses.addstr(POSITIONS2 % ["? concurrent calls (limit ?)", "Peak was ? calls, after ?s"])
Curses.addstr(POSITIONS2 % ["? out-of-call msg (discarded)", "? open sockets"])
Curses.addstr("\n")
Curses.addstr((POSITIONS3 % ["", "",]) % ["Messages", "Retrans", "Timeout", "Unexpected-Msg"])
#strings.each_with_index do |s, i|
Curses.addstr(s % [#msg_counts[i], 0, #timeout_counts[i], #unexpected_msg_counts[i]])
end
Curses.refresh
end
def initialize
#new_calls = MinuteLongBuffer.new
#strings = []
#msg_counts = []
#unexpected_msg_counts = []
#timeout_counts = []
#start = Time.new.to_f
Thread.new do
context = ZMQ::Context.new
socket = context.socket(ZMQ::SUB)
socket.connect("tcp://localhost:5556")
socket.setsockopt(ZMQ::SUBSCRIBE, "SIPP")
socket2 = context.socket(ZMQ::REQ)
socket2.connect("tcp://localhost:5557")
socket2.send_string "",0
socket2.recv_string msg2 = ""
parse_scenario_desc msg2
loop do
socket.recv_strings(msgs = [])
msgs.each do |msg|
name, ts, scenario, callnum, callid, idx, result = msg.split(":")
if name == "SIPP-PHASE_SUCCESS"
inc_msg idx
end
if name == "SIPP-UNEXPECTED_MSG_RECVD"
inc_unexpected idx
end
if name == "SIPP-RECV_TIMED_OUT"
inc_timeout idx
end
end
end
end
end
end
SCENARIO = Scenario.new
Curses.init_screen()
Thread.new do
loop do
tmp = Curses.getch
end
end
loop do
SCENARIO.update
sleep 1
end
Also when I check the ruby version it is pointing to 2.3.3 but while running the it shows me ruby 2.3.0 running the file. If someone could explain why is that would also help me.
After installing gems in file manually I am getting another error please see below.
/jsipp-master$ ruby curses.rb
Unable to load this gem. The libzmq library (or DLL) could not be found.
If this is a Windows platform, make sure libzmq.dll is on the PATH.
If the DLL was built with mingw, make sure the other two dependent DLLs,
libgcc_s_sjlj-1.dll and libstdc++6.dll, are also on the PATH.
For non-Windows platforms, make sure libzmq is located in this search path:
["/Library/Ruby/Gems/2.3.0/gems/ffi-rzmq-core-1.0.6/lib/ffi-rzmq-core/../../ext/libzmq.dylib", "/usr/local/bin/libzmq.dylib", "/usr/bin/libzmq.dylib", "/bin/libzmq.dylib", "/usr/sbin/libzmq.dylib", "/sbin/libzmq.dylib", "/opt/X11/bin/libzmq.dylib", "/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/libzmq.dylib", "/usr/local/lib/libzmq.dylib", "/opt/local/lib/libzmq.dylib", "/usr/local/lib/libzmq.dylib", "/usr/lib64/libzmq.dylib"]
/Library/Ruby/Gems/2.3.0/gems/ffi-rzmq-core-1.0.6/lib/ffi-rzmq-core/libzmq.rb:61:in `rescue in <module:LibZMQ>': The libzmq library (or DLL) could not be loaded (LoadError)
from /Library/Ruby/Gems/2.3.0/gems/ffi-rzmq-core-1.0.6/lib/ffi-rzmq-core/libzmq.rb:10:in `<module:LibZMQ>'
from /Library/Ruby/Gems/2.3.0/gems/ffi-rzmq-core-1.0.6/lib/ffi-rzmq-core/libzmq.rb:7:in `<top (required)>'
from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Library/Ruby/Gems/2.3.0/gems/ffi-rzmq-core-1.0.6/lib/ffi-rzmq-core.rb:3:in `<top (required)>'
from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Library/Ruby/Gems/2.3.0/gems/ffi-rzmq-2.0.6/lib/ffi-rzmq.rb:66:in `<top (required)>'
from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:127:in `require'
from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:127:in `rescue in require'
from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:40:in `require'
from curses.rb:4:in `<main>'
It is possible you have not installed the curses gem.
You can check to see if it is installed using the gem list command. If your gem is not listed, it isn't installed:
$ gem list curses
*** LOCAL GEMS ***
$
If it is not installed, install the gem using the gem install command:
$ gem install curses
Fetching: curses-1.2.4.gem (100%)
Building native extensions. This could take a while...
Successfully installed curses-1.2.4
Parsing documentation for curses-1.2.4
Installing ri documentation for curses-1.2.4
Done installing documentation for curses after 0 seconds
1 gem installed
$
You can read more in the online gem command reference.
That should get you beyond the error you are seeing.

Cannot load such file in Test-Unit Sinatra

Im currently writing my test cases using ruby sinatra and im having a very weird or just im missing an important note/tip when writing a test cases using Test-Unit sinatra.
My problem is my my environments.rb is not being loaded in my test from my app.rb. but if i run it, it does get loaded.
here is my app.rb
require 'rubygems'
require 'sinatra'
require 'pg'
require './config/environments'
require './models/user'
module Registration
class HelloWorldApp < Sinatra::Base
helpers do
include Rack::Utils
alias_method :h, :escape_html
end
get '/' do
#title = " Introduction Biatch"
erb :index
end
post '/register' do
DB[:users].insert(username: params[:username],password: params[:password])
redirect '/view'
end
get '/view' do
#users = User.all
erb :view
end
get '/view/:id' do
#user = User.find(id: params[:id])
erb :edit
end
post '/edit/:id' do
#user = User.find(id: params[:id])
#user.update(username: params[:username],password: params[:password])
redirect '/view'
end
get '/delete/:id' do
#delete_user = User.find(id: params[:id])
#delete_user.delete
redirect '/view'
end
end
end
my environments.rb is in the config folder.
and here is my sample test cases. (not yet finished)
require 'rubygems'
require 'test/unit'
require 'test/unit/assertions'
require '../app/app'
module Registration
class TestCrud < Test::Unit::TestCase
# include Registration::HelloWorldApp
def test_insert_user
end
def test_get_all_user
end
def test_delete_all_user
end
end
end
and this is the error it throws.
C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- ./config/environments (LoadError)
from C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Users/John/Documents/Sinatra-Intro/app/app.rb:4:in `<top (required)>'
from C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from test_crud.rb:4:in `<main>'
C:\Users\John\Documents\Sinatra-Intro\test>
Im very confused what did i do wrong. thanks in advance.
Ive found the answer, just change the require './config/environments' to require_relative. and change the require '../app/app' into require_into.

LoadError on require './primes.rb' in terminal

When I do require './primes.rb' in irb I get this:
1.9.3-p392 :004 > require './primes.rb'
LoadError: cannot load such file -- ./primes.rb
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from (irb):4
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'
Here is the primes.rb document:
# primes.rb
require 'debugger'
def prime?(num)
debugger
(1..num).each do |i|
if (num % i) == 0
return false
end
end
end
def primes(num_primes)
ps = []
num = 1
while ps.count < num_primes
primes << num if prime?(num)
end
end
if __FILE__ == $PROGRAM_NAME
puts primes(100)
end
Any suggestions of how to get this to work would be greatly appreciated!
When I do require relative it gives me this:
1.9.3-p392 :010 > require_relative 'primes.rb'
LoadError: cannot infer basepath
from (irb):10:in `require_relative'
from (irb):10
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'
When I do the second solution below it gives me this:
1.9.3-p392 :013 > $LOAD_PATH << "."
=> ["/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/x86_64-darwin11.4.2", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/vendor_ruby/1.9.1", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin11.4.2", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/vendor_ruby", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/x86_64-darwin11.4.2", "."]
1.9.3-p392 :014 > require 'primes.rb'
LoadError: cannot load such file -- primes.rb
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from (irb):14
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'
1.9.3-p392 :015 >
When I try it in pry:
[4] pry(main)> require_relative 'primes.rb'
LoadError: cannot infer basepath
from (pry):2:in `require_relative'
[5] pry(main)> require 'primes.rb'
LoadError: cannot load such file -- primes.rb
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
[6] pry(main)> .ls
Applications Movies git-completion.bash
Desktop Music rails_projects
Documents Pictures ruby
Downloads Public runwithfriends
Dropbox code shopify
Library dev sites
[7] pry(main)> require 'ruby/app_acad_mini_curriculum/debugging/primes.rb'
LoadError: cannot load such file -- ruby/app_acad_mini_curriculum/debugging/primes.rb
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
Try require_relative
require_relative 'primes.rb'
EDIT:
Note that this will only work from within a script. If you are trying to require this script into an irb session then you will need to provide the full path to primes.rb. The reason is where irb's location is. For instance, try Dir.pwd inside irb and you will see where require_relative is attempting to search for primes.rb.
There are a couple things you could do:
# Just need to require the one file.
require_relative File.join('users', 'yourusername', 'prime_folder', 'prime.rb')
# Many files in the same folder
$LOAD_PATH << File.join('users', 'yourusername', 'prime_folder')
require 'prime.rb'
require 'another_file.rb'
Another option, one that I use, is Pry. It is like irb and is very easy to call from a script. It is a gem so:
gem install pry
At the end of your script, you could do:
if $0 == __FILE__
require 'pry'
binding.pry
end
You would then drop into an irb like REPL where you can test and debug your methods. I can't survive without it.
unlike ruby 1.8, you can't require a file that is in the same folder, because the current folder is not on the load path any longer.
To emulate the behavior of ruby 1.8, you could try
$LOAD_PATH << "."
require 'primes.rb'
However, the correct way to do in ruby 1.9, as #CharlesCaldwell pointed, is using relative_require.
Here is a good discussion of the best way to deal with this.
note that relative_require does not work in irb. You can check the motive on #CharlesCaldwell answer.
But looking in your task question, you should not use irb, you should use pry:
We're going to use two gems. One is called Pry, which is a replacement for irb. You'll have to gem install pry. It's not essential for debugging that you use Pry, but it will make life nicer.
Here is an example using relative require:
[fotanus#thing ~]$ cat primes.rb
# primes.rb
def prime?(num)
(1..num).each do |i|
if (num % i) == 0
return false
end
end
end
def primes(num_primes)
ps = []
num = 1
while ps.count < num_primes
primes << num if prime?(num)
end
end
if __FILE__ == $PROGRAM_NAME
puts primes(100)
end
[fotanus#thing ~]$ cat a.rb
require_relative 'primes.rb'
[fotanus#thing ~]$ ruby a.rb

Simple use of EM::Synchrony#sync causes 'root fiber' FiberError -- my fault?

This program
require 'em-synchrony' ## v1.0.0
require 'em-hiredis' ## v0.1.0
module EventMachine
module Hiredis
class Client
def self.connect(host = 'localhost', port = 6379)
conn = new(host, port)
EM::Synchrony.sync conn.connect
conn
end
alias :old_method_missing :method_missing
def method_missing(sym, *args)
EM::Synchrony.sync old_method_missing(sym, *args)
end
end
end
end
EventMachine.synchrony do
redis = EM::Hiredis.connect
redis.set('foo', 'bar')
puts redis.get('foo')
EM.stop
end
dies like this
$ ruby /tmp/reddy.rb
/home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-synchrony-1.0.0/lib/em-synchrony.rb:58:in `yield': can't yield from root fiber (FiberError)
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-synchrony-1.0.0/lib/em-synchrony.rb:58:in `sync'
from /tmp/reddy.rb:16:in `method_missing'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-hiredis-0.1.0/lib/em-hiredis/client.rb:119:in `select'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-hiredis-0.1.0/lib/em-hiredis/client.rb:38:in `block in connect'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-hiredis-0.1.0/lib/em-hiredis/event_emitter.rb:8:in `call'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-hiredis-0.1.0/lib/em-hiredis/event_emitter.rb:8:in `block in emit'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-hiredis-0.1.0/lib/em-hiredis/event_emitter.rb:8:in `each'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-hiredis-0.1.0/lib/em-hiredis/event_emitter.rb:8:in `emit'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-hiredis-0.1.0/lib/em-hiredis/connection.rb:15:in `connection_completed'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/eventmachine-1.0.0.beta.4/lib/eventmachine.rb:179:in `run_machine'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/eventmachine-1.0.0.beta.4/lib/eventmachine.rb:179:in `run'
from /home/blt/.rvm/gems/ruby-1.9.3-p0/gems/em-synchrony-1.0.0/lib/em-synchrony.rb:27:in `synchrony'
from /tmp/reddy.rb:22:in `<main>'
I find this deeply confusing. Why doesn't it work and am I at fault? If so, what can I do differently? Unless I've glossed over something, this is kosher, per the em-synchrony README.
I think your code can work if you find the correct version of em-hiredis it is trying to monkey patch, that is one problem with loose dependencies.
Here is a fully working code but based on the master branch of em-synchrony:
Gemfile:
source :rubygems
gem 'em-synchrony', :git => "git://github.com/igrigorik/em-synchrony.git"
gem 'em-hiredis', '~> 0.1.0'
test.rb:
require 'rubygems'
require 'bundler/setup'
require 'em-synchrony'
require 'em-synchrony/em-hiredis'
EventMachine.synchrony do
redis = EM::Hiredis.connect
redis.set('foo', 'bar')
puts redis.get('foo')
EM.stop
end
and then run it with:
$ bundle
$ ruby test.rb
Monkey patching is an inherently flawed way of patching gems unless you ensure the exact version of the gem you patched is used which is something em-synchrony should enforce or at least detect.

Ruby 1.9.2 - Read and parse a remote CSV

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

Resources