Nokogiri method error - ruby

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)

Related

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

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.

Ruby Rspec cucumber array each do

I have scenario when upon login to the page, I am presented with numerous profile(can be 1 to 5).
I am looking for specific profile based by tn number.
I locate element that represent tn and then put in array to search for all available elements with same to locate correct profile in order to click on it.
Here is the code:
And(/^I look for "([^"]*)"$/) do |number|
elements = #driver.find_elements(:css => "h3.phone-number")
elements.each do |element|
renewals_page.select_profile.click if element.text == #config[number]
return element
end
fail
end
I am passing desired number from yaml file depends on the account.
renewals_page.select_profile.click is defined in another file as method
def select_profile
#driver.find_element(:css => "h3.phone-number")
end
So when I try to locate that element and click on it, I get following error
unexpected return (LocalJumpError)
./features/step_definitions/renewals_login_step.rb:28:in `block (2 levels) in <top (required)>'
./features/step_definitions/renewals_login_step.rb:26:in `each'
./features/step_definitions/renewals_login_step.rb:26:in `/^I look for "([^"]*)"$/'
Remove return from here:
renewals_page.select_profile.click if element.text == #config[number]
# ⇓⇓⇓⇓⇓⇓
# return element # removed
element # proper working return from lambda: result of last line
end
the reason is that return keyword may be used to return from method only, not from lambda.

How to parse XML with Mechanize and XMLSimple in ruby?

I'm trying to fetch a remote XML file with Mechanize to get icecast status information. But I'm having problems to pass the XML file from Mechanize::File format to string or some XML format which XMLSimple can work with.
The XML document looks like that:
<icestats>
<admin>donschoe#stackoverflow.com</admin>
<!-- ... -->
</icestats>
My code looks like that right now:
require 'mechanize'
require 'xmlsimple'
server = 'example.net'
port = 8000
user = 'stackoverflow'
password = 'hackme'
agent = Mechanize.new
agent.user_agent_alias = 'Linux Firefox'
agent.add_auth("http://#{server}:#{port}/admin/status.xml", user, password)
agent.get("http://#{server}:#{port}/admin/status.xml")
xml = agent.current_page
status = XmlSimple.xml_in(xml)
puts status['admin']
This should output: donschoe#stackoverflow.com
But it throws:
/home/user/.gem/ruby/1.9.1/gems/xml-simple-1.1.2/lib/xmlsimple.rb:191:in 'xml_in': Could not parse object of type: <Mechanize::File>. (ArgumentError)
Now, I understand the XMLSimple needs a string and therefore I tried to convert the Mechanize::File format to string, replacing the second last line with:
status = XmlSimple.xml_in(xml.to_s)
But this throws an even more weird exception:
/usr/lib64/ruby/1.9.1/rexml/parsers/baseparser.rb:406:in `block in pull_event': Undefined prefix Mechanize: found (REXML::UndefinedNamespaceException)
from /usr/lib64/ruby/1.9.1/set.rb:222:in `block in each'
from /usr/lib64/ruby/1.9.1/set.rb:222:in `each_key'
from /usr/lib64/ruby/1.9.1/set.rb:222:in `each'
from /usr/lib64/ruby/1.9.1/rexml/parsers/baseparser.rb:404:in `pull_event'
from /usr/lib64/ruby/1.9.1/rexml/parsers/baseparser.rb:183:in `pull'
from /usr/lib64/ruby/1.9.1/rexml/parsers/treeparser.rb:22:in `parse'
from /usr/lib64/ruby/1.9.1/rexml/document.rb:231:in `build'
from /usr/lib64/ruby/1.9.1/rexml/document.rb:43:in `initialize'
from /home/user/.gem/ruby/1.9.1/gems/xml-simple-1.1.2/lib/xmlsimple.rb:965:in `new'
from /home/user/.gem/ruby/1.9.1/gems/xml-simple-1.1.2/lib/xmlsimple.rb:965:in `parse'
from /home/user/.gem/ruby/1.9.1/gems/xml-simple-1.1.2/lib/xmlsimple.rb:164:in `xml_in'
from /home/user/.gem/ruby/1.9.1/gems/xml-simple-1.1.2/lib/xmlsimple.rb:203:in `xml_in'
from debugging.rb:16:in `<main>'
What's wrong with my approach? When I download the XML file and use the local XML file the code above works as desired.
I'm especially looking for solutions with Mechanize rather than Nokogiri.
Try changing:
xml = agent.current_page
to:
xml = agent.current_page.body

Ruby Spreadsheet: Bad file descriptor - test.xls (Errno::EBADF)

I have problem with script that makes simple .xls file and writes data to one cell. Here is simple code:
require 'spreadsheet'
class Filter
def filter
#excel = Spreadsheet::Workbook.new
#sheet = #excel.create_worksheet
#sheet[0, 0] = "test"
#excel.write 'test.xls'
end
end
f = Filter.new
f.filter
But it raises error:
C:/Ruby193/lib/ruby/gems/1.9.1/gems/ruby-ole-1.2.11.5/lib/ole/storage/base.rb:62:in
write_nonblock': Bad file descriptor - test.xls (Errno::EBADF)
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/ruby-ole-1.2.11.5/lib/ole/storage/base.rb:62:in
initialize'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/ruby-ole-1.2.11.5/lib/ole/storage/base.rb:78:in
new'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/ruby-ole-1.2.11.5/lib/ole/storage/base.rb:78:in
open'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.7.4/lib/spreadsheet/excel/writer/workbook.rb:4
53:in write_from_scratch'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.7.4/lib/spreadsheet/excel/writer/workbook.rb:6
31:inwrite_workbook'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.7.4/lib/spreadsheet/writer.rb:15:in
block in write'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.7.4/lib/spreadsheet/writer.rb:14:in
open'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.7.4/lib/spreadsheet/writer.rb:14:in
write'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.7.4/lib/spreadsheet/workbook.rb:116:in
write'
from filter.rb:10:in `filter'
from filter.rb:15:in `<main>'
because ruby-ole 1.2.11.5 doesn't support windows platform,
more detail: ruby-ole issue
you can use ruby-ole 1.2.11.4 to avoid this problem.
require 'rubygems'
gem 'ruby-ole','1.2.11.4'
require 'spreadsheet'
I've seen these before. First verify that you can write to that file's location.
My guess is either the file is already open in Excel or your antivirus is blocking the 'threat'.

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

Resources