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, #validate, #validate_headers, #validate_json, #validate_schema

Methods inherited from Request

#basic_encode, #connect, #delete_request, #get_request, #header, #patch_request, #post_request, #pre_logger, #put_request, #uri

Constructor Details

#initializeApi


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


47
48
49
50
51
# File 'lib/client-api/base.rb', line 47

def body
  unless @output.body == "" || @output.body.nil? || @output.body == "{}"
    JSON.parse(@output.body)
  end
end

#delete(url, headers = nil) ⇒ Object


25
26
27
28
29
# File 'lib/client-api/base.rb', line 25

def delete(url, headers = nil)
  @output = delete_request(url, :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, :headers => headers)
  self.post_logger if $logger
  self.output_json_body if json_output
end

#messageObject


72
73
74
# File 'lib/client-api/base.rb', line 72

def message
  @output.message
end

#output_json_bodyObject


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/client-api/base.rb', line 53

def output_json_body
  unless @output.body == "" || @output.body.nil? || @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


37
38
39
40
41
# File 'lib/client-api/base.rb', line 37

def patch(url, body, headers = nil)
  @output = patch_request(url, :body => body, :headers => headers)
  self.post_logger if $logger
  self.output_json_body if json_output
end

#post(url, body, headers = nil) ⇒ Object


19
20
21
22
23
# File 'lib/client-api/base.rb', line 19

def post(url, body, headers = nil)
  @output = post_request(url, :body => body, :headers => headers)
  self.post_logger if $logger
  self.output_json_body if json_output
end

#post_loggerObject


76
77
78
79
80
81
82
83
84
85
86
# File 'lib/client-api/base.rb', line 76

def post_logger
  (@output.body == "" || @output.body.nil? || @output.body == "{}") ? 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


31
32
33
34
35
# File 'lib/client-api/base.rb', line 31

def put(url, body, headers = nil)
  @output = put_request(url, :body => body, :headers => headers)
  self.post_logger if $logger
  self.output_json_body if json_output
end

#response_headersObject


67
68
69
70
# File 'lib/client-api/base.rb', line 67

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


43
44
45
# File 'lib/client-api/base.rb', line 43

def status
  @output.code.to_i
end