Empty array in smarter_csv - ruby-on-rails

I am using Ruby 2.1.1 and the gem smarter_csv version 1.0.17. I am trying to get a simple example to work but keep getting an empty array. Any hints on what I am doing wrong.
2.1.1 :006 > f = File.open("/Users/diasks2/test.csv", "rb")
2.1.1 :007 > f.read
=> "source_language,target_language\rhello,hola\rlibrary,biblioteca"
2.1.1 :008 > require 'smarter_csv'
=> true
2.1.1 :009 > data = SmarterCSV.process("/Users/diasks2/test.csv")
=> []

This should work:
data = SmarterCSV.process("/Users/diasks2/test.csv", row_sep: "\r")
When not indicating row_sep, smarter_csv will use the system new line separator which is "\r\n" for windows machines, and "\r" for unix machines. Your file has "\r" to separate the lines, so you need to explicitly indicate this.
Edit - apparently there is a feature for smarter_csv to guess the row separator - if you use the following:
data = SmarterCSV.process("/Users/diasks2/test.csv", row_sep: :auto)
The gem should identify file with lines ending with \n, \r\n, or \r and parse them accordingly.

Related

How do I create a checksum of carrierwave upload to verify the download?

How do I create a checksum (MD5, sha512, whatever) of a file when I upload it, so that when I download (using cache_stored_file!), I can verify that it is indeed the original file that was uploaded?
The Ruby Digest module can help with this.
One way solution would be to read the file on upload and assign it a unique digest with a before_create callback. I would add it as a column on the file table in your database.
Here's some output from IRB to show how it would work:
2.2.2 :001 > require 'digest'
=> true
2.2.2 :002 > f = File.read 'test.rb'
=> "Original content\n"
2.2.2 :003 > Digest::SHA256.hexdigest(f)
=> "646722e7ee99e28d618142b9d3a1bfcbe2196d8332ae632cc867ae5d1c8c57b5"
# (... file modified ...)
2.2.2 :004 > f = File.read 'test.rb'
=> "Original content with more content\n"
2.2.2 :005 > Digest::SHA256.hexdigest(f)
=> "c29f2f77c0777a78dbdf119bf0a58b470c098635dfc8279542e4c49d6f20e62c"
You can use this digest in your download method to check the integrity of the file. If you read the file again, produce a digest, and it matches the original digest, you can be confident the file hasn't been altered since it was uploaded.
Ruby Digest Module
md5 = Digest::MD5.file('path_to_file').hexdigest
This would read file in blocks and avoid reading the whole file in RAM which is done in File.read()
For SHA checksum
Digest::SHA2.hexdigest( File.read("/path/to/my/file.txt") );
OR
Digest::SHA2.file(myFile).hexdigest
=> "fa5880ac744f3c05c649a864739530ac387c8c8b0231a7d008c27f0f6a2753c7"
More details for SHA checksum generation SHA Checksum

Soft signs in rails console

I want to create multiple categories via console and I want to be able add soft signs. At this moment I can't do that.
It's very important to project that I can save category names with soft signs.
Can somebody tip me where to search? I searched such tag - soft signs rails.
There wasn't any usefull resource.
Thanks
EDIT
Soft signs in my native language is like this.
Ā,Š,Ē,Ž with that symbol called soft sign abowe the character.
At this moment when I try to save new category record it shows me this kind off error
thodError: undefined methodcache_ancestry!' for #
But I am sure that I didn't change anything in models or controllers :(
What version of Ruby is this? What you're seeing there are either US-ASCII strings with UTF-8 data in them (Ruby 1.9) or byte arrays (Ruby 1.8).
If you're using Ruby 1.8, you may need to use Iconv to convert your encoding from US-ASCII to UTF-8. If you're using Ruby 1.9, then make sure you're creating UTF-8 strings and it should work just fine.
Note that those escape sequences are correct - that is the literal byte array of those characters, assuming the proper encoding is applied, so you may not need to actually change anything. If the bytes are right, everything's fine - you're just seeing ruby interpret the string as ASCII rather than UTF-8 or whatnot.
In Ruby 1.8, when you #inspect a string, you get the escaped version, but putsing it will show you the actual string:
1.8.7 :021 > s = "Komunālās mašīnas"
=> "Komun\304\201l\304\201s ma\305\241\304\253nas"
1.8.7 :022 > puts s
Komunālās mašīnas
In 1.9, you get the correct display all around, so long as your encoding is right:
1.9.3p327 :001 > s = "Komunālās mašīnas"
=> "Komunālās mašīnas"
1.9.3p327 :004 > s.force_encoding "US-ASCII"
=> "Komun\xC4\x81l\xC4\x81s ma\xC5\xA1\xC4\xABnas"
1.9.3p327 :005 > puts s
Komunālās mašīnas
Check this out Edgars:
#encoding: UTF-8
t = 'ŠšÐŽžÀÁÂÃÄAÆAÇÈÉÊËÌÎÑNÒOÓOÔOÕOÖOØOUÚUUÜUÝYÞBßSàaáaâäaaæaçcèéêëìîðñòóôõöùûýýþÿƒ'
fallback = {
'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',
'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',
'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U',
'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a',
'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i',
'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u',
'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f'
}
p t.encode('us-ascii', :fallback => fallback)
See Ruby 1.9.x replace sets of characters with specific cleaned up characters in a string
EDIT:
To get all the characters for your language you will need to add them as desired to the fallback hash. When I run "Komunālās mašīnas" as the variable 't' I get this:
t = "Komunālās mašīnas"
t.encode('us-ascii', :fallback => fallback)
Encoding::UndefinedConversionError: U+0101 from UTF-8 to US-ASCII
You can tell from this where the problem lies by googling U+0101 which shows
http://www.charbase.com/0101-unicode-latin-small-letter-a-with-macron
So now you know which letter is not working and you can add it to the fallback hash like so:
fallback = { OTHER DEFINITIONS , 'ā'=>'a'}
Here's a place to start:
http://www.ascii-codes.com/cp775.html

Cannot select column with accent in rails

I have the Level record in my rails app, it contains 1 field (name) that can contain chars with accents.
1.9.3p125 :008 > Level.all
Level Load (0.4ms) SELECT "levels".* FROM "levels"
=> [#<Level id: 1, name: "Débutant">, #<Level id: 2, name: "Intermédiaire">, #<Level id: 3, name: "Avancé">]
But when I query, I have:
1.9.3p125 :011 > Level.where("name = ?", "D\U+FFC3\U+FFA9butant").first
Level Load (0.3ms) SELECT "levels".* FROM "levels" WHERE (name = 'Dbutant') LIMIT 1
=> nil
I cannot type Level.where("name = ?", "Débutant").first when using rails c as é is directly replaced by \U+FFC3\U+FFA9. But in my controller, the result is the same, I cannot query accentuated string.
I currently use sqlite for my tests.
Your problem in the console is related to the readline library: You have to install Ruby with proper readline support.
If you have installed Ruby using rvm, the quickest way to resolve this problem is:
rvm pkg install readline
and then (assuming you are using 1.9.3, otherwise just replace with your Ruby version):
rvm reinstall 1.9.3 --with-readline-dir=$rvm_path/usr
After that, typing é in the Rails console should work again...
Regarding your other problem (in the controller): Could you please post a log excerpt containing the SQL query that is being made?
Place this string
#encoding: utf-8
to the first line of your Ruby file.

Fastercsv shows malformedCSVError, what am i doing wrong?

I am implementing in Ruby and i am running a project which reads a CSV file to add users.
but when i pick my file it just gives always the same error:
FasterCSV::MalformedCSVError in User importController#match
Illegal quoting on line 1.
my CSV file just exists of :
"RubenPersoon1","test","Bauwens","Ruben","rub#gmail.com",0
anyone who knows what can be wrong?
Try to upgrade your FasterCSV gem version. With the latest version it works:
FasterCSV.parse_line '"RubenPersoon1","test","Bauwens","Ruben","rub#gmail.com",0'
=> ["RubenPersoon1", "test", "Bauwens", "Ruben", "rub#gmail.com", "0"]
ruby-1.8.7-p352 :005 > FasterCSV.parse '"RubenPersoon1","test","Bauwens","Ruben","rub#gmail.com",0'
=> [["RubenPersoon1", "test", "Bauwens", "Ruben", "rub#gmail.com", "0"]]
Also, keep in mind that if you are on Ruby 1.9.2, FasterCSV is already included. Just require 'csv' and use the CSV class.

Hash.from_xml double escapes &

>> h={:title => "hi & mv288" }
=> {:title=>"hi & mv288"}
>> h.to_xml
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n <title>hi &amp; mv288</title>\n</hash>\n"
>> Hash.from_xml h.to_xml
=> {"hash"=>{"title"=>"hi & mv288"}}
If you notice line#2 and #4, the & characters in the title value became & after
a series of Hash.to_xml and from_xml method calls.
Is there any way to prevent Hash.from_xml from converting & into &.
We switched the xml parser to Nokogiri to resolve this issue.
Add this line in your environment.rb
ActiveSupport::XmlMini.backend = 'Nokogiri'
You will have to have nokogiri gem installed though. If you need a pure
java implementation of nokogiri, check this out.
https://github.com/tenderlove/nokogiri/wiki/pure-java-nokogiri-for-jruby
The installation command is,
gem install nokogiri --pre
You may also use LibXml as XmlMiini.backend to resolve this issue.

Resources