Sha256: 1d60a8c65c452ec4a94ccb832407f15158e1612c245d197677f06dd9c87386f5

Contents?: true

Size: 1.73 KB

Versions: 7

Compression:

Stored size: 1.73 KB

Contents

# typed: false
# frozen_string_literal: true

require 'csv'
require 'singleton'

using Workato::Extension::HashWithIndifferentAccess

module Workato
  module Connector
    module Sdk
      class LookupTables
        include Singleton

        def self.from_yaml(path = DEFAULT_LOOKUP_TABLES_PATH)
          instance.load_data(YAML.load_file(path))
        end

        def self.from_csv(table_id, table_name, path)
          rows = CSV.foreach(path, headers: true, return_headers: false).map(&:to_h)
          instance.load_data(table_name => { id: table_id, rows: rows })
        end

        class << self
          delegate :load_data,
                   :lookup,
                   to: :instance
        end

        def lookup(table_name_or_id, *args)
          table = find_table(table_name_or_id)
          return {} unless table

          condition = args.extract_options!
          row = table.lazy.where(condition).first
          return {} unless row

          row.to_hash.with_indifferent_access
        end

        def load_data(data = {})
          @table_by_id ||= {}
          @table_by_name ||= {}
          data.each do |name, table|
            table = HashWithIndifferentAccess.wrap(table)
            rows = table['rows'].freeze
            @table_by_id[table['id'].to_i] = rows
            @table_by_name[name] = rows
          end
        end

        private

        def find_table(table_name_or_id)
          unless @table_by_id
            raise 'Lookup Tables are not initialized. ' \
                  'Init data by calling LookupTable.from_file or LookupTable.load_data'
          end

          (table_name_or_id.is_int? && @table_by_id[table_name_or_id.to_i]) || @table_by_name[table_name_or_id]
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
workato-connector-sdk-1.3.5 lib/workato/connector/sdk/lookup_tables.rb
workato-connector-sdk-1.3.4 lib/workato/connector/sdk/lookup_tables.rb
workato-connector-sdk-1.3.3 lib/workato/connector/sdk/lookup_tables.rb
workato-connector-sdk-1.3.2 lib/workato/connector/sdk/lookup_tables.rb
workato-connector-sdk-1.3.1 lib/workato/connector/sdk/lookup_tables.rb
workato-connector-sdk-1.3.0 lib/workato/connector/sdk/lookup_tables.rb
workato-connector-sdk-1.2.0 lib/workato/connector/sdk/lookup_tables.rb