Class: JSONClient

Inherits:
HTTPClient show all
Defined in:
lib/jsonclient.rb

Overview

JSONClient auto-converts Hash <-> JSON in request and response.

  • For PATCH, 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’

Constant Summary collapse

CONTENT_TYPE_JSON_REGEX =
/(application|text)\/(x-)?json/i
CONTENT_TYPE_JSON =
'application/json; charset=utf-8'

Constants inherited from HTTPClient

HTTPClient::CookieManager, HTTPClient::DEFAULT_AGENT_NAME, HTTPClient::GSSAPIEnabled, HTTPClient::LIB_NAME, HTTPClient::NTLMEnabled, HTTPClient::PROPFIND_DEFAULT_EXTHEADER, HTTPClient::RUBY_VERSION_STRING, HTTPClient::SSPIEnabled, HTTPClient::VERSION

Instance Attribute Summary collapse

Attributes inherited from HTTPClient

#base_url, #cookie_manager, #default_header, #follow_redirect_count, #proxy_auth, #request_filter, #ssl_config, #test_loopback_response, #www_auth

Instance Method Summary collapse

Methods inherited from HTTPClient

#cookies, #debug_dev, #debug_dev=, #default_redirect_uri_callback, #delete, #delete_async, #force_basic_auth=, #get, #get_async, #get_content, #head, #head_async, #keep_webmock_compat, #no_proxy, #no_proxy=, #options, #options_async, #patch_async, #post_async, #post_content, #propfind, #propfind_async, #proppatch, #proppatch_async, #proxy, #proxy=, #put_async, #redirect_uri_callback=, #request_async, #request_async2, #reset, #reset_all, #save_cookie_store, #set_auth, #set_basic_auth, #set_cookie_store, #set_proxy_auth, #strict_redirect_uri_callback, timeout_scheduler, #trace, #trace_async

Methods included from HTTPClient::Util

#argument_to_hash, hash_find_value, #http?, #https?, #keyword_argument, try_require, uri_dirname, uri_part_of, urify, #warning

Constructor Details

#initialize(*args) ⇒ JSONClient

Returns a new instance of JSONClient.



14
15
16
17
18
# File 'lib/jsonclient.rb', line 14

def initialize(*args)
  super
  @content_type_json_request = CONTENT_TYPE_JSON
  @content_type_json_response_regex = CONTENT_TYPE_JSON_REGEX
end

Instance Attribute Details

#content_type_json_requestObject (readonly)

Returns the value of attribute content_type_json_request.



11
12
13
# File 'lib/jsonclient.rb', line 11

def content_type_json_request
  @content_type_json_request
end

#content_type_json_response_regexObject (readonly)

Returns the value of attribute content_type_json_response_regex.



12
13
14
# File 'lib/jsonclient.rb', line 12

def content_type_json_response_regex
  @content_type_json_response_regex
end

Instance Method Details

#patch(uri, *args, &block) ⇒ Object



20
21
22
# File 'lib/jsonclient.rb', line 20

def patch(uri, *args, &block)
  request(:patch, uri, argument_to_hash_for_json(args), &block)
end

#post(uri, *args, &block) ⇒ Object



24
25
26
# File 'lib/jsonclient.rb', line 24

def post(uri, *args, &block)
  request(:post, uri, argument_to_hash_for_json(args), &block)
end

#put(uri, *args, &block) ⇒ Object



28
29
30
# File 'lib/jsonclient.rb', line 28

def put(uri, *args, &block)
  request(:put, uri, argument_to_hash_for_json(args), &block)
end

#request(method, uri, *args, &block) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/jsonclient.rb', line 32

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