Sha256: a9bbd70b11a2762305208f6a4ea3213613debac6a24bcde3e37cf174cda2e3de

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require 'pry'

module SimpleHelper
  class ResponseParser
    attr_reader :body, :format

    def initialize(body, format)
      @body = body
      @format = format
    end

    def self.perform(body, format)
      new(body, format).parse
    end

    def parse
      return nil if body.nil?
      return nil if body == 'null'
      return nil if body.valid_encoding? && body.strip.empty?

      @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, csv

    UTF8_BOM = "\xEF\xBB\xBF"

    def json
      JSON.parse(body, quirks_mode: true, allow_nan: true)
    rescue JSON::ParserError
      puts "Response cannot be parsed because it's not a string not valid JSON. please use .plain to get the the plain response"
    end

    def plain
      body
    end

    # def csv
    #   CSV.parse(body)
    # 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.1 lib/simple_helper/response_parser.rb