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

Returns a new instance of JsonRequester.



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

def initialize(host)
  @host = host
  @conn = init_conn
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



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/json_requester.rb', line 31

def form_send(http_method, path, params={}, headers={})
  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
# File 'lib/json_requester.rb', line 12

def http_send(http_method, path, params={}, 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



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/json_requester.rb', line 43

def json_send(http_method, path, params={}, headers={})
  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

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



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

def normal_send(http_method, path, params={}, headers={})
  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