Sha256: bf78026fd8483db0929b2e1b3ec115797526c79ce60c71b26760f4a9096d2df2

Contents?: true

Size: 1.69 KB

Versions: 3

Compression:

Stored size: 1.69 KB

Contents

module MarketplaceKit
  module Services
    class Config
      def endpoint
        @endpoint
      end

      def load(endpoint_name)
        @config = {}

        load_config_from '.builder'
        load_config_from '.builder-autogenerated', required: false

        @endpoint = endpoint_name || default_endpoint
        raise Errors::MarketplaceError.new 'Error: Invalid env passed!' if @config[@endpoint].nil?
      rescue Errno::ENOENT
        raise Errors::MarketplaceError.new 'Please create .builder file in order to continue.'
      end

      def token
        @config[@endpoint]['token'].to_s
      end

      def set_token(value)
        @config[@endpoint]['token'] = value
        save_token(value)
      end

      def url
        @config[@endpoint]['url'].to_s
      end

      def default_endpoint
        @config.select { |_k, config| config['default']}.keys.first || 'localhost'.freeze
      end

      private

      def load_config_from(file_name, options = {})
        return if options[:required] == false && !File.exist?("#{MarketplaceKit.builder_folder}.builder-autogenerated")

        text_config = File.read("#{MarketplaceKit.builder_folder}#{file_name}")
        @config.deeper_merge! JSON.parse(text_config)
      end

      def save_token(value)
        config = autogenerated_config.merge({ @endpoint => { token: value } })
        File.write("#{MarketplaceKit.builder_folder}.builder-autogenerated", config.to_json)
      end

      def autogenerated_config
        return {} unless File.exist?("#{MarketplaceKit.builder_folder}.builder-autogenerated")

        raw_config = File.read("#{MarketplaceKit.builder_folder}.builder-autogenerated")
        JSON.parse raw_config
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
marketplace-kit-0.2.4 lib/marketplace_kit/services/config.rb
marketplace-kit-0.2.3 lib/marketplace_kit/services/config.rb
marketplace-kit-0.2.2 lib/marketplace_kit/services/config.rb