How to put an image on a label using Prawn/labels? - ruby

I'm using Ruby for scripting, and Prawn-labels to print ISBN labels for books, using Barby to generate the EAN-13 bar codes.
The ISBNs come out of MySQL. That works fine. I can create a PDF of 30-up labels containing the titles of the books, using Prawn/labels. That works fine.
However, I am not having much joy putting a Barby-generated PNG image of the proper EAN-13 barcode on the label.
Here's getting the info out of the database and generating EAN-13, pretty straightforward:
query = "SELECT ISBN13, Title FROM s_library WHERE Has_barcode != 'y'"
result = client.query(query, symbolize_keys: true)
tags = []
result.each do |row|
tags << [row[:ISBN13], row[:Title], Barby::EAN13.new(row[:ISBN13].chop)]
end
tags.sort! {|a, b| a[1] <=> b[1]} # Sort by book title
So now I have a two-dimensional Array, with each row containing an ISBN String, a Title String, and a Barby holding the EAN-13 coding for the ISBN (#<Barby::EAN13:0x00007fc6da13ce98 #data="978140006215">
).
Now I'm following the example for Prawn::Labels, and if I leave out trying to #embed_image the PNG data, I can successfully generate 30-up labels with the title of each book:
Prawn::Labels.generate('EAN_tags.pdf', tags, :type => 'Avery5160') do |pdf, tag|
pdf.font '/Library/Fonts/Erasdemi.TTF'
pdf.text(tag[1], :align => :center)
png = Barby::PngOutputter.new(tag[2]).to_png
# pdf.embed_image(png, Prawn::Images::PNG.new(png), {})
end
`open EAN_tags.pdf`
When I un-comment the image embedding, I get a cryptic traceback that I don't quite understand:
25: from /Users/jan/bin/Print_ISBN13.rb:73:in `<main>'
24: from /usr/local/lib/ruby/gems/2.7.0/gems/prawn-labels-1.2.6/lib/prawn/labels.rb:18:in `generate'
23: from /usr/local/lib/ruby/gems/2.7.0/gems/prawn-2.3.0/lib/prawn/document.rb:408:in `render_file'
22: from /usr/local/lib/ruby/gems/2.7.0/gems/prawn-2.3.0/lib/prawn/document.rb:408:in `open'
21: from /usr/local/lib/ruby/gems/2.7.0/gems/prawn-2.3.0/lib/prawn/document.rb:408:in `block in render_file'
20: from /usr/local/lib/ruby/gems/2.7.0/gems/prawn-2.3.0/lib/prawn/document.rb:400:in `render'
19: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/renderer.rb:167:in `render'
18: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/renderer.rb:202:in `render_body'
17: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/document_state.rb:72:in `render_body'
16: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/object_store.rb:70:in `each'
15: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/object_store.rb:70:in `each'
14: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/object_store.rb:71:in `block in each'
13: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/document_state.rb:78:in `block in render_body'
12: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/reference.rb:26:in `object'
11: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:87:in `pdf_object'
10: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:87:in `each'
9: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:93:in `block in pdf_object'
8: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:87:in `pdf_object'
7: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:87:in `each'
6: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:93:in `block in pdf_object'
5: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:87:in `pdf_object'
4: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:87:in `each'
3: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:93:in `block in pdf_object'
2: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:75:in `pdf_object'
1: from /usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:25:in `utf8_to_utf16'
/usr/local/lib/ruby/gems/2.7.0/gems/pdf-core-0.8.1/lib/pdf/core/pdf_object.rb:25:in `encode': "\\x89" to UTF-8 in conversion from ASCII-8BIT to UTF-8 to UTF-16BE (Encoding::UndefinedConversionError)
I can dump the png variable to a file and see the barcode.
From the stack, it looks like it's trying to interpret the PNG data as a UTF-16 String. Ugh. Wasn't expecting that.
Any clues as to what I'm doing wrong?

Use pdf.image(png) instead of pdf.embed_image. This is a wrapper around pdf.embed_image that does the necessary work of getting the necessary data out of the PNG file.

The answer appears to be to use something called StringIO, which apparently provides a File interface to an in-memory (blob) image.
So, I added the middle line, below:
png = Barby::PngOutputter.new(tag[2]).to_png
str_png = StringIO.new(png)
pdf.image(str_png)
instead of trying to pass binary data via embed_image, and I am now getting the barcode in the final PDF output. (It still needs some tweaking, but the basic concept is now working.)
I had searched here many ways — really! But the word "blob" is what got the answer on this site.

Related

How do I parse a tab-delimited line that contains a quote?

I'm using Ruby 2.4. How do I parse a tab-delimited line that contains a quote character? This is what's happening to me now ...
2.4.0 :003 > line = "11\tDave\tO\"malley"
=> "11\tDave\tO\"malley"
2.4.0 :004 > CSV.parse(line, col_sep: "\t")
CSV::MalformedCSVError: Illegal quoting in line 1.
from /Users/davea/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/csv.rb:1912:in `block (2 levels) in shift'
from /Users/davea/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/csv.rb:1868:in `each'
from /Users/davea/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/csv.rb:1868:in `block in shift'
from /Users/davea/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/csv.rb:1828:in `loop'
from /Users/davea/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/csv.rb:1828:in `shift'
from /Users/davea/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/csv.rb:1770:in `each'
from /Users/davea/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/csv.rb:1784:in `to_a'
from /Users/davea/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/csv.rb:1784:in `read'
from /Users/davea/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/csv.rb:1324:in `parse'
from (irb):4
from /Users/davea/.rvm/gems/ruby-2.4.0#global/gems/railties-5.0.1/lib/rails/commands/console.rb:65:in `start'
from /Users/davea/.rvm/gems/ruby-2.4.0#global/gems/railties-5.0.1/lib/rails/commands/console_helper.rb:9:in `start'
from /Users/davea/.rvm/gems/ruby-2.4.0#global/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
from /Users/davea/.rvm/gems/ruby-2.4.0#global/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /Users/davea/.rvm/gems/ruby-2.4.0#global/gems/railties-5.0.1/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Although teh example illustrates my point, I can't easily control the input coming in. So, although an answer coudl be< "Remove all quotes from teh string before parsing," I want to preserve the data as closely as possible.
That's a malformed document if you're trying to adhere to the CSV standard. Instad you might just brute-force it and pray there's no tabs in the data itself:
line.split(/\t/)
The CSV parsing library comes in handy when you're dealing with data like this:
"1\t2\t\"3a\t3b\"\t4"
Update: If you're prepared to abuse the CSV library a little then you can do this:
CSV.parse("11\tDave\tO\"malley", col_sep: "\t", quote_char: "\0")
That basically kills quote detection, so if there is other data that depends on that being processed correctly this may not work out.
"11\tDave\tO\"malley" is not valid CSV data. Strangely enough, the answer is to use two double-quotes, and to double quote each element
2.3.1 :001 > require 'csv'
=> true
2.3.1 :002 > line = "\"11\"\t\"Dave\"\t\"O\"\"malley\""
=> "\"11\"\t\"Dave\"\t\"O\"\"malley\""
2.3.1 :003 > puts line # for clarity
"11" "Dave" "O""malley"
=> nil
2.3.1 :004 > CSV.parse(line, col_sep: "\t")
=> [["11", "Dave", "O\"malley"]]

Nokogiri method error

I am using Nokogiri as a tool to help me with a project I'm working on for school. Right now the idea of the project is to search an HTML document for a keyword that the user provides. The function will find all occurrences of the keyword in the HTML string after the <body> element unless the keyword appears within an HTML tag, then surround the string found with tags to ``highlight’’ the keyword. Right now my code is failing with a pretty hefty error call stack. The main error is this:
Nokogiri::CSS::SyntaxError:unexpected'$'after''[:substring_match,"simple"]'(Nokogiri::CSS::SyntaxError)
My Current Method:
require 'nokogiri'
file = File.open ("desktop/Personal/code.HTML")
#doc = Nokogiri::HTML(file)
puts #doc
puts "Welcome to the HTML keyword highlighter!"
puts "Please enter a keyword"
keyword = gets.chomp
highlight = "<span style='background-color: yellow; color: black'>#{keyword}</span>"
search = #doc.xpath("//body").search("[text()*=#{keyword}")
search.each do |node|
nodeN = node.name
nodeH = node.content.gsub(keyword, highlight)
node.replace(nodeH)
end
file.close
The error call stack:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2. 0.0/gems/nokogiri-1.5.6/lib/nokogiri/css/parser_extras.rb:87:in `on_error': unexpected '$' after '[:substring_match, "simple"]' (Nokogiri::CSS::SyntaxError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/racc/parser.rb:258:in `_racc_do_parse_c'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/racc/parser.rb:258:in `do_parse'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/css/parser_extras.rb:62:in `parse'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/css/parser_extras.rb:79:in `xpath_for'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/css.rb:23:in `xpath_for'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:111:in `block (2 levels) in css'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:109:in `map'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:109:in `block in css'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:239:in `block in each'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:238:in `upto'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:238:in `each'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:105:in `css'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:81:in `block in search'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:80:in `each'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:80:in `search'
from /Users/Eva/Desktop/Personal/Project_Nokogiri.rb:21:in `<main>'
Any advice on how to go about preventing this would be truly appreciated! I'm new to programming, Ruby, and Nokogiri so I have very little understanding of how to fix this on my own. Sorry if formatting is bad. Thank you for any help!
You forgot to close the square bracket:
search = #doc.xpath("//body").search("[text()*=#{keyword}]")
I'd add quotes just in case:
search = #doc.xpath("//body").search("[text()*=\"#{keyword}\"]")
(You might expand that by also ensuring the double quote is escaped if it is in keyword to make it bulletproof)

TypeError reading in multiline regex

In a file is apparently stored a multiline regex, saved by YAML::dump(b1) without a problem.
Question, how can I load it back when Syck seems to have issues with the multiline regular expression?
b2 = YAML::load(File.open("browserObj.yaml", 'r'))
Syck::TypeError: Invalid Regular expression: "/\\A\\s*\n ([a-zA-Z][\\-+.a-zA-Z\\d]*): (?# 1: scheme)\n (?:\n ((?:[\\-_.!~*'()a-zA-Z\\d;?:#&=+$,]|%[a-fA-F\\d]{2})(?:[\\-_.!~*'()a-zA-Z\\d;\\/?:#&=+$,\\[\\]]|%[a-fA-F\\d]{2})*) \\ (?# 2: opaque)\n |\n (?:(?:\n \\/\\/(?:\n \\ (?:(?:((?:[\\-_.!~*'()a-zA-Z\\d;:&=+$,]|%[a-fA-F\\d]{2})*)#)? \\ (?# 3: userinfo)\n (?:((?:(?:[a-zA-Z0-9\\-.]|%\\h\\h)+|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\[(?:(?:[a-fA-F\\d]{1,4}:)*(?:[a-fA-F\\d]{1,4}|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})|(?:(?:[a-fA-F\\d]{1,4}:)*[a-fA-F\\d]{1,4})?::(?:(?:[a-fA-F\\d]{1,4}:)*(?:[a-fA-F\\d]{1,4}|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}))?)\\]))(?::(\\d*))?))? (?# 4: host, 5: port)\n |\n ((?:[\\-_.!~*'()a-zA-Z\\d$,;:#&=+]|%[a-fA-F\\d]{2})+) \\ (?# 6: registry)\n )\n |\n (?!\\/\\/)) \\ (?# XXX: '\\/\\/' is the mark for hostport)\n \\ (\\/(?:[\\-_.!~*'()a-zA-Z\\d:#&=+$,]|%[a-fA-F\\d]{2})*(?:;(?:[\\-_.!~*'()a-zA-Z\\d:#&=+$,]|%[a-fA-F\\d]{2})*)*(?:\\/(?:[\\-_.!~*'()a-zA-Z\\d:#&=+$,]|%[a-fA-F\\d]{2})*(?:;(?:[\\-_.!~*'()a-zA-Z\\d:#&=+$,]|%[a-fA-F\\d]{2})*)*)*)? \\ (?# 7: path)\n )(?:\\?((?:[\\-_.!~*'()a-zA-Z\\d;\\/?:#&=+$,\\[\\]]|%[a-fA-F\\d]{2})*))? \\ (?# 8: query)\n )\n (?:\\#((?:[\\-_.!~*'()a-zA-Z\\d;\\/?:#&=+$,\\[\\]]|%[a-fA-F\\d]{2})*))? \\ (?# 9: fragment)\n \\s*\\z/x"
from /usr/lib/ruby/1.9.1/syck/rubytypes.rb:284:in `yaml_new'
from /usr/lib/ruby/1.9.1/syck.rb:135:in `transfer'
from /usr/lib/ruby/1.9.1/syck.rb:135:in `node_import'
from /usr/lib/ruby/1.9.1/syck.rb:135:in `load'
from /usr/lib/ruby/1.9.1/syck.rb:135:in `load'
from /usr/lib/ruby/1.9.1/syck.rb:146:in `block in load_file'
from /usr/lib/ruby/1.9.1/syck.rb:145:in `open'
from /usr/lib/ruby/1.9.1/syck.rb:145:in `load_file'
from (irb):428
from /usr/bin/irb:12:in `<main>'
I've seen discussion about a patch to rubytypes.rb but otherwise nothing.
I'd rather not switch to Psych since that opens up another can of worms:
YAML::ENGINE.yamler = "psych"
# attempt to load from file again
TypeError: can't convert Fixnum into String
What's going on? Is there any hope? I had no prior knowledge of Syck nor Psych nor the internals of YAML parsing until this error made it non-transparent.
You could store the regexp as string. When you read it back from the yaml file just convert the string back into a regexp. Regexp.new myregexp_str - This is of course just a workaround.
Update:
Or you could do something that really stupid and patch psych to handle multiline regexp.
Since the patched method is to long to post it grab it here.
But here is the explanation of what was really patched.
module Psych
module Visitors
class ToRuby < Psych::Visitors::Visitor
# ...
def deserialize
# ... L75
when "!ruby/regexp"
o.value =~ /^\/(.*)\/([mixn]*)$/m # <- notice the added "m" for multiline mode
# ...
end
end
end
end
using ruby-1.9.3-p194
For everybody lurking around, this looks like an old bug. I just tried with ruby 1.9.3 and I'm getting this:
[3] pry(main)> YAML.load(YAML.dump(/asd/m))
=> /asd/m

How do I open local image file, encode it, and post via URI? (posting via Tumblr API)

I'm trying to read local image file, properly encode it and post to Tumbrl. According to the Tumblr API I can pass a parameter data which is Array (URL-encoded binary contents) Limit: 5 MB
I've tested my code with http://api.tumblr.com/v2/blog/#{BLOG}/info request. It is working. But I can't post a photo. Here is my code:
require 'oauth'
require 'oauth/consumer'
require 'open-uri'
require 'active_support'
CONSUMER = 'foo'
SECRET = 'foo'
TOKEN = 'foo'
TOKEN_SECRET = 'foo'
BLOG = 'foo'
consumer=OAuth::Consumer.new(CONSUMER, SECRET, {:site=>"http://tumblr.com"})
access_token = OAuth::AccessToken.new(consumer, TOKEN, TOKEN_SECRET)
# Here I tried one of two lines:
# data = Base64.encode64(IO.binread('./resized')) #first try
data = URI::encode(IO.binread('./resized')) #second try
# response = access_token.get "http://api.tumblr.com/v2/blog/#{BLOG}/info?api_key=#{CONSUMER}"
# puts response
response=access_token.post "http://api.tumblr.com/v2/blog/#{BLOG}/post?api_key=#{CONSUMER}&type=photo&data=#{data}&link=http://ya.ru&"
puts response
1st try:
% ruby ./w_oauth.rb
/usr/lib/ruby/1.9.1/uri/common.rb:176:in `split': bad URI(is not URI?): http://api.tumblr.com/v2/blog/foo/post?api_key=foo&type=photo&data=/9j/4AAQSkZJRgABAQEASABIAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4w (URI::InvalidURIError)
ICh1c2luZyBJSkcgSlBFRyB2NjIpLCBxdWFsaXR5ID0gODAK/9sAQwAGBAUG
BQQGBgUGBwcGCAoQCgoJCQoUDg8MEBcUGBgXFBYWGh0lHxobIxwWFiAsICMm
(!!!long piece of image data skipped!!!)
FI/16HfTbyHPWurqdE+TGH4wx2js5SKQb+6b4bIj3aurqCrEtcXrf/4yf/dS
DLet/wCzEB6sa6uoomxJN2eaQj5mkYuerQj611dQM7Fx/wDLF/8AbXV1dTA/
/9k=
&link=http://ya.ru&
from /usr/lib/ruby/1.9.1/uri/common.rb:211:in `parse'
from /usr/lib/ruby/1.9.1/uri/common.rb:747:in `parse'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/tokens/access_token.rb:7:in `request'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/tokens/access_token.rb:47:in `post'
from ./w_oauth.rb:23:in `<main>'
2nd try:
% ruby ./w_oauth.rb
/var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/helper.rb:14:in `force_encoding': can't modify frozen String (RuntimeError)
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/helper.rb:14:in `rescue in escape'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/helper.rb:12:in `escape'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/helper.rb:43:in `block (2 levels) in normalize'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/helper.rb:42:in `collect'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/helper.rb:42:in `block in normalize'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/helper.rb:37:in `map'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/helper.rb:37:in `normalize'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/request_proxy/base.rb:98:in `normalized_parameters'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/request_proxy/base.rb:113:in `signature_base_string'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/signature/base.rb:77:in `signature_base_string'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/signature/hmac/base.rb:12:in `digest'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/signature/base.rb:65:in `signature'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/signature.rb:23:in `sign'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/client/helper.rb:45:in `signature'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/client/helper.rb:75:in `header'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/client/net_http.rb:91:in `set_oauth_header'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/client/net_http.rb:30:in `oauth!'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/consumer.rb:224:in `sign!'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/consumer.rb:188:in `create_signed_request'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/consumer.rb:159:in `request'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/tokens/consumer_token.rb:25:in `request'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/tokens/access_token.rb:12:in `request'
from /var/lib/gems/1.9.1/gems/oauth-0.4.6/lib/oauth/tokens/access_token.rb:47:in `post'
from ./w_oauth.rb:23:in `<main>'
UPD: ./resized is a proper JPEG file:
% file ./resized
./resized: JPEG image data, JFIF standard 1.01, comment: "CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 80"
URI encoding is not enough. You also need to encode: , / ? : # & = + $ #.
Try:
URI.escape(IO.binread('./resized'), Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))

Ruby 1.9 CSV: selectively ignoring conversions for a column

I have following CSV data:
10,11,12.34
I can parse this using CSV from the standard library, and have the values converted from strings to numbers:
require 'csv'
CSV.parse( "10,11,12.34" )
=> [["10", "11", "12.34"]]
CSV.parse( "10,11,12.34", {:converters => [:integer,:integer,:float]} )
=> [[10, 11, 12.34]]
I don't want to convert column 1, I'd just like that left as a string. My guess was I could omit a value from the converters array, but that didn't work:
CSV.parse( "10,11,12.34", {:converters => [nil,:integer,:float]} )
NoMethodError: undefined method `arity' for nil:NilClass
from /home/ian/.rvm/rubies/jruby-1.6.6/lib/ruby/1.9/csv.rb:2188:in `convert_fields'
from org/jruby/RubyArray.java:1614:in `each'
from /home/ian/.rvm/rubies/jruby-1.6.6/lib/ruby/1.9/csv.rb:2187:in `convert_fields'
from org/jruby/RubyArray.java:2332:in `collect'
from org/jruby/RubyEnumerator.java:190:in `each'
from org/jruby/RubyEnumerator.java:404:in `with_index'
from /home/ian/.rvm/rubies/jruby-1.6.6/lib/ruby/1.9/csv.rb:2186:in `convert_fields'
from /home/ian/.rvm/rubies/jruby-1.6.6/lib/ruby/1.9/csv.rb:1923:in `shift'
from org/jruby/RubyKernel.java:1408:in `loop'
from /home/ian/.rvm/rubies/jruby-1.6.6/lib/ruby/1.9/csv.rb:1825:in `shift'
from /home/ian/.rvm/rubies/jruby-1.6.6/lib/ruby/1.9/csv.rb:1767:in `each'
from org/jruby/RubyEnumerable.java:391:in `to_a'
from /home/ian/.rvm/rubies/jruby-1.6.6/lib/ruby/1.9/csv.rb:1778:in `read'
from /home/ian/.rvm/rubies/jruby-1.6.6/lib/ruby/1.9/csv.rb:1365:in `parse'
from (irb):25:in `evaluate'
In fact I haven't been able to find any way of specifying that I'd like the first column to be left unconverted. Any suggestions?
Update
I think I misunderstood the design intention for :converters. It's not a 1:1 mapping by column, but a list of converters to be applied (I think) to all values. I'm not sure, the docs aren't too clear. So the more general question is: How do I convert some columns in my CSV, and not others?
The documentation says these options aren't specified per column, but are instead a list of converters that will be applied to all columns.
Example:
CSV.parse("10,11,13,12.34", { :converters => [lambda{|s|s.to_s + 'x'}] })
# => [["10x", "11x", "13x", "12.34x"]]
Since the CSV module is eager to convert everything it can, you may as well shift back any columns you want using .to_s or use the :unconverted_fields option to save the original values and allow access to them.

Resources