Sha256: c78e7905fafcca1c4ce53ccf02a867a5ac2710c2a3bebd69694d4124825762d5
Contents?: true
Size: 1.71 KB
Versions: 1
Compression:
Stored size: 1.71 KB
Contents
# frozen_string_literal: true require_relative 'array/wrap' require_relative 'array/to_csv' require_relative 'string/string_colorize' module SimpleHelper class ResponseParser attr_reader :body, :format, :path def initialize(body, format, path) @body = body @format = format @path = path end def self.perform(body, format, path) new(body, format, path).parse end def parse if body.nil? || body == 'null' || (body.valid_encoding? && body.strip.empty?) return log(message: 'Invalid response') end @body = body.gsub(/\A#{UTF8_BOM}/, '') if body.valid_encoding? && body.encoding == Encoding::UTF_8 send(format) end protected # TODO: Support other formats like xml UTF8_BOM = "\xEF\xBB\xBF" def json JSON.parse(body, quirks_mode: true, allow_nan: true) rescue JSON::ParserError log(message: "Response cannot be parsed because it's not a string nor valid JSON. please use .plain to get the the plain response") end def plain body end def csv data = path.nil? ? json : json.dig(*path.split(',')) raise ArgumentError, 'Cannot export nil or empty hash please pass proper path'.bold.brown.gray if data.nil? Array.wrap(data).to_csv('file_name.csv') end def log(message: nil) { "status": 400, "error": 'Bad Request', "message": message } end # def xml # MultiXml.parse(body) # end # def parse_supported_format # send(format) # rescue NoMethodError => e # raise NotImplementedError, "#{self.class.name} has not implemented a parsing method for the #{format.inspect} format.", e.backtrace # end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
simple_request-0.1.5 | lib/simple_helper/response_parser.rb |