Parsing Uris That Have Curly Braces, Uri::Invalidurierror: Bad Uri(Is Not Uri)

URI::InvalidURIError bad URI(is not URI?): using Tanker

That's a URI.parse exception, it means some url is incorrectly specified or generated. Are you sure you set up the config correctly? From the readme at https://github.com/kidpollo/tanker you need to do:

Initialization

If you’re using Rails, config/initializers/tanker.rb is a good place
for this:

YourAppName::Application.config.index_tank_url = 'http://:xxxxxxxxx@xxxxx.api.indextank.com'

If you are not using rails you can put this somewhere before you load
your models

Tanker.configuration = {:url => 'http://:xxxxxxxxx@xxxxx.api.indextank.com' }

You would probably want to have fancier configuration depending on
your environment. Be sure to copy and paste the correct url provided
by the IndexTank Dashboard

If you've already done that, please double check the urls for typos.

Sinatra/Rack `ERROR URI::InvalidURIError: bad URI(is not URI?)` on redirect

In the line

redirect to('/show/#{URI.escape((result.keys).first)}')

you are using single quotes. This means that string interpolation isn’t performed, so the literal string /show/#{URI.escape((result.keys).first)} is being used as the url and that is why it is failing.

In order for interpolation to work you need to use double quotes like this:

redirect to("/show/#{URI.escape((result.keys).first)}")

This will cause #{URI.escape((result.keys).first)} to be replaced with the escaped movie name, which should be a valid url.

Note that in your first redirect you do use double quotes, so it works as expected:

redirect to("/search/#{URI.escape(params['search'])}")

Open URI - Invalid URI Error, encoding/escaping not affecting

Don't try to inject variables into URLs. If they contain characters that need to be encoded per the spec, they won't be by interpolation. Instead, take advantage of the right tools for the job, like Ruby's URI class or the Addressable::URI gem.

See "How to post a URL containting curly braces and colons" for how to do this using well tested wheels.

In your situation, something like this will work:

require 'uri'

code = 'qwer3456*&^%'
start_month = 1
start_day = 1
start_year = 2014
end_month = 12
end_day = 31
end_year = 2015

uri = URI.parse("http://ichart.finance.yahoo.com/table.csv")
uri.query = URI.encode_www_form(
{
'g' => 'd',
'ignore' => '.csv',
's' => code,
'a' => start_month,
'b' => start_day,
'c' => start_year,
'd' => end_month,
'e' => end_day,
'f' => end_year
}
)
uri.to_s # => "http://ichart.finance.yahoo.com/table.csv?g=d&ignore=.csv&s=qwer3456*%26%5E%25&a=1&b=1&c=2014&d=12&e=31&f=2015"

URI.parse documentation

It seems URI(url) and URI.parse(url) do exactly the same:

u1 = URI("http://stackoverflow.com/")
u2 = URI.parse("http://stackoverflow.com/")
u1 == u2 # => true


Related Topics



Leave a reply



Submit