Module: Bixby::HttpClient
- Included in:
- BaseModule, JsonRequest
- Defined in:
- lib/bixby_common/util/http_client.rb
Overview
Utilities for creating HTTP Clients. Just a thin wrapper around curb and JSON for common cases.
Instance Method Summary collapse
-
#create_post_data(data) ⇒ Object
Convert a Hash into a Curl::Postfield Array.
-
#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
#create_post_data(data) ⇒ Object
Convert a Hash into a Curl::Postfield Array
30 31 32 33 34 35 |
# File 'lib/bixby_common/util/http_client.rb', line 30 def create_post_data(data) if data.kind_of? Hash then data = data.map{ |k,v| Curl::PostField.content(k, v) } end data end |
#http_get(url) ⇒ String
Execute an HTTP GET request to the given URL
15 16 17 |
# File 'lib/bixby_common/util/http_client.rb', line 15 def http_get(url) Curl::Easy.http_get(url).body_str end |
#http_get_json(url) ⇒ Object
Execute an HTTP GET request (see #http_get) and parse the JSON response
23 24 25 |
# File 'lib/bixby_common/util/http_client.rb', line 23 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
42 43 44 |
# File 'lib/bixby_common/util/http_client.rb', line 42 def http_post(url, data) return Curl::Easy.http_post(url, create_post_data(data)).body_str end |
#http_post_download(url, data, dest) ⇒ void
This method returns an undefined value.
Execute an HTTP post request and save the response body
60 61 62 63 64 65 66 |
# File 'lib/bixby_common/util/http_client.rb', line 60 def http_post_download(url, data, dest) File.open(dest, "w") do |io| c = Curl::Easy.new(url) c.on_body { |d| io << d; d.length } c.http_post(data) end end |
#http_post_json(url, data) ⇒ Object
Execute an HTTP POST request (see #http_get) and parse the JSON response
51 52 53 |
# File 'lib/bixby_common/util/http_client.rb', line 51 def http_post_json(url, data) MultiJson.load(http_post(url, data)) end |