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) ⇒ JsonRequester



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

def initialize(host, multipart: false)
  @host = host
  @multipart = multipart
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



5
6
7
# File 'lib/json_requester.rb', line 5

def conn
  @conn
end

#hostObject (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 = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/json_requester.rb', line 46

def form_send(http_method, path, params={}, headers={})
  conn = init_conn
  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)
rescue => e
  error_response(e)
end

#http_send(http_method, path, params = {}, headers = {}) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/json_requester.rb', line 12

def http_send(http_method, path, params={}, headers={})
  puts "send #{http_method} reqeust to #{@host} with\npath: #{path}\nparams: #{params}\nheaders: #{headers}"
  if http_method == :get
    normal_send(http_method, path, params, headers)
  else
    json_send(http_method, path, params, headers)
  end
end

#json_send(http_method, path, params = {}, headers = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/json_requester.rb', line 33

def json_send(http_method, path, params={}, headers={})
  conn = init_conn
  res = conn.send(http_method) do |req|
    req.url path
    req.headers = headers if object_present?(headers)
    req.headers['Content-Type'] = 'application/json;charset=utf-8'
    req.body = params.to_json if object_present?(params)
  end
  process_response(res)
rescue => e
  error_response(e)
end

#multipart_form_send(http_method, path, params = {}, headers = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/json_requester.rb', line 59

def multipart_form_send(http_method, path, params={}, headers={})
  conn = init_conn
  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)
end

#normal_send(http_method, path, params = {}, headers = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/json_requester.rb', line 21

def normal_send(http_method, path, params={}, headers={})
  conn = init_conn
  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)
rescue => e
  error_response(e)
end