Class: SnapImage::Config
- Inherits:
-
Object
- Object
- SnapImage::Config
- Defined in:
- lib/snapimage/config.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #get_config ⇒ Object
-
#initialize(config) ⇒ Config
constructor
- Arguments: * config
-
Filename of the YAML or JSOn file to load or a config Hash.
- #set_config_defaults ⇒ Object
- #validate_config ⇒ Object
Constructor Details
#initialize(config) ⇒ Config
Arguments:
- config
-
Filename of the YAML or JSOn file to load or a config Hash.
NOTE: All keys are strings, not symbols.
7 8 9 |
# File 'lib/snapimage/config.rb', line 7 def initialize(config) @raw_config = config end |
Instance Method Details
#[](key) ⇒ Object
42 43 44 |
# File 'lib/snapimage/config.rb', line 42 def [](key) get_config[key] end |
#get_config ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/snapimage/config.rb', line 21 def get_config return @config if @config @config = @raw_config if @raw_config.is_a? String ext = File.extname(@raw_config) case ext when ".yml", ".yaml" @config = YAML::load(File.open(@raw_config)) when ".json" @config = JSON::parse(File.read(@raw_config)) else raise SnapImage::UnknownFileType, "Unknown filetype. Expecting .yaml, .yml, or .json: #{@raw_config}" end end raise SnapImage::UnknownConfigType, "Unknown config type. Expecting a filename or hash: #{@config}" unless @config.is_a? Hash validate_config set_config_defaults @config end |
#set_config_defaults ⇒ Object
17 18 19 |
# File 'lib/snapimage/config.rb', line 17 def set_config_defaults @config["max_file_size"] ||= 10485760 # 10MB end |
#validate_config ⇒ Object
11 12 13 14 15 |
# File 'lib/snapimage/config.rb', line 11 def validate_config raise SnapImage::InvalidConfig, 'Missing "adapter"' unless @config["adapter"] raise SnapImage::InvalidConfig, "Unknown adapter type. Expecting local or cloudinary: #{@config["adapter"]}" unless @config["adapter"] SnapImage.const_get(@config["adapter"].capitalize).const_get("Config").new(@config).validate_config end |