Created
February 17, 2011 12:58
-
-
Save mislav/831675 to your computer and use it in GitHub Desktop.
Use Net::HTTP in a way that handles gzip/deflate compression transparently
This file contains 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 'addressable/uri' | |
require 'net/http' | |
require 'yajl' | |
# Wikipedia API | |
url = Addressable::URI.parse 'http://en.wikipedia.org/w/api.php?format=json' | |
# let Addressable handle query encoding | |
url.query_values = url.query_values.update :srsearch => 'Scott Pilgrim', :action => 'query', :list => 'search' | |
response = Net::HTTP.start(url.host, url.port) { |http| | |
http.get url.request_uri, 'user-agent' => 'Ruby app <[email protected]>' | |
# if Zlib is available, this also sets: | |
# Accept-encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3 | |
} | |
# raises an instance of Net::HTTPExceptions exception | |
response.error! unless Net::HTTPSuccess === response | |
# response.body is automatically uncompressed | |
data = Yajl::Parser.parse response.body | |
results = data['query']['search'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment