Module: Bixby::HttpClient
- Included in:
- JsonRequest
- Defined in:
- lib/bixby-common/util/http_client.rb
Overview
Utilities for using HTTP Clients. Just a thin wrapper around httpi and JSON for common cases.
Instance Method Summary collapse
-
#http_get(url) ⇒ String
Execute an HTTP GET request to the given URL.
-
#http_get_json(url) ⇒ Object
Execute an HTTP GET request (see #http_get) and parse the JSON response.
-
#http_post(url, data) ⇒ String
Execute an HTTP POST request to the given URL.
-
#http_post_download(url, data, dest) ⇒ void
Execute an HTTP post request and save the response body.
-
#http_post_json(url, data) ⇒ Object
Execute an HTTP POST request (see #http_get) and parse the JSON response.
Instance Method Details
#http_get(url) ⇒ String
Execute an HTTP GET request to the given URL
17 18 19 |
# File 'lib/bixby-common/util/http_client.rb', line 17 def http_get(url) HTTPI.get(url).body end |
#http_get_json(url) ⇒ Object
Execute an HTTP GET request (see #http_get) and parse the JSON response
25 26 27 |
# File 'lib/bixby-common/util/http_client.rb', line 25 def http_get_json(url) MultiJson.load(http_get(url)) end |
#http_post(url, data) ⇒ String
Execute an HTTP POST request to the given URL
34 35 36 37 |
# File 'lib/bixby-common/util/http_client.rb', line 34 def http_post(url, data) req = HTTPI::Request.new(:url => url, :body => data) return HTTPI.post(req).body end |
#http_post_download(url, data, dest) ⇒ void
This method returns an undefined value.
Execute an HTTP post request and save the response body
53 54 55 56 57 58 59 60 |
# File 'lib/bixby-common/util/http_client.rb', line 53 def http_post_download(url, data, dest) File.open(dest, "w") do |io| req = HTTPI::Request.new(:url => url, :body => data) req.on_body { |d| io << d; d.length } HTTPI.post(req) end true end |
#http_post_json(url, data) ⇒ Object
Execute an HTTP POST request (see #http_get) and parse the JSON response
44 45 46 |
# File 'lib/bixby-common/util/http_client.rb', line 44 def http_post_json(url, data) MultiJson.load(http_post(url, data)) end |