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
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/httpclient-2.8.3/sample/jsonclient.rb
httpclient-fixcerts-2.8.5 sample/jsonclient.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/httpclient-2.8.3/sample/jsonclient.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/httpclient-2.8.3/sample/jsonclient.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/sample/jsonclient.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/3.0.0/gems/httpclient-2.8.3/sample/jsonclient.rb
video_chat_get-0.1.9 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/sample/jsonclient.rb
vagrant-unbundled-2.2.14.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/sample/jsonclient.rb
video_chat_get-0.1.6 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/sample/jsonclient.rb
vagrant-unbundled-2.2.10.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/sample/jsonclient.rb
video_chat_get-0.1.5 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/sample/jsonclient.rb
video_chat_get-0.1.4 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/sample/jsonclient.rb
video_chat_get-0.1.3 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/sample/jsonclient.rb
video_chat_get-0.1.2 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/sample/jsonclient.rb
video_chat_get-0.1.1 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/sample/jsonclient.rb
video_chat_get-0.1.0 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/sample/jsonclient.rb
vagrant-unbundled-2.2.9.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/sample/jsonclient.rb
vagrant-unbundled-2.2.8.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/sample/jsonclient.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/sample/jsonclient.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.4.0/gems/httpclient-2.8.3/sample/jsonclient.rb