Skip to content

Commit

Permalink
Merge pull request #1 from sergioisidoro/e7o-configs
Browse files Browse the repository at this point in the history
Using configs instead of env vars
  • Loading branch information
sergioisidoro authored Apr 24, 2019
2 parents 92a3c45 + 2c2b0a7 commit 7a15e0c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
32 changes: 27 additions & 5 deletions lib/e7o.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
require 'e7o/middleware'
require 'e7o/railtie' if defined?(Rails)
module E7o
class Configuration < OpenStruct
def setup_defaults
self.redis_host = 'redis://localhost:6379'
self.e7o_request_store_key = :e7o_counter
self.enabled = false
end
end

def self.configure
@config = Configuration.new
@config.setup_defaults
yield(@config) if block_given?
@config
end

def self.config
@config || configure
end
end


module I18n
module E7oKeyRegistry
def lookup(locale, key, scope = [], options = {})
separator = options[:separator] || I18n.default_separator
flat_key = I18n.normalize_keys(locale, key, scope, separator).join(separator)

RequestStore.store[:e7o_counter] ||= {}
RequestStore.store[:e7o_counter][flat_key] ||= 0
RequestStore.store[:e7o_counter][flat_key] += 1
RequestStore.store[E7o.config.e7o_request_store_key] ||= {}
RequestStore.store[E7o.config.e7o_request_store_key][flat_key] ||= 0
RequestStore.store[E7o.config.e7o_request_store_key][flat_key] += 1

super
end
end
end

I18n::Backend::Simple.send :include, I18n::E7oKeyRegistry

require 'e7o/middleware'
require 'e7o/railtie' if defined?(Rails)
7 changes: 2 additions & 5 deletions lib/e7o/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module E7o
class Middleware
def initialize(app)
@redis_connection ||= Redis.new url: determine_redis_provider
@redis_connection ||= Redis.new url: E7o.config.redis_host
@app = app
end

Expand All @@ -27,11 +27,8 @@ def call(env)
private

def e7o_enabled
ENV['E7O_ENABLED'] == 'true'
E7o.config.enabled
end

def determine_redis_provider
ENV['E7O_REDIS_URL'] || ENV[ENV['REDIS_PROVIDER'] || 'REDIS_URL']
end
end
end

0 comments on commit 7a15e0c

Please sign in to comment.