Giter VIP home page Giter VIP logo

trebuchet-1's Introduction

Trebuchet

Trebuchet launches features at people. Wisely choose a strategy, aim, and launch!

Installation

Trebuchet can be used with Rails or standalone.

To use with Rails: gem 'trebuchet', :require => 'trebuchet_rails'

Setup

Trebuchet defaults to storing data in memory, or can be used with Redis or Memcache as a data store:

Trebuchet.set_backend :memcached
Trebuchet.set_backend :redis, :client => Redis.new(:host => 'example.com')
Trebuchet.set_backend :redis_cached, :client => Redis.new(:host => 'example.com')

A Rails initializer is a great spot for this. You may want to use a few other settings, either hardcoded values or procs (eval'd in the context of the controller):

Trebuchet.admin_view = proc { current_user.try(:admin?) } # /trebuchet admin interface access
Trebuchet.time_zone = proc { current_user.time_zone } # or just "Mountain Time (US & Canada)"

Aim

Trebuchet can be aimed while your application is running. The syntax is:

Trebuchet.aim('awesome_feature', :percent, 1)

Which will launch 'awesome_feature' to 1% of users.

Another builtin strategy allows launching to particular user IDs:

Trebuchet.aim('awesome_feature', :users, [23, 42])

You can also combine multiple strategies, in which case the feature is launched if any of them is true:

Trebuchet.feature('awesome_feature').aim(:percent, 1).aim(:users, [23, 42])

If you don't aim Trebuchet for a feature, the default action is not to launch it to anyone.

Launch

In a view, do this:

<% trebuchet.launch('time_machine') do %>
    <p>Welcome to the future!</p>
<% end %>

The code between do .. end will only run if the strategy for 'time_machine' allows launching to current_user.

You can also use it in a controller:

def index
    trebuchet.launch('time_machine') do
        @time_machine = TimeMachine.new
    end
end

Custom Strategies

Trebuchet ships with a number of default strategies but you can also define your own custom strategies like so:

Trebuchet.define_strategy(:admins) do |user|
    !!(user && user.has_role?(:admin))
end

controller.current_user is yielded to the block and it should return true for users you want to launch to. You can use parameters with custom strategies too:

Trebuchet.define_strategy(:markets) do |user, markets|
    markets.include?(user.market)
end

Like parameters for builtin strategies, these can be changed while the application is running. For example:

Trebuchet.aim('time_machine', :markets, ['San Francisco', 'New York City'])

When using Trebuchet together with Rails, a good place to define custom strategies is in an initializer.

Visitor Strategy

Trebuchet can be used to launch to visitors (no user object present). First, set the visitor id either directly (in a before filter) or as a proc:

Trebuchet.visitor_id = 123

Trebuchet.visitor_id = proc { |request| request && request.cookies[:visitor] && request.cookies[:visitor].hash }

If you're using a proc, Trebuchet passes in the request object. It expects that the proc returns an integer. If it returns anything else, Trebuchet will not launch.

trebuchet-1's People

Contributors

ascott avatar clizzin avatar gkoo avatar iamnirav avatar ianlogan avatar jeffyip avatar jiayou50 avatar josephsofaer avatar jtai avatar liukai avatar mcharkov avatar nelgau avatar nwadams avatar salbito avatar schleyfox avatar sonicwang avatar spencerdemars avatar wyattdanger avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.