Created
April 16, 2014 23:02
-
-
Save weirdpercent/10940927 to your computer and use it in GitHub Desktop.
Using open-uri to fetch URLs with error-handling.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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