Class: ClientApi::Api

Inherits:
Request show all
Includes:
ClientApi
Defined in:
lib/client-api/base.rb

Constant Summary

Constants included from ClientApi

VERSION

Instance Method Summary collapse

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

#initializeApi

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

#bodyObject 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 } || pdf_response_header
    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, headers = nil)
  @output = delete_request(url_generator(url), :headers => 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, headers = nil)
  @output = delete_with_body_request(url_generator(url), :body => body, :headers => 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, headers = nil)
  @output = get_request(url_generator(url), :headers => 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, headers = nil)
  @output = get_with_body_request(url_generator(url), :body => body, :headers => headers)
  self.post_logger if $logger
  self.output_json_body if json_output
end

#messageObject



102
103
104
# File 'lib/client-api/base.rb', line 102

def message
  @output.message
end

#output_json_bodyObject



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 } || pdf_response_header
    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, headers = nil)
  @output = patch_request(url_generator(url), :body => body, :headers => headers)
  self.post_logger if $logger
  self.output_json_body if json_output
end

#pdf_response_headerObject



93
94
95
96
97
98
99
100
# File 'lib/client-api/base.rb', line 93

def pdf_response_header
  response_headers.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, headers = nil)
  if body.is_a? Hash
    if body['type'] && body['data']
      @output = post_request_x(url_generator(url), :body => body, :headers => headers)
    else
      @output = post_request(url_generator(url), :body => body, :headers => headers)
    end
  else
    raise 'invalid body'
  end

  self.post_logger if $logger
  self.output_json_body if json_output
end

#post_loggerObject



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 }) || pdf_response_header) ? res_body = 'empty response body' : res_body = body

  $logger.debug("Response code == #{@output.code.to_i}")
  $logger.debug("Response body == #{res_body}")

  log_headers = {}
  @output.response.each { |key, value|  log_headers.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, headers = nil)
  @output = put_request(url_generator(url), :body => body, :headers => headers)
  self.post_logger if $logger
  self.output_json_body if json_output
end

#response_headersObject



88
89
90
91
# File 'lib/client-api/base.rb', line 88

def response_headers
  resp_headers = {}
  @output.response.each { |key, value|  resp_headers.merge!(key.to_s => value.to_s) }
end

#statusObject Also known as: code



64
65
66
# File 'lib/client-api/base.rb', line 64

def status
  @output.code.to_i
end