Git Product home page Git Product logo

ruby-sdk-1's Introduction

Optimizely Ruby SDK

Build Status Coverage Status Apache 2.0

This repository houses the Ruby SDK for use with Optimizely Full Stack and Optimizely Rollouts.

Optimizely Full Stack is A/B testing and feature flag management for product development teams. Experiment in any application. Make every feature on your roadmap an opportunity to learn. Learn more at https://www.optimizely.com/platform/full-stack/, or see the documentation.

Optimizely Rollouts is free feature flags for development teams. Easily roll out and roll back features in any application without code deploys. Mitigate risk for every feature on your roadmap. Learn more at https://www.optimizely.com/rollouts/, or see the documentation.

Getting Started

Installing the SDK

The SDK is available through RubyGems. To install:

gem install optimizely-sdk

Feature Management Access

To access the Feature Management configuration in the Optimizely dashboard, please contact your Optimizely account executive.

Using the SDK

You can initialize the Optimizely instance in two ways: directly with a datafile, or by using a factory class, OptimizelyFactory, which provides methods to create an Optimizely instance with the default configuration.

Initialization with datafile

Initialize Optimizely with a datafile. This datafile will be used as ProjectConfig throughout the life of the Optimizely instance.

optimizely_instance = Optimizely::Project.new(datafile)

Initialization by OptimizelyFactory

  1. Initialize Optimizely by providing an sdk_key and an optional datafile. This will initialize an HTTPConfigManager that makes an HTTP GET request to the URL (formed using your provided sdk_key and the default datafile CDN url template) to asynchronously download the project datafile at regular intervals and update ProjectConfig when a new datafile is received.

    optimizely_instance = Optimizely::OptimizelyFactory.default_instance('put_your_sdk_key_here', datafile)
    

When the datafile is given then it will be used initially before any update.

  1. Initialize Optimizely by providing a Config Manager that implements a config method. You can customize our HTTPConfigManager as needed.

    custom_config_manager = CustomConfigManager.new
    optimizely_instance = Optimizely::OptimizelyFactory.default_instance_with_config_manager(custom_config_manager)
    
  2. Initialize Optimizely with required sdk_key and other optional arguments.

     optimizely_instance = Optimizely::OptimizelyFactory.custom_instance(
        sdk_key,
        datafile,
        event_dispatcher,
        logger,
        error_handler,
        skip_json_validation,
        user_profile_service,
        config_manager,
        notification_center,
        event_processor
    )
    

HTTP Config Manager

The HTTPConfigManager asynchronously polls for datafiles from a specified URL at regular intervals by making HTTP requests.

 http_project_config_manager = Optimizely::HTTPProjectConfigManager.new(
        sdk_key: nil,
        url: nil,
        datafile: nil,
        url_template: nil,
        auto_update: nil,
        polling_interval: nil,
        start_by_default: nil,
        blocking_timeout: nil,
        logger: nil,
        error_handler: nil,
        skip_json_validation: false,
        notification_center: notification_center,
        datafile_access_token: nil,
        proxy_config: nil
      )

Note: You must provide either the sdk_key or URL. If you provide both, the URL takes precedence.

sdk_key The sdk_key is used to compose the outbound HTTP request to the default datafile location on the Optimizely CDN.

datafile You can provide an initial datafile to bootstrap the DataFileProjectConfig so that it can be used immediately. The initial datafile also serves as a fallback datafile if HTTP connection cannot be established. The initial datafile will be discarded after the first successful datafile poll.

polling_interval The polling interval is used to specify a fixed delay between consecutive HTTP requests for the datafile. Valid duration is greater than 0 and less than 2592000 seconds. Default is 5 minutes.

url_template A string with placeholder {sdk_key} can be provided so that this template along with the provided sdk_key is used to form the target URL.

start_by_default Boolean flag used to start the AsyncScheduler for datafile polling if set to True.

blocking_timeout The blocking timeout period is used to specify a maximum time to wait for initial bootstrapping. Valid blocking timeout period is between 1 and 2592000 seconds. Default is 15 seconds.

datafile_access_token An access token sent in an authorization header with the request to fetch private datafiles.

You may also provide your own logger, error handler, or notification center.

Advanced configuration

The following properties can be set to override the default configurations for HTTPConfigManager.

PropertyName Default Value Description
update_interval 5 minutes Fixed delay between fetches for the datafile
sdk_key nil Optimizely project SDK key
url nil URL override location used to specify custom HTTP source for the Optimizely datafile
url_template 'https://cdn.optimizely.com/datafiles/{sdk_key}.json' Parameterized datafile URL by SDK key
datafile nil Initial datafile, typically sourced from a local cached source
auto_update true Boolean flag to specify if callback needs to execute infinitely or only once
start_by_default true Boolean flag to specify if datafile polling should start right away as soon as HTTPConfigManager initializes
blocking_timeout 15 seconds Maximum time in seconds to block the config call until config has been initialized

A notification signal will be triggered whenever a new datafile is fetched and Project Config is updated. To subscribe to these notifications, use the notification_center.add_notification_listener(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:OPTIMIZELY_CONFIG_UPDATE], @callback)

BatchEventProcessor

BatchEventProcessor is a batched implementation of the EventProcessor

  • Events passed to the BatchEventProcessor are immediately added to a Queue.

  • The BatchEventProcessor maintains a single consumer thread that pulls events off of the Queue and buffers them for either a configured batch size or for a maximum duration before the resulting LogEvent is sent to the NotificationCenter.

Use BatchEventProcessor
event_processor = Optimizely::BatchEventProcessor.new(
    event_queue: SizedQueue.new(10),
    event_dispatcher: event_dispatcher,
    batch_size: 10,
    flush_interval: 30000,
    logger: logger,
    notification_center: notification_center
)

Advanced configuration

The following properties can be used to customize the BatchEventProcessor configuration.

Property Name Default Value Description
event_queue 1000 SizedQueue.new(100) or Queue.new. Queues individual events to be batched and dispatched by the executor. Default value is 1000.
event_dispatcher nil Used to dispatch event payload to Optimizely. By default EventDispatcher.new will be set.
batch_size 10 The maximum number of events to batch before dispatching. Once this number is reached, all queued events are flushed and sent to Optimizely.
flush_interval 30000 ms Maximum time to wait before batching and dispatching events. In milliseconds.
notification_center nil Notification center instance to be used to trigger any notifications.

Close Optimizely

If you enable event batching, make sure that you call the close method, optimizely.close(), prior to exiting. This ensures that queued events are flushed as soon as possible to avoid any data loss.

Note: Because the Optimizely client maintains a buffer of queued events, we recommend that you call close() on the Optimizely instance before shutting down your application or whenever dereferencing the instance.

Method Description
close() Stops all timers and flushes the event queue. This method will also stop any timers that are happening for the datafile manager.

See the Optimizely Full Stack developer documentation to learn how to set up your first Full Stack project and use the SDK.

Development

Building the SDK

To build a local copy of the gem which will be output to /pkg:

rake build

Unit tests

Running all tests

You can run all unit tests with:

rake spec

Contributing

Please see CONTRIBUTING.

Credits

This software incorporates code from the following open source projects:

Httparty https://github.com/jnunemaker/httparty
Copyright © 2008 John Nunemaker
License (MIT): https://github.com/jnunemaker/httparty/blob/master/MIT-LICENSE

JSON Schema Validator https://github.com/ruby-json-schema/json-schema
Copyright © 2010-2011, Lookingglass Cyber Solutions
License (MIT): https://github.com/ruby-json-schema/json-schema/blob/master/LICENSE.md

Murmurhash3 https://github.com/funny-falcon/murmurhash3-ruby
Copyright © 2012 Sokolov Yura 'funny-falcon'
License (MIT): https://github.com/funny-falcon/murmurhash3-ruby/blob/master/LICENSE

Additional Code

This software may be used with additional code that is separately downloaded by you. These components are subject to their own license terms, which you should review carefully.

Bundler https://github.com/bundler/bundler
Copyright © 2008-2018 Andre Arko, Engine Yard, et al
License (MIT): https://github.com/bundler/bundler/blob/master/LICENSE.md

Coveralls https://github.com/lemurheavy/coveralls-ruby
Copyright © 2012 Wil Gieseler
License (MIT): https://github.com/lemurheavy/coveralls-ruby/blob/master/LICENSE

Rake https://github.com/ruby/rake
Copyright © 2004-2017 Jim Weirich
License (MIT): https://github.com/ruby/rake/blob/master/MIT-LICENSE

RSpec https://github.com/rspec/rspec
Copyright © 2009 Chad Humphries, David Chelimsky
Copyright © 2006 David Chelimsky, The RSpec Development Team
Copyright © 2005 Steven Baker
License (MIT): https://github.com/rspec/rspec/blob/master/LICENSE.md

RuboCop https://github.com/rubocop-hq/rubocop
Copyright © 2012-19 Bozhidar Batsov
License (MIT): https://github.com/rubocop-hq/rubocop/blob/master/LICENSE.txt

WebMock https://github.com/bblimke/webmock
Copyright © 2009-2010 Bartosz Blimke
License (MIT): https://github.com/bblimke/webmock/blob/master/LICENSE

ruby-sdk-1's People

Contributors

alda-optimizely avatar aliabbasrizvi avatar bcobb avatar brandonhudavid avatar delikat avatar elliotykim avatar gaganawhad avatar jaeopt avatar jasonkarns avatar juancarlostong avatar mauerbac avatar mikeproeng37 avatar mnoman09 avatar msohailhussain avatar nchilada avatar oakbani avatar onufryk avatar rashidsp avatar thomaszurkan-optimizely avatar yasirfolio3 avatar yuanc avatar zashraf1985 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.