Sha256: fc630afe427b6ccf2bf985ab7882e396705ca97109a2901aeb2caf7e40e2b05b

Contents?: true

Size: 1.73 KB

Versions: 71

Compression:

Stored size: 1.73 KB

Contents

require 'httpclient'
require 'json'
 
# JSONClient auto-converts Hash <-> JSON in request and response.
# * For POST or PUT request, convert Hash body to JSON String with 'application/json; charset=utf-8' header.
# * For response, convert JSON String to Hash when content-type is '(application|text)/(x-)?json'
class JSONClient < HTTPClient
  CONTENT_TYPE_JSON_REGEX = /(application|text)\/(x-)?json/i
  CONTENT_TYPE_JSON = 'application/json; charset=utf-8'

  attr_reader :content_type_json_request
  attr_reader :content_type_json_response_regex
 
  def initialize(*args)
    super
    @content_type_json_request = CONTENT_TYPE_JSON
    @content_type_json_response_regex = CONTENT_TYPE_JSON_REGEX
  end
 
  def post(uri, *args, &block)
    request(:post, uri, argument_to_hash_for_json(args), &block)
  end
 
  def put(uri, *args, &block)
    request(:put, uri, argument_to_hash_for_json(args), &block)
  end

  def request(method, uri, *args, &block)
    res = super
    if @content_type_json_response_regex =~ res.content_type
      res = wrap_json_response(res)
    end
    res
  end
 
private

  def argument_to_hash_for_json(args)
    hash = argument_to_hash(args, :body, :header, :follow_redirect)
    if hash[:body].is_a?(Hash)
      hash[:header] = json_header(hash[:header])
      hash[:body] = JSON.generate(hash[:body])
    end
    hash
  end

  def json_header(header)
    header ||= {}
    if header.is_a?(Hash)
      header['Content-Type'] = @content_type_json_request
    else
      header << ['Content-Type', @content_type_json_request]
    end
    header
  end

  def wrap_json_response(original)
    res = ::HTTP::Message.new_response(JSON.parse(original.content))
    res.http_header = original.http_header
    res.previous = original
    res
  end
end

Version data entries

71 entries across 63 versions & 7 rubygems

Version Path
vagrant-unbundled-1.8.4.1 vendor/bundle/ruby/2.3.0/gems/httpclient-2.8.0/lib/jsonclient.rb
httpclient-2.8.0 lib/jsonclient.rb
httpclient-2.7.2 lib/jsonclient.rb
vagrant-unbundled-1.8.1.1 vendor/bundle/ruby/2.3.0/gems/httpclient-2.7.1/lib/jsonclient.rb
httpclient-2.7.1 lib/jsonclient.rb
httpclient-2.7.0.1 lib/jsonclient.rb
httpclient-2.7.0 lib/jsonclient.rb
vagrant-cloudstack-1.2.0 vendor/bundle/gems/httpclient-2.6.0.1/lib/jsonclient.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/httpclient-2.6.0.1/lib/jsonclient.rb
httpclient-2.6.0.1 lib/jsonclient.rb
httpclient-2.6.0 lib/jsonclient.rb