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

Instance Method Details

#http_get(url) ⇒ String

Execute an HTTP GET request to the given URL

Parameters:

  • url (String)

Returns:

  • (String)

    Contents of the response’s body



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

:nocov:

Parameters:

  • url (String)

Returns:

  • (Object)

    Result of calling JSON.parse() on the response body



26
27
28
# 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) ⇒ String

Execute an HTTP POST request to the given URL

Parameters:

  • url (String)
  • data (Hash)

    Key/Value pairs to POST

Returns:

  • (String)

    Contents of the response’s body



36
37
38
39
# 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) ⇒ void

This method returns an undefined value.

Execute an HTTP post request and save the response body

Parameters:

  • url (String)
  • data (Hash)

    Key/Value pairs to POST



57
58
59
60
61
62
63
64
# 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) ⇒ Object

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

:nocov:

Parameters:

  • url (String)
  • data (Hash)

    Key/Value pairs to POST

Returns:

  • (Object)

    Result of calling JSON.parse() on the response body



47
48
49
# File 'lib/bixby-common/util/http_client.rb', line 47

def http_post_json(url, data)
  MultiJson.load(http_post(url, data))
end