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

Parameters:

  • url (String)

Returns:

  • (Object)

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



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

Parameters:

  • url (String)
  • data (Hash)

    Key/Value pairs to POST

Returns:

  • (String)

    Contents of the response’s body



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

Parameters:

  • url (String)
  • data (Hash)

    Key/Value pairs to POST



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

Parameters:

  • url (String)
  • data (Hash)

    Key/Value pairs to POST

Returns:

  • (Object)

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



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