Skip to content

Commit

Permalink
configuration block, readme, remove UUID dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Vasilyev committed Apr 15, 2015
1 parent 6c0d00e commit b2329d7
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 16 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
language: ruby
rvm:
- 1.9.3
- 2.2.2
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Build Status](https://travis-ci.org/seatgeek/sixpack-rb.svg?branch=master)](https://travis-ci.org/seatgeek/sixpack-rb)

# Sixpack

Ruby client library for SeatGeek's Sixpack ab testing framework.
Expand Down Expand Up @@ -47,14 +49,14 @@ For future requests, create the `Session` using the `client_id` stored in the co

```ruby
client_id = get_cookie_from_web_framework("sixpack-id")
session = Sixpack::Session.new client_id
session = Sixpack::Session.new(client_id)
session.convert("new-test")
```

Sessions can take an optional `options` hash that takes `:base_url`, and a params hash that takes `:ip_address`, and `:user_agent` a keys. If you would like to instantiate a session with a known client id, you can do that here. IP address and user agent can be passed to assist in bot detection.

options = {
:host => 'http://mysixpacklocation.com',
:base_url => 'http://mysixpacklocation.com'
}
params = {
:ip_address => '1.2.3.4'
Expand All @@ -63,6 +65,30 @@ Sessions can take an optional `options` hash that takes `:base_url`, and a param

If Sixpack is unreachable or other errors occur, sixpack-rb will provide the control alternative object.

## Configuration

You can configure the Sixpack in the configure block:

```ruby
Sixpack.configure do |config|
config.base_url = 'http://10.20.30.40:5000'
end
```

You can use the `configure` block when initializing your app, for instance in a
Rails initializer.

Note that options, passed directly into `Session` constructor override the configuration options.

```ruby
Sixpack.configure do |config|
config.base_url = 'http://foo:5000'
end

s = Sixpack::Session.new(id, base_url: 'http://bar:6000')

expect(s.base_url).to eq 'http://bar:6000' #=> true
```

## Contributing

Expand Down
28 changes: 18 additions & 10 deletions lib/sixpack.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
require "addressable/uri"
require "net/http"
require "json"
require "uuid"
require "uri"

require "sixpack/version"
require "sixpack/configuration"

module Sixpack
extend self

attr_accessor :base_url
class << self

@base_url = "http://localhost:5000"
def configuration
@configuration ||= Configuration.new
end

def configure
yield(configuration)
end

def generate_client_id
uuid = UUID.new
uuid.generate
def generate_client_id
SecureRandom.uuid
end
end


class Session
attr_accessor :base_url, :client_id, :ip_address, :user_agent
attr_reader :base_url
attr_accessor :client_id, :ip_address, :user_agent

def initialize(client_id=nil, options={}, params={})
default_options = {:base_url => Sixpack.base_url}
options = default_options.merge(options)
# options supplied directly will override the configured options
options = Sixpack.configuration.to_hash.merge(options)

@base_url = options[:base_url]

default_params = {:ip_address => nil, :user_agent => :nil}
Expand Down
13 changes: 13 additions & 0 deletions lib/sixpack/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Sixpack
class Configuration
attr_accessor :base_url

def initialize
@base_url = 'http://localhost:5000'
end

def to_hash
{base_url: @base_url}
end
end
end
1 change: 0 additions & 1 deletion sixpack.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'redis'

gem.add_runtime_dependency 'json'
gem.add_runtime_dependency 'uuid'
gem.add_runtime_dependency 'addressable'
end
32 changes: 29 additions & 3 deletions spec/lib/sixpack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,37 @@

require 'spec_helper'

describe Sixpack do
RSpec.describe Sixpack do
before(:each) do
redis = Redis.new
redis.keys("*").each do |k|
redis.del(k)
redis.flushdb
end

context 'configuration' do
it 'should contain default base_url' do
s = Sixpack::Session.new("foo")
expect(s.base_url).to eq 'http://localhost:5000'
end

it 'should allow specifying the base_url in Session options' do
s = Sixpack::Session.new("foo", base_url: 'http://0.0.0.0:5555')
expect(s.base_url).to eq 'http://0.0.0.0:5555'
end

it 'should allow specifying the base_url in configuration block' do
Sixpack.configure do |config|
config.base_url = 'http://4.4.4.4'
end
s = Sixpack::Session.new("foo")
expect(s.base_url).to eq 'http://4.4.4.4'
end

it 'session base_url should override configuration base_url' do
Sixpack.configure do |config|
config.base_url = 'http://4.4.4.4'
end
s = Sixpack::Session.new("foo", base_url: 'http://5.5.5.5')
expect(s.base_url).to eq 'http://5.5.5.5'
end
end

Expand Down

0 comments on commit b2329d7

Please sign in to comment.