Skip to content

Framework for controlling application behavior through configurable properties

License

Notifications You must be signed in to change notification settings

scott-steadman/registry

 
 

Repository files navigation

=== Registry Engine

  This engine provides a registry mechanism which can be used to enable/disable
  functionality or change parameters at runtime.


=== Installation

  # Gemfile
  gem 'registry', :git => 'https://github.com/ss/registry.git'

== Testing/Coverage

  # setup test environment
  rbenv install 1.9.3-p551
  gem install bundler -v '~>1.0.0'
  bundle install --clean --path=vendor/bundle
  bundle exec rake db:migrate

  # run tests
  bundle exec rake test

  # generate coverage report
  bundle exec rake registry:coverage
  # point your browser at http://localhost:3000/coverage/

=== Usage

  Configure the plugin

  in config/initializers/registry.rb

    Registry.configure do |config|

      # This permission check is used to access the registry UI
      config.permission_check { redirect_to login_path and return false unless current_user.admin? }

      # The layout used by the registry UI
      config.layout = 'admin'

      # user id to blame when updating entries
      config.user_id = { current_user.id }
    end


=== Example usage

  in app/controllers/api_controller.rb

    class ApiController < ActionController::Base

      before_filter :ensure_api_enabled, :check_rate_limit

      ...

    private

      def ensure_api_enabled
        raise ServiceUnavailableError.new('API Disabled') unless Registry.api.enabled?
      end

      def check_rate_limit
        cache_key = "api_rate_limit_p#{current_user.id}"

        requests = Rails.cache.read(cache_key).to_i
        if requests >= Registry.api.request_limit
          # Over the limit.
          raise ForbiddenError.new('Rate limit exceeded.')
        else
          # Under the limit.
          Rails.cache.write(cache_key, (requests+1).to_s, :expires_in => Registry.api.request_window)
        end
      end

    end # class ApiController


  ---

  To initialize your registry, create a file of default values and add a rake task.


  in config/registry.yml

    defaults:
      api_enabled:        true
      api_request_limit:  1
      api_request_window: 1000

    development:
      api_request_window: 100

    test:
      api_request_window: 100

    production:
      api_enabled:        false
      api_request_limit:  10
      api_request_window: 10000


  in lib/tasks/my_tasks.rake

    desc 'Import configuration'
    task :import_configuration => [:environment] do
      Registry.import("#{Rails.root}/config/registry.yml", :verbose => true)
    end

  ---

  You can update the registry via console.

  Registry.api.enabled = false

About

Framework for controlling application behavior through configurable properties

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • CSS 80.1%
  • Ruby 12.5%
  • JavaScript 5.1%
  • HTML 2.3%