Skip to content

Instantly share code, notes, and snippets.

@weirdpercent
Created April 16, 2014 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weirdpercent/10940927 to your computer and use it in GitHub Desktop.
Save weirdpercent/10940927 to your computer and use it in GitHub Desktop.
Using open-uri to fetch URLs with error-handling.
require 'multi_json'
require 'open-uri/cached'
def search(url, result)
OpenURI::Cache.cache_path = 'tmp/open-uri'
uri=URI.parse(url)
status=uri.open.meta[:status]
if status[0] == "200"
result=MultiJson.load(uri.open.read)
else
puts "HTTP Status #{status[0]} #{status[1]}"
result=[]
end
return result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment