class NetHTTP::Response

Attributes

body[R]
body_obj[R]
body_os[R]
code[R]
headers[R]
headers_hash[R]
headers_os[R]
logger[R]
response[R]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/response/response.rb, line 21
def initialize(opts = {})
  send('logger=', opts[:logger])
  @response = opts[:response]
  send('response=', opts[:response])
  send('code=')
  send('headers=')
  send('headers_hash=')
  send('headers_os=')
  send('body=')
  send('body_obj=')
  send('body_os=')
  log_response
end

Public Instance Methods

body=() click to toggle source
# File lib/response/response.rb, line 35
def body=
  @body = response.body
end
body_obj=() click to toggle source
# File lib/response/response.rb, line 39
def body_obj=
  @body_obj = {}

  if content_type.to_s.downcase.include?('json')
    @body_obj = parse_json(body, logger)
  elsif content_type.to_s.downcase.include?('xml') || content_type.to_s.downcase.include?('html')
    @body_obj = parse_xml(body, logger)
  end

  @body_obj
end
body_os=() click to toggle source
# File lib/response/response.rb, line 51
def body_os=
  @body_os = JSON.parse(body_obj.to_json, object_class: OpenStruct)
end
code=() click to toggle source
# File lib/response/response.rb, line 55
def code=
  @code = response.code
end
content_type() click to toggle source
# File lib/response/response.rb, line 59
def content_type
  content_type = headers_hash.select { |k,v| k.to_s.downcase == 'content_type' }

  return nil if content_type.empty?

  content_type[:content_type]
end
headers=() click to toggle source
# File lib/response/response.rb, line 67
def headers=
  @headers = {}
  response.to_hash.each do |key, value|
    @headers[key] = value.flatten[0].to_s
  end
end
headers_hash=() click to toggle source
# File lib/response/response.rb, line 74
def headers_hash=
  @headers_hash = NetHTTP::Core::Utilities.convert_hash_keys(
    object: headers,
    format: 'snake',
    type: 'symbol'
  )
end
headers_os=() click to toggle source
# File lib/response/response.rb, line 82
def headers_os=
  @headers_os = JSON.parse(headers_hash.to_json, object_class: OpenStruct)
end
log_response() click to toggle source
# File lib/response/response.rb, line 127
def log_response
  logger.debug('Response Code => ' + code)
  logger.debug('Response Headers => ')
  logger.debug(headers)
  logger.debug('Response Body => ')
  logger.debug(body)
end
logger=(logger = nil) click to toggle source
# File lib/response/response.rb, line 123
def logger=(logger = nil)
  @logger = Core.get_logger(logger)
end
parse_json(body, logger) click to toggle source
# File lib/response/response.rb, line 90
def parse_json(body, logger)
  begin
    NetHTTP::Core::Utilities.json_2_hash(body, 'symbol', logger)
  rescue JSON::ParserError => err
    logger.debug(err)
    return {}
  end
end
parse_xml(body, logger) click to toggle source
# File lib/response/response.rb, line 99
def parse_xml(body, logger)
  begin
    NetHTTP::Core::Utilities.xml_2_hash(body, 'symbol', logger)
  rescue Nokogiri::XML::SyntaxError => err
    logger.debug(err)
    return {}
  end
end
response=(response) click to toggle source
# File lib/response/response.rb, line 86
def response=(response)
  @response = response
end
valid_html?() click to toggle source
# File lib/response/response.rb, line 116
def valid_html?
  NetHTTP::Core::Utilities.valid_html?(body, logger)
end
valid_json?() click to toggle source
# File lib/response/response.rb, line 108
def valid_json?
  NetHTTP::Core::Utilities.valid_json?(body, logger)
end
valid_xml?() click to toggle source
# File lib/response/response.rb, line 112
def valid_xml?
  NetHTTP::Core::Utilities.valid_xml?(body, logger)
end