module Bixby::HttpClient

Utilities for using HTTP Clients. Just a thin wrapper around httpi and JSON for common cases.

Public Instance Methods

http_get(url) click to toggle source

Execute an HTTP GET request to the given URL

@param [String] url @return [String] Contents of the response’s body

# File lib/bixby-common/util/http_client.rb, line 17
def http_get(url)
  HTTPI.get(url).body
end
http_get_json(url) click to toggle source

Execute an HTTP GET request (see http_get) and parse the JSON response

@param [String] url @return [Object] Result of calling JSON.parse() on the response body :nocov:

# File lib/bixby-common/util/http_client.rb, line 26
def http_get_json(url)
  MultiJson.load(http_get(url))
end
http_post(url, data) click to toggle source

Execute an HTTP POST request to the given URL

@param [String] url @param [Hash] data Key/Value pairs to POST @return [String] Contents of the response’s body

# File lib/bixby-common/util/http_client.rb, line 36
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) click to toggle source

Execute an HTTP post request and save the response body

@param [String] url @param [Hash] data Key/Value pairs to POST @return [void]

# File lib/bixby-common/util/http_client.rb, line 57
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) click to toggle source

Execute an HTTP POST request (see http_get) and parse the JSON response

@param [String] url @param [Hash] data Key/Value pairs to POST @return [Object] Result of calling JSON.parse() on the response body :nocov:

# File lib/bixby-common/util/http_client.rb, line 47
def http_post_json(url, data)
  MultiJson.load(http_post(url, data))
end