Sha256: 52b6fda9c23c62a86476512c70223484f3b4307cd60d45f9e6109f20e3cc5ad7

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

module Agcod
  class Configuration
    REQUIRED_OPTIONS = ["access_key", 
      "secret_key",
      "partner_id",
      "uri",
      "discount_percentage"
    ]    
    
    class << self
      attr_reader :options
      attr_accessor :logger

      def load(app_root = nil, env = nil)
        if app_root
          @app_root = app_root
        else
          @app_root = Rails.root if defined?(Rails)
        end
        
        if @app_root.nil? || 
          !FileTest.exists?(config_filename = File.join(@app_root, 'config', 'agcod.yml'))
          
          raise Error::ConfigurationError, "Configuration for AGCOD not found" 
        end

        config_file = File.read(config_filename)

        environment = Rails.env if defined?(Rails)
        environment = env if env

        @options = YAML.load(config_file)[environment]
        validate_options
        @options
      end

      def set(opt = {})
        @options ||= {}
        @options.merge!(opt)

        validate_options
        @options
      end
     
      def access_key
        @options["access_key"]
      end

      def secret_key
        @options["secret_key"]
      end

      def partner_id
        @options["partner_id"]
      end

      def uri
        @options["uri"]
      end

      def discount_percentage
        @options["discount_percentage"]
      end

      private
      
      def validate_options
        REQUIRED_OPTIONS.each do |opt|
          if options[opt].nil? || options[opt] == ""
            raise Error::ConfigurationError, "#{opt} was not specified" 
          end
        end    
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
agcod-0.0.4 lib/agcod/configuration.rb