Class: JsonRequester

Inherits:
Object
  • Object
show all
Defined in:
lib/json_requester.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, multipart: false, ssl_verify: true, timeout: 60, user_agent: "") ⇒ JsonRequester

Returns a new instance of JsonRequester.



10
11
12
13
14
15
16
# File 'lib/json_requester.rb', line 10

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

#connObject (readonly)

Returns the value of attribute conn.



8
9
10
# File 'lib/json_requester.rb', line 8

def conn
  @conn
end

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/json_requester.rb', line 8

def host
  @host
end

Instance Method Details

#form_send(http_method, path, params = {}, headers = {}, sort_params: true, need_response_header: false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/json_requester.rb', line 52

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 StandardError => 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



18
19
20
21
22
23
24
25
# File 'lib/json_requester.rb', line 18

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



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/json_requester.rb', line 39

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 StandardError => e
  error_response(e)
end

#multipart_form_send(http_method, path, params = {}, headers = {}, sort_params: true, need_response_header: false) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/json_requester.rb', line 65

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



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/json_requester.rb', line 27

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 StandardError => e
  error_response(e)
end