Class: JsonRequester
- Inherits:
-
Object
- Object
- JsonRequester
- Defined in:
- lib/json_requester.rb
Instance Attribute Summary collapse
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
Instance Method Summary collapse
- #form_send(http_method, path, params = {}, headers = {}, sort_params: true, need_response_header: false) ⇒ Object
- #http_send(http_method, path, params = {}, headers = {}, sort_params: true, need_response_header: false, content_type_charset: 'utf-8') ⇒ Object
-
#initialize(host, multipart: false, ssl_verify: true, timeout: 60, user_agent: '') ⇒ JsonRequester
constructor
A new instance of JsonRequester.
- #json_send(http_method, path, params = {}, headers = {}, sort_params: true, need_response_header: false, content_type_charset: 'utf-8') ⇒ Object
- #multipart_form_send(http_method, path, params = {}, headers = {}, sort_params: true, need_response_header: false) ⇒ Object
- #normal_send(http_method, path, params = {}, headers = {}, sort_params: true, need_response_header: false) ⇒ Object
Constructor Details
#initialize(host, multipart: false, ssl_verify: true, timeout: 60, user_agent: '') ⇒ JsonRequester
Returns a new instance of JsonRequester.
7 8 9 10 11 12 13 |
# File 'lib/json_requester.rb', line 7 def initialize(host, multipart: false, ssl_verify: true, timeout: 60, user_agent: '') @host = host @multipart = multipart @ssl_verify = ssl_verify @timeout = timeout @user_agent = user_agent.strip.to_s end |
Instance Attribute Details
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
5 6 7 |
# File 'lib/json_requester.rb', line 5 def conn @conn end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
5 6 7 |
# File 'lib/json_requester.rb', line 5 def host @host end |
Instance Method Details
#form_send(http_method, path, params = {}, headers = {}, sort_params: true, need_response_header: false) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/json_requester.rb', line 49 def form_send(http_method, path, params={}, headers={}, sort_params: true, need_response_header: false) conn = init_conn(sort_params: sort_params) res = conn.send(http_method) do |req| req.url path req.headers = headers if object_present?(headers) req.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8' req.body = URI.encode_www_form(params) if object_present?(params) end process_response(res, need_response_header: need_response_header) rescue => e error_response(e) end |
#http_send(http_method, path, params = {}, headers = {}, sort_params: true, need_response_header: false, content_type_charset: 'utf-8') ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/json_requester.rb', line 15 def http_send(http_method, path, params={}, headers={}, sort_params: true, need_response_header: false, content_type_charset: 'utf-8') puts "send #{http_method} request to #{@host} with\npath: #{path}\nparams: #{params}\nheaders: #{headers}" if http_method == :get normal_send(http_method, path, params, headers, sort_params: sort_params, need_response_header: need_response_header) else json_send(http_method, path, params, headers, sort_params: sort_params, need_response_header: need_response_header, content_type_charset: content_type_charset) end end |
#json_send(http_method, path, params = {}, headers = {}, sort_params: true, need_response_header: false, content_type_charset: 'utf-8') ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/json_requester.rb', line 36 def json_send(http_method, path, params={}, headers={}, sort_params: true, need_response_header: false, content_type_charset: 'utf-8') conn = init_conn(sort_params: sort_params) res = conn.send(http_method) do |req| req.url path req.headers = headers if object_present?(headers) req.headers['Content-Type'] = object_present?(content_type_charset) ? "application/json;charset=#{content_type_charset}" : 'application/json' req.body = params.to_json if object_present?(params) end process_response(res, need_response_header: need_response_header) rescue => e error_response(e) end |
#multipart_form_send(http_method, path, params = {}, headers = {}, sort_params: true, need_response_header: false) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/json_requester.rb', line 62 def multipart_form_send(http_method, path, params={}, headers={}, sort_params: true, need_response_header: false) conn = init_conn(sort_params: sort_params) res = conn.send(http_method) do |req| req.url path req.headers = headers if object_present?(headers) req.body = params if object_present?(params) end process_response(res, need_response_header: need_response_header) end |
#normal_send(http_method, path, params = {}, headers = {}, sort_params: true, need_response_header: false) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/json_requester.rb', line 24 def normal_send(http_method, path, params={}, headers={}, sort_params: true, need_response_header: false) conn = init_conn(sort_params: sort_params) res = conn.send(http_method) do |req| req.url path req.headers = headers if object_present?(headers) req.params = params if object_present?(params) end process_response(res, need_response_header: need_response_header) rescue => e error_response(e) end |