Sha256: 273a2684aaabc4b18517732bb73a946bbd2eb33421b6d0e1745293ffcdb2b70e

Contents?: true

Size: 1.64 KB

Versions: 19

Compression:

Stored size: 1.64 KB

Contents

require 'httpi'
require 'multi_json'

HTTPI.log = false

module Bixby

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

  # Execute an HTTP GET request to the given URL
  #
  # @param [String] url
  # @return [String] Contents of the response's body
  def http_get(url)
    HTTPI.get(url).body
  end

  # 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:
  def http_get_json(url)
    MultiJson.load(http_get(url))
  end
  # :nocov:

  # 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
  def http_post(url, data)
    req = HTTPI::Request.new(:url => url, :body => data)
    return HTTPI.post(req).body
  end

  # 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:
  def http_post_json(url, data)
    MultiJson.load(http_post(url, data))
  end
  # :nocov:

  # Execute an HTTP post request and save the response body
  #
  # @param [String] url
  # @param [Hash] data  Key/Value pairs to POST
  # @return [void]
  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

end # HttpClient

end # Bixby

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
bixby-common-0.7.1 lib/bixby-common/util/http_client.rb
bixby-common-0.7.0 lib/bixby-common/util/http_client.rb
bixby-common-0.6.6 lib/bixby-common/util/http_client.rb
bixby-common-0.6.5 lib/bixby-common/util/http_client.rb
bixby-common-0.6.4 lib/bixby-common/util/http_client.rb
bixby-common-0.6.3 lib/bixby-common/util/http_client.rb
bixby-common-0.6.2 lib/bixby-common/util/http_client.rb
bixby-common-0.6.1 lib/bixby-common/util/http_client.rb
bixby-common-0.6.0 lib/bixby-common/util/http_client.rb
bixby-common-0.5.0 lib/bixby-common/util/http_client.rb
bixby-common-0.4.13 lib/bixby-common/util/http_client.rb
bixby-common-0.4.12 lib/bixby-common/util/http_client.rb
bixby-common-0.4.11 lib/bixby-common/util/http_client.rb
bixby-common-0.4.10 lib/bixby-common/util/http_client.rb
bixby-common-0.4.9 lib/bixby-common/util/http_client.rb
bixby-common-0.4.8 lib/bixby-common/util/http_client.rb
bixby-common-0.4.7 lib/bixby-common/util/http_client.rb
bixby-common-0.4.6 lib/bixby-common/util/http_client.rb
bixby-common-0.4.5 lib/bixby-common/util/http_client.rb