class ClientApi::Api

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

body() click to toggle source
# 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
Also aliased as: resp
code()
Alias for: status
delete(url, headers = nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
message() click to toggle source
# File lib/client-api/base.rb, line 102
def message
  @output.message
end
output_json_body() click to toggle source
# 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) click to toggle source
# 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_header() click to toggle source
# 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) click to toggle source
# 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_logger() click to toggle source
# 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) click to toggle source
# 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
resp()
Alias for: body
response_headers() click to toggle source
# 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
status() click to toggle source
# File lib/client-api/base.rb, line 64
def status
  @output.code.to_i
end
Also aliased as: code