Sha256: f9d3f10339bf1f2d9307254f124a1b511844edd96bf7412f017691ff9fcf104a

Contents?: true

Size: 1.49 KB

Versions: 78

Compression:

Stored size: 1.49 KB

Contents

require 'httpclient'
require 'json'
 
module HTTP
  class Message
    # Returns JSON object of message body
    alias original_content content
    def content
      if JSONClient::CONTENT_TYPE_JSON_REGEX =~ content_type
        JSON.parse(original_content)
      else
        original_content
      end
    end
  end
end
 
 
# JSONClient provides JSON related methods in addition to HTTPClient.
class JSONClient < HTTPClient
  CONTENT_TYPE_JSON_REGEX = /(application|text)\/(x-)?json/i
 
  attr_accessor :content_type_json
 
  class JSONRequestHeaderFilter
    attr_accessor :replace
 
    def initialize(client)
      @client = client
      @replace = false
    end
 
    def filter_request(req)
      req.header['content-type'] = @client.content_type_json if @replace
    end
 
    def filter_response(req, res)
      @replace = false
    end
  end
 
  def initialize(*args)
    super
    @header_filter = JSONRequestHeaderFilter.new(self)
    @request_filter << @header_filter
    @content_type_json = 'application/json; charset=utf-8'
  end
 
  def post(uri, *args, &block)
    @header_filter.replace = true
    request(:post, uri, jsonify(argument_to_hash(args, :body, :header, :follow_redirect)), &block)
  end
 
  def put(uri, *args, &block)
    @header_filter.replace = true
    request(:put, uri, jsonify(argument_to_hash(args, :body, :header)), &block)
  end
 
private
 
  def jsonify(hash)
    if hash[:body] && hash[:body].is_a?(Hash)
      hash[:body] = JSON.generate(hash[:body])
    end
    hash
  end
end

Version data entries

78 entries across 70 versions & 8 rubygems

Version Path
vagrant-unbundled-2.0.2.0 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/sample/jsonclient.rb
vagrant-unbundled-2.0.1.0 vendor/bundle/ruby/2.4.0/gems/httpclient-2.8.3/sample/jsonclient.rb
vagrant-unbundled-2.0.0.1 vendor/bundle/ruby/2.4.0/gems/httpclient-2.8.3/sample/jsonclient.rb
vagrant-unbundled-1.9.8.1 vendor/bundle/ruby/2.4.0/gems/httpclient-2.8.3/sample/jsonclient.rb
vagrant-unbundled-1.9.7.1 vendor/bundle/ruby/2.4.0/gems/httpclient-2.8.3/sample/jsonclient.rb
vagrant-unbundled-1.9.5.1 vendor/bundle/ruby/2.4.0/gems/httpclient-2.8.3/sample/jsonclient.rb
vagrant-unbundled-1.9.1.1 vendor/bundle/ruby/2.4.0/gems/httpclient-2.8.3/sample/jsonclient.rb
httpclient-2.8.3 sample/jsonclient.rb
vagrant-compose-yaml-0.1.3 vendor/bundle/ruby/2.2.0/gems/httpclient-2.8.0/sample/jsonclient.rb
vagrant-compose-yaml-0.1.2 vendor/bundle/ruby/2.2.0/gems/httpclient-2.8.0/sample/jsonclient.rb
vagrant-compose-yaml-0.1.1 vendor/bundle/ruby/2.2.0/gems/httpclient-2.8.0/sample/jsonclient.rb
vagrant-compose-yaml-0.1.0 vendor/bundle/ruby/2.2.0/gems/httpclient-2.8.0/sample/jsonclient.rb
httpclient-2.8.2.4 sample/jsonclient.rb
httpclient-2.8.2.3 sample/jsonclient.rb
httpclient-2.8.2.2 sample/jsonclient.rb
httpclient-2.8.2.1 sample/jsonclient.rb
httpclient-2.8.2 sample/jsonclient.rb
httpclient-2.8.1 sample/jsonclient.rb
vagrant-unbundled-1.8.5.2 vendor/bundle/ruby/2.3.0/gems/httpclient-2.8.0/sample/jsonclient.rb
vagrant-unbundled-1.8.5.1 vendor/bundle/ruby/2.3.0/gems/httpclient-2.8.0/sample/jsonclient.rb