Skip to content

Instantly share code, notes, and snippets.

@mislav
Created February 17, 2011 12:58
Show Gist options
  • Save mislav/831675 to your computer and use it in GitHub Desktop.
Save mislav/831675 to your computer and use it in GitHub Desktop.
Use Net::HTTP in a way that handles gzip/deflate compression transparently
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 <mislav.marohnic@gmail.com>'
# 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