Sha256: ed2e82152ab3072a8a1cfacced78a7d8a85072e5dd05b5745156b514d033b7aa

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

module HomePage
  class ApiProviderHost
    ENVIRONMENTS = [:development, :test, :staging, :production]
    ALIASES = { 
      dev: :development, testing: :test, stage: :staging, show: :staging, 
      live: :production, prod: :production 
    }
    FALLBACKS = { 
      development: [:development, :test, :staging, :production], 
      test: [:test, :development, :staging, :production],
      staging: [:staging, :production],
      production: [:production]
    }
    
    def initialize(provider, working_environment)
      @provider = provider
      @environment = working_environment.to_s.to_sym
    end
    
    def setting_namespace
      "home_page.apis.providers.#{@provider}.hosts"
    end
    
    def environment
      if ENVIRONMENTS.include?(@environment)
        @environment
      else
        ALIASES[@environment] || raise(
          NotImplementedError, 
          'Your environment is unknown. Please update alias mapping!'
        )
      end
    end
    
    def to_s
      host = nil
      
      FALLBACKS[environment].each do |provider_environment|
        host = Setting["#{setting_namespace}.#{provider_environment}"]
        
        break if host
      end
      
      unless host
        raise(
          NotImplementedError, 
          'The API provider does not support your environment!'
        )
      end
      
      host
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
home_page-0.0.6 lib/home_page/api_provider_host.rb