Sha256: ac7e0f9c4083afe02ebf8e43c4fec6cc71d635aed5b601a1c9e39ffac33a1a23

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

module EZAPIClient
  class GenData
    ALLOWED_CHARACTERS = '[^\w\s-]'

    include Virtus.model
    attribute :username, String
    attribute :password, String
    attribute :prv_path, String
    attribute :eks_path, String
    attribute :reference_no, String
    attribute :message, Hash
    attribute :json, String, lazy: true, default: :default_json
    attribute :command, String, lazy: true, default: :default_command
    attribute :logger, Object
    attribute :log, Boolean

    def self.call(attributes)
      self.new(attributes).()
    end

    def call
      if log
        logger.info(EZAPIClient::LOG_PROGNAME) { command }
        ExecCommand.(command, logger)
      else
        ExecCommand.(command)
      end
    end

    private

    def default_command
      [
        "java -cp",
        JAR_PATH,
        "ezpadala.EZdata",
        prv_path,
        eks_path,
        username,
        password,
        reference_no,
        %Q('#{json}'),
      ].join(" ")
    end

    def default_json
      message.each_with_object({}) do |(key, value), hash|
        hash[key.to_s.camelcase(:lower)] = strip_special_characters(value)
      end.to_json
    end

    def strip_special_characters(value)
      return value if !value.is_a?(String)

      value.gsub(Regexp.new(ALLOWED_CHARACTERS), '')
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ezapi_client-1.2.1 lib/ezapi_client/services/gen_data.rb