Sha256: d3a917a005fe8fa28f4e367a6f4fb5c2cfd4b8865f7ccf606a06da97a77a04fd

Contents?: true

Size: 733 Bytes

Versions: 1

Compression:

Stored size: 733 Bytes

Contents

# frozen_string_literal: true

require 'json'

module Lecter
  class FormatterPayload
    WRONG_PARAMETERS_MSG = 'Wrong parameters'
    attr_reader :result, :error_message

    def initialize(payload)
      @dirty_payload = payload
    end

    def call
      @result = json_parse(dirty_payload).merge(lecter_enabled_parameter)
    rescue JSON::ParserError
      @error_message = WRONG_PARAMETERS_MSG
      false
    end

    private

    attr_accessor :dirty_payload

    def json_parse(string)
      string = '{' + string + '}' unless string.match(/\A{.*}\z/)
      string.gsub!('=>', ':')&.gsub!(/(“|”)/, '"')
      JSON.parse(string)
    end

    def lecter_enabled_parameter
      { lecter_enabled: true }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lecter-0.2.0 lib/lecter/formatter_payload.rb