Skip to content

Instantly share code, notes, and snippets.

@lgranger
Last active January 4, 2016 23:41
Show Gist options
  • Save lgranger/81be45d3d066a98756b9 to your computer and use it in GitHub Desktop.
Save lgranger/81be45d3d066a98756b9 to your computer and use it in GitHub Desktop.

When I was learning API's I ran across this error:

ruby/2.2.0/uri/rfc3986_parser.rb:66:in `split': bad URI(is not URI?): (URI::InvalidURIError)

The error came from passing a string with a space in it into string interpolation in an API URI.nThis is was confusing because I had passed in the same string into another API URI and it was fine. When you pass a string "like this" into the query part of the URI then the browser knows what to do. When you pass "like this" into a different part of the string it doesn't know what to do and do it breaks.

WEBrick will do the ruby-string to URI-string conversion for you. To use it require 'WEBrick'and then call WEBrick::HTTPUtils.escape passing in the string you want converted as a parameter.

Here's how I used it in my code:


require 'httparty'
require 'pry'
require 'WEBrick'

class FavoriteBooks
  attr_accessor :search_term
  def initialize(tittle_or_author)
    @search_term = WEBrick::HTTPUtils.escape(tittle_or_author)
  end
  def author_info_by_name
    author_info = HTTParty.get("https://www.goodreads.com/api/author_url/#{@search_term}?key=APIKEY").parsed_response
  end
end

fav_book = FavoriteBooks.new("Barbara Kingsolver")
puts(fav_book.author_info_by_name)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment