easy_conf 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6867474022805747ef2c360053e00d7c97f5f66d
4
- data.tar.gz: 7a9321beeba8722dc7c82830233b4284150fe381
3
+ metadata.gz: 78e252ae88731a6e0a71c05b14871714c50ba738
4
+ data.tar.gz: bce63f90b3310f312153995eb0441b17b009d21d
5
5
  SHA512:
6
- metadata.gz: f6c4e451cc84947a46764a841c50c841b1209650c0c159cb493e38bbfc5971b44560b32b9d14555ce91acf66d94d3cd04e687ca6adca85f960c2edbc712c639f
7
- data.tar.gz: 9c8131e7cb77f41d0cc55ab78b6bfd01f82975dd34073de91e561ab4ff55b64aefa6b454b9f0b96f5d79eb5c74563af54ae2e9b20dabf261d760a54d4ee1d3ba
6
+ metadata.gz: a53045c54df06d04bc67731d58b959f2ec6d6b062a1622d1734f02d568421543d0644de8a160ad20f4bd9c618a76875dc25f64be987d8e99e704e48cbd8cd98b
7
+ data.tar.gz: 2466eeae91287e081ec582eba490a5767f9e7e6d879f4eec65ea3d660ff655bbf88e21fc95388ab74c38d4a1afbb6ed14dc0af65d882227efe76b4a583ef61fa
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ *.gem
data/README.md CHANGED
@@ -1,8 +1,12 @@
1
1
  # EasyConf
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/easy_conf`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ EasyConf, as its name suggest, is a gem for managing application configuration easily. It works really simple; tries to get configuration key from config files you've specified and uses environment variables as fallback. So if configuration key is not found on config files it will look at the environment variables.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ You may wonder why I've created this gem. The answer is simple; because convention matters!
6
+
7
+ I saw lots of applications written buy other awesome developers and I noticed that they are managing their configurations either by using config files or environment variables or even both! As a result of this, in this applications you can see something like `ENV['foo']` and `Rails.application.config` mixed all around the source of the application. What a mess.
8
+
9
+ Beside of this complexity your production environment may have some restrictions you don't have on your own development environment. For example, if you are using Heroku you can't use config files to configure your application. Because you have to untrack config files on your VCS unless you have a good reason to track them. By using EasyConf while you can configure your application with config files also you can use environment variables on your production environment and this will give you a good abstraction on configuration!
6
10
 
7
11
  ## Installation
8
12
 
@@ -22,20 +26,128 @@ Or install it yourself as:
22
26
 
23
27
  ## Usage
24
28
 
25
- TODO: Write usage instructions here
29
+ #### EasyConf configuration
30
+
31
+ EasyConf requires its own configuration to determine where is configuration files. For Rails applications, create a file under config directory which looks like;
32
+
33
+ ```ruby
34
+ require 'easy_conf'
35
+
36
+ EasyConf.configure do |config|
37
+ config.config_files = ["#{Rails.root}/config/config.yml"]
38
+ config.inform_with = :log
39
+ config.white_list_keys = [:foo]
40
+ config.environment = :production
41
+ end
42
+ ```
43
+
44
+ For other ruby applications, place this file wherever you want and load it at the top of the application.
45
+
46
+ #### Accessing configuration keys
47
+
48
+ After configuration you can use it inside of your code like this;
49
+
50
+ ```ruby
51
+ EasyConf.get(:redis_url) # tries to load redis_url from yml or environment variable
52
+ EasyConf.read(:redis_url) # alias of get method
53
+ EasyConf.app_config.redis_url # another alias for accessing configuration keys
54
+ ```
55
+
56
+ You may want to define a global variable or a constant on top level of your application like `easy_conf.rb` configuration file to get keys as method calls.
57
+
58
+ By constants;
59
+
60
+ ```ruby
61
+ CONFIG = EasyConf.app_config # define this constant on top level of your application
62
+ ```
63
+
64
+ By global variables;
65
+
66
+ ```ruby
67
+ $config = EasyConf.app_config
68
+ ```
69
+
70
+ You can get all configuration keys as an array like;
71
+
72
+ ```ruby
73
+ EasyConf.keys # [:foo, ...]
74
+ ```
75
+ Or if you want to get your configurations from config files as Hash, you can just do;
76
+
77
+ ```ruby
78
+ EasyConf.to_hash # { "foo" => "bar" }
79
+ ```
26
80
 
27
- ## Development
81
+ ## Configuration Keys
28
82
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
83
+ **config_files:** *Required! List of configuration files to be used.
84
+ If you don't configure this, EasyConf can read configurations from only environment variables.
30
85
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
86
+ **inform_with:** Optional. Information method used in case of error. Whether sliently puts log or raises exception. Valid options are `:log` and `:exception`. Default is `:log`.
87
+
88
+ **white_list_keys:** Optional. List of config keys to be used. If this configuration is set, config keys which are not in this list won't be accessible through EasyConf!
89
+
90
+ **black_list_keys:** Optional. List of config keys to be ignored. If this configuration is set, config keys which are in this list won't be accessible through EasyConf! *Note that, black list keys overrides white list keys.*
91
+
92
+ **environment:** Optional. Specifies environment scope. For Rails applications, the value is same with Rails.
93
+
94
+ ## TIPS
95
+
96
+ #### Configuring Rails Initializers With EasyConf
97
+
98
+ If you want to use EasyConf to configure your Rails initializers, put EasyConf configuration file to root of your Rails project and require EasyConf file in your `config/application.rb` file. But keep that in your mind, if you require EasyConf on top of your initializer you can't use `Rails.root` while configuring EasyConf anymore. In this case you have to give absolute paths by yourself.
99
+
100
+ ```ruby
101
+ ...
102
+ Bundler.require(*Rails.groups)
103
+
104
+ require File.expand_path('../easy_conf', __FILE__)
105
+
106
+ module YourApp
107
+ class Application < Rails::Application
108
+ ...
109
+ ```
110
+
111
+ Then configure EasyConf like;
112
+
113
+
114
+ ```ruby
115
+ require 'easy_conf'
116
+
117
+ EasyConf.configure do |config|
118
+ config.config_files = ["/Users/.../config/config.yml"]
119
+ end
120
+ ```
121
+
122
+ #### Deploy With AWS Elastic Beanstalk
123
+
124
+ AWS Elastic Beanstalk is a really good service to deploy applications into AWS. With Elastic Beanstalk you can use a command line interface to deploy your application in minutes. But because lack of information you may struggle to configure your application. The easiest way to configure your application is using environment variables. To set environment variables you can use either AWS Console or AWS CLI. However if you have lots of configurations AWS has a bad news for you! There is a limitation on environment variables. You can use only *4096* characters to set environment variables for your application. If you reach the limitations you may want to use configuration files to configure your application. But remember that we have to untrack our configuration files. The solution is simple; use `ebextensions` to place your config files into your application directory during deployment. Here is a sample extension which reads configuration file from private S3 bucket and writes it into the correct location.
125
+
126
+ ```
127
+ files:
128
+ "/opt/elasticbeanstalk/hooks/appdeploy/pre/01a_get_config_yml.sh":
129
+ mode: "000777"
130
+ content: |
131
+ . /opt/elasticbeanstalk/support/envvars
132
+
133
+ cd /var/app/ondeck/config
134
+ aws s3 cp $remote_config_file config.yml
135
+ ```
136
+
137
+ This little shell script reads the location of config file from environment file, downloads it and writes it into the place where it should be. You have to adjust access policies for your S3 and Elastic Beanstalk user otherwise it can't download the configuration file.
138
+
139
+ If you think storing configuration files at S3 is not good then you can use some tools like Apache ZooKeeper. For now EasyConf doesn't support it but you can find it in TODO list.
140
+
141
+ ## TODO
142
+
143
+ 1. Write tests
144
+ 2. Write adapter for ZooKeeper
32
145
 
33
146
  ## Contributing
34
147
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/easy_conf.
148
+ Bug reports and pull requests are welcome on GitHub at https://github.com/meinac/easy_conf.
36
149
 
37
150
 
38
151
  ## License
39
152
 
40
153
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
@@ -0,0 +1,84 @@
1
+ module EasyConf
2
+ class AppConfig # :nodoc
3
+
4
+ MESSAGES = {
5
+ unpermitted_key: 'Found unpermitted key'.freeze,
6
+ file_not_found: 'Configuration file not found'.freeze,
7
+ config_not_found: 'Configuration not found'.freeze,
8
+ }
9
+
10
+ EXCEPTIONS = {
11
+ unpermitted_key: 'UnpermittedConfigError'.freeze,
12
+ file_not_found: 'ConfigFileNotFoundError'.freeze,
13
+ config_not_found: 'ConfigNotFoundError'.freeze,
14
+ }
15
+
16
+ def initialize
17
+ @table = __read_config
18
+ end
19
+
20
+ def method_missing(meth, *args, &block)
21
+ return __notify(:unpermitted_key, meth) if __list_check(meth)
22
+
23
+ __read_from_table(meth) || __read_from_env(meth) || __notify(:config_not_found, meth)
24
+ end
25
+
26
+ def __keys
27
+ @table.keys
28
+ end
29
+
30
+ def __to_hash
31
+ @table.dup
32
+ end
33
+
34
+ private
35
+ def __configuration
36
+ EasyConf.configuration
37
+ end
38
+
39
+ def __read_config
40
+ env = __configuration.environment.to_s
41
+
42
+ __configuration.config_files.inject({}) do |conf, config_file|
43
+ tmp = if File.exists?(config_file)
44
+ yaml_data = YAML.load_file(config_file)
45
+ env.blank? ? yaml_data : yaml_data.fetch(env, {})
46
+ else
47
+ __notify(:file_not_found, config_file) && {}
48
+ end
49
+ conf.merge(tmp)
50
+ end
51
+ end
52
+
53
+ def __read_from_table(key)
54
+ if val = @table[key.to_s]
55
+ define_singleton_method(key) { val }
56
+ send(key)
57
+ end
58
+ end
59
+
60
+ def __read_from_env(key)
61
+ if val = ENV[key.to_s]
62
+ define_singleton_method(key) { val }
63
+ send(key)
64
+ end
65
+ end
66
+
67
+ def __list_check(key)
68
+ (__configuration.black_list_keys.length > 0 && __configuration.black_list_keys.include?(key)) ||
69
+ (__configuration.black_list_keys.length == 0 &&
70
+ (__configuration.white_list_keys.length > 0 && !__configuration.white_list_keys.include?(key)))
71
+ end
72
+
73
+ def __notify(event, obj)
74
+ message = "#{MESSAGES[event]}: `#{obj}`"
75
+
76
+ if __configuration.inform_with == :exception
77
+ exception_class = "EasyConf::#{EXCEPTIONS[event]}"
78
+ raise exception_class.constantize.new(message)
79
+ else
80
+ puts message
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,24 @@
1
+ module EasyConf
2
+ class Configuration # :nodoc
3
+ attr_accessor :config_files, :inform_with, :white_list_keys, :black_list_keys, :environment
4
+
5
+ def initialize
6
+ @config_files = []
7
+ @inform_with = :log
8
+ @white_list_keys = []
9
+ @black_list_keys = []
10
+ end
11
+
12
+ def white_list_keys=(keys)
13
+ @white_list_keys = keys.map(&:to_sym)
14
+ end
15
+
16
+ def black_list_keys=(keys)
17
+ @black_list_keys = keys.map(&:to_sym)
18
+ end
19
+
20
+ def environment
21
+ @environment ||= defined?(Rails) ? Rails.env : nil
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,9 @@
1
+ module EasyConf
2
+ class ConfigFileNotFoundError < IOError
3
+
4
+ def backtrace
5
+ []
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module EasyConf
2
+ class ConfigNotFoundError < IOError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module EasyConf
2
+ class UnpermittedConfigError < ArgumentError
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module EasyConf
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/easy_conf.rb CHANGED
@@ -1,5 +1,88 @@
1
1
  require "easy_conf/version"
2
+ require "easy_conf/app_config"
3
+ require "easy_conf/configuration"
4
+ require "easy_conf/errors/config_not_found_error"
5
+ require "easy_conf/errors/unpermitted_config_error"
6
+ require "easy_conf/errors/config_file_not_found_error"
2
7
 
3
8
  module EasyConf
4
-
9
+
10
+ class << self
11
+ # Returns all configurations of your applications.
12
+ #
13
+ # Example:
14
+ #
15
+ # EasyConf.app_config # => <EasyConf::AppConfig>
16
+ #
17
+ # You can access configurations of your application by
18
+ # method call with configuration name.
19
+ #
20
+ # Example:
21
+ #
22
+ # EasyConf.app_config.access_key # => "some_key"
23
+ def app_config
24
+ @config ||= AppConfig.new
25
+ end
26
+
27
+ # Read Application configuration by sending key as argument.
28
+ #
29
+ # Example:
30
+ #
31
+ # EasyConf.read(:access_key) # => "some_key"
32
+ def read(key)
33
+ app_config.send(key.to_s)
34
+ end
35
+ alias_method :get, :read
36
+
37
+ # Read Application config keys
38
+ #
39
+ # Example:
40
+ #
41
+ # EasyConf.keys # => [:foo, :bar, ...]
42
+ def keys
43
+ app_config.__keys
44
+ end
45
+
46
+ # Get application configs as Hash
47
+ #
48
+ # Example:
49
+ #
50
+ # EasyConf.to_hash # => { foo: 'bar', ... }
51
+ def to_hash
52
+ app_config.__to_hash
53
+ end
54
+
55
+ # Configures the EasyConf gem for the following keys;
56
+ #
57
+ # config_files : Required! List of configuration files to be used.
58
+ # inform_with : Optional. Information method used in case of error.
59
+ # Whether sliently puts log or raises exception.
60
+ # white_list_keys : Optional. List of config keys to be used.
61
+ # If this configuration is set, config keys
62
+ # which are not in this list won't be
63
+ # accessible through EasyConf!
64
+ # black_list_keys : Optional! List of config keys to be ignored.
65
+ # If this configuration is set, config keys
66
+ # which are in this list won't be accessible
67
+ # through EasyConf!
68
+ # environment : Optional. Specifies environment scope. For Rails
69
+ # applications, the value is same with Rails.
70
+ #
71
+ # Example:
72
+ #
73
+ # EasyConf.configure do |config|
74
+ # config.config_files = [config.yml]
75
+ # config.white_list_keys = ['access_key', 'secret_key', ...]
76
+ # config.black_list_keys = ['black_key', ...]
77
+ # end
78
+ def configure
79
+ yield(configuration)
80
+ app_config # initialize config
81
+ end
82
+
83
+ def configuration # :nodoc
84
+ @configuration ||= Configuration.new
85
+ end
86
+ end
87
+
5
88
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_conf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mehmet Emin İNAÇ
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-08 00:00:00.000000000 Z
11
+ date: 2016-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,6 +72,11 @@ files:
72
72
  - bin/setup
73
73
  - easy_conf.gemspec
74
74
  - lib/easy_conf.rb
75
+ - lib/easy_conf/app_config.rb
76
+ - lib/easy_conf/configuration.rb
77
+ - lib/easy_conf/errors/config_file_not_found_error.rb
78
+ - lib/easy_conf/errors/config_not_found_error.rb
79
+ - lib/easy_conf/errors/unpermitted_config_error.rb
75
80
  - lib/easy_conf/version.rb
76
81
  homepage: http://github.com/meinac/easy_conf
77
82
  licenses: