Sha256: 25d0a627de15fd68d1909a35b840dcdf423557b74a8057829db87a5daffba0e1

Contents?: true

Size: 1.64 KB

Versions: 10

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true
require "uri"

module Skylight
  module Util
    class Component
      attr_accessor :environment, :name

      NAME_FORMAT = /\A[a-zA-Z0-9_-]+\z/
      DEFAULT_NAME = "web"
      WORKER_NAME = "worker"
      DEFAULT_ENVIRONMENT = "production"

      def initialize(environment, name, force_worker: false)
        @environment = environment || DEFAULT_ENVIRONMENT
        @name        = resolve_name(name, force_worker)

        raise ArgumentError, "environment can't be blank" if @environment.empty?
        validate_string!(@environment, "environment")
        validate_string!(@name, "name")
      end

      def to_s
        "#{name}:#{environment}"
      end

      def to_encoded_s
        @to_encoded_s ||= URI.encode_www_form_component(to_s)
      end

      def web?
        name == DEFAULT_NAME
      end

      def worker?
        !web?
      end

      # keys here should match those from the main config
      def as_json(*)
        {
          component: name,
          env: environment
        }
      end

      private

        def program_name
          $PROGRAM_NAME
        end

        def argv
          ARGV
        end

        def resolve_name(given_name, force_worker)
          # don't allow workers to be called 'web'
          return WORKER_NAME if force_worker && (given_name.nil? || given_name == DEFAULT_NAME)
          return DEFAULT_NAME if given_name.nil?

          given_name
        end

        def validate_string!(string, kind)
          return true if string =~ NAME_FORMAT
          raise ArgumentError, "#{kind} can only contain lowercase letters, numbers, and dashes"
        end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
skylight-4.3.2 lib/skylight/util/component.rb
skylight-4.3.1 lib/skylight/util/component.rb
skylight-4.3.0 lib/skylight/util/component.rb
skylight-4.2.3 lib/skylight/util/component.rb
skylight-4.2.2 lib/skylight/util/component.rb
skylight-4.2.1 lib/skylight/util/component.rb
skylight-4.2.0 lib/skylight/util/component.rb
skylight-4.2.0.beta3 lib/skylight/util/component.rb
skylight-4.2.0.beta2 lib/skylight/util/component.rb
skylight-4.2.0.beta lib/skylight/util/component.rb