Class: ClientApi::Api
Constant Summary
Constants included
from ClientApi
VERSION
Instance Method Summary
collapse
-
#body ⇒ Object
(also: #resp)
-
#delete(url, headers = nil) ⇒ Object
-
#delete_with_body(url, body = nil, headers = nil) ⇒ Object
-
#get(url, headers = nil) ⇒ Object
-
#get_with_body(url, body = nil, headers = nil) ⇒ Object
-
#initialize ⇒ Api
constructor
-
#message ⇒ Object
-
#output_json_body ⇒ Object
-
#patch(url, body, headers = nil) ⇒ Object
-
#pdf_response_header ⇒ Object
-
#post(url, body, headers = nil) ⇒ Object
-
#post_logger ⇒ Object
-
#put(url, body, headers = nil) ⇒ Object
-
#response_headers ⇒ Object
-
#status ⇒ Object
(also: #code)
Methods included from ClientApi
#base_url, #basic_auth, configuration, configure, #datatype, #deep_traverse, #headers, #is_num?, #json_output, #payload, #time_out, #url_generator, #validate, #validate_empty, #validate_headers, #validate_json, #validate_key, #validate_list, #validate_schema, #validate_size
Methods inherited from Request
#base_url_definition, #basic_encode, #connect, #delete_request, #delete_with_body_request, #get_request, #get_with_body_request, #header, #patch_request, #post_request, #post_request_x, #pre_logger, #put_request, #uri
Constructor Details
#initialize ⇒ Api
Returns a new instance of Api.
9
10
11
|
# File 'lib/client-api/base.rb', line 9
def initialize
((FileUtils.rm Dir.glob("./#{json_output['Dirname']}/*.json"); $roo = true)) if json_output && $roo == nil
end
|
Instance Method Details
#body ⇒ Object
Also known as:
resp
68
69
70
71
72
|
# File 'lib/client-api/base.rb', line 68
def body
unless ['', nil, '{}'].any? { |e| e == @output.body } ||
JSON.parse(%{#{@output.body}})
end
end
|
#delete(url, headers = nil) ⇒ Object
40
41
42
43
44
|
# File 'lib/client-api/base.rb', line 40
def delete(url, = nil)
@output = delete_request(url_generator(url), :headers => )
self.post_logger if $logger
self.output_json_body if json_output
end
|
#delete_with_body(url, body = nil, headers = nil) ⇒ Object
46
47
48
49
50
|
# File 'lib/client-api/base.rb', line 46
def delete_with_body(url, body = nil, = nil)
@output = delete_with_body_request(url_generator(url), :body => body, :headers => )
self.post_logger if $logger
self.output_json_body if json_output
end
|
#get(url, headers = nil) ⇒ Object
13
14
15
16
17
|
# File 'lib/client-api/base.rb', line 13
def get(url, = nil)
@output = get_request(url_generator(url), :headers => )
self.post_logger if $logger
self.output_json_body if json_output
end
|
#get_with_body(url, body = nil, headers = nil) ⇒ Object
19
20
21
22
23
|
# File 'lib/client-api/base.rb', line 19
def get_with_body(url, body = nil, = nil)
@output = get_with_body_request(url_generator(url), :body => body, :headers => )
self.post_logger if $logger
self.output_json_body if json_output
end
|
#message ⇒ Object
102
103
104
|
# File 'lib/client-api/base.rb', line 102
def message
@output.message
end
|
#output_json_body ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/client-api/base.rb', line 74
def output_json_body
unless ['', nil, '{}'].any? { |e| e == @output.body } ||
unless json_output['Dirname'] == nil
FileUtils.mkdir_p "#{json_output['Dirname']}"
time_now = (Time.now.to_f).to_s.gsub('.','')
begin
File.open("./#{json_output['Dirname']}/#{json_output['Filename']+"_"+time_now}""#{time_now}"".json", "wb") {|file| file.puts JSON.pretty_generate(JSON.parse(@output.body))}
rescue StandardError => e
raise("\n"+" Not a compatible (or) Invalid JSON response => [kindly check the uri & request details]".brown + " \n\n #{e.message}")
end
end
end
end
|
#patch(url, body, headers = nil) ⇒ Object
58
59
60
61
62
|
# File 'lib/client-api/base.rb', line 58
def patch(url, body, = nil)
@output = patch_request(url_generator(url), :body => body, :headers => )
self.post_logger if $logger
self.output_json_body if json_output
end
|
93
94
95
96
97
98
99
100
|
# File 'lib/client-api/base.rb', line 93
def
.map do |data|
if data[0].downcase == 'Content-Type'.downcase && (data[1][0].include? 'application/pdf')
return true
end
end
false
end
|
#post(url, body, headers = nil) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/client-api/base.rb', line 25
def post(url, body, = nil)
if body.is_a? Hash
if body['type'] && body['data']
@output = post_request_x(url_generator(url), :body => body, :headers => )
else
@output = post_request(url_generator(url), :body => body, :headers => )
end
else
raise 'invalid body'
end
self.post_logger if $logger
self.output_json_body if json_output
end
|
#post_logger ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/client-api/base.rb', line 106
def post_logger
((['', nil, '{}'].any? { |e| e == @output.body }) || ) ? res_body = 'empty response body' : res_body = body
$logger.debug("Response code == #{@output.code.to_i}")
$logger.debug("Response body == #{res_body}")
= {}
@output.response.each { |key, value| .merge!(key.to_s => value.to_s) }
$logger.debug("Response headers == #{log_headers}")
$logger.debug("=====================================================================================")
end
|
#put(url, body, headers = nil) ⇒ Object
52
53
54
55
56
|
# File 'lib/client-api/base.rb', line 52
def put(url, body, = nil)
@output = put_request(url_generator(url), :body => body, :headers => )
self.post_logger if $logger
self.output_json_body if json_output
end
|
88
89
90
91
|
# File 'lib/client-api/base.rb', line 88
def
= {}
@output.response.each { |key, value| .merge!(key.to_s => value.to_s) }
end
|
#status ⇒ Object
Also known as:
code
64
65
66
|
# File 'lib/client-api/base.rb', line 64
def status
@output.code.to_i
end
|