sixpack-client 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sixpack.rb CHANGED
@@ -1,51 +1,47 @@
1
+ require "addressable/uri"
1
2
  require "net/http"
2
3
  require "json"
3
-
4
4
  require "uuid"
5
+ require "uri"
5
6
 
6
7
  require "sixpack/version"
7
8
 
8
9
  module Sixpack
9
- @@host = "localhost"
10
- def self.host
11
- @@host
12
- end
13
- def self.host=(host)
14
- @@host = host
15
- end
10
+ extend self
16
11
 
17
- @@port = 5000
18
- def self.port
19
- @@port
20
- end
21
- def self.port=(port)
22
- @@port = port
23
- end
12
+ attr_accessor :base_url
24
13
 
25
- def self.simple_participate(experiment_name, alternatives, client_id=nil, force=nil)
14
+ @base_url = "http://localhost:5000"
15
+
16
+ def simple_participate(experiment_name, alternatives, client_id=nil, force=nil)
26
17
  session = Session.new(client_id)
27
18
  res = session.participate(experiment_name, alternatives, force)
28
- res["alternative"]
19
+ res["alternative"]["name"]
29
20
  end
30
21
 
31
- def self.simple_convert(experiment_name, client_id)
22
+ def simple_convert(experiment_name, client_id)
32
23
  session = Session.new(client_id)
33
24
  session.convert(experiment_name)["status"]
34
25
  end
35
26
 
36
- def self.generate_client_id
27
+ def generate_client_id
37
28
  uuid = UUID.new
38
29
  uuid.generate
39
30
  end
40
31
 
41
32
  class Session
42
- attr_accessor :host, :port, :client_id
33
+ attr_accessor :base_url, :client_id, :ip_address, :user_agent
43
34
 
44
- def initialize(client_id=nil, options={})
45
- default_options = {:host => Sixpack.host, :port => Sixpack.port}
35
+ def initialize(client_id=nil, options={}, params={})
36
+ default_options = {:base_url => Sixpack.base_url}
46
37
  options = default_options.merge(options)
47
- @host = options[:host]
48
- @port = options[:port]
38
+ @base_url = options[:base_url]
39
+
40
+ default_params = {:ip_address => nil, :user_agent => :nil}
41
+ params = default_params.merge(params)
42
+
43
+ @ip_address = params[:ip_address]
44
+ @user_agent = params[:user_agent]
49
45
 
50
46
  if client_id.nil?
51
47
  @client_id = Sixpack::generate_client_id()
@@ -78,7 +74,12 @@ module Sixpack
78
74
  params[:force] = force
79
75
  end
80
76
 
81
- self.get_response("/participate", params)
77
+ res = self.get_response("/participate", params)
78
+ # On server failure use control
79
+ if res["status"] == "failed"
80
+ res["alternative"] = {:name => alternatives[0]}
81
+ end
82
+ res
82
83
  end
83
84
 
84
85
  def convert(experiment_name)
@@ -89,10 +90,36 @@ module Sixpack
89
90
  self.get_response("/convert", params)
90
91
  end
91
92
 
93
+ def build_params(params)
94
+ unless @ip_address.nil?
95
+ params[:ip_address] = @ip_address
96
+ end
97
+ unless @user_agent.nil?
98
+ params[:user_agent] = @user_agent
99
+ end
100
+ params
101
+ end
102
+
92
103
  def get_response(endpoint, params)
93
- uri = URI("http://#{@host}:#{@port}" + endpoint)
94
- uri.query = URI.encode_www_form(params)
95
- res = Net::HTTP.get_response(uri)
104
+ uri = URI.parse(@base_url)
105
+ http = Net::HTTP.new(uri.host, uri.port)
106
+
107
+ if uri.scheme == "https"
108
+ http.use_ssl = true
109
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
110
+ end
111
+
112
+ http.open_timeout = 0.25
113
+ http.read_timeout = 0.25
114
+ query = Addressable::URI.form_encode(self.build_params(params))
115
+
116
+ begin
117
+ res = http.start do |http|
118
+ http.get(uri.path + endpoint + "?" + query)
119
+ end
120
+ rescue
121
+ return {"status" => "failed", "error" => "http error"}
122
+ end
96
123
  if res.code == "500"
97
124
  {"status" => "failed", "response" => res.body}
98
125
  else
@@ -1,3 +1,3 @@
1
1
  module Sixpack
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
data/sixpack.gemspec CHANGED
@@ -20,4 +20,5 @@ Gem::Specification.new do |gem|
20
20
  gem.add_development_dependency 'redis'
21
21
 
22
22
  gem.add_runtime_dependency 'uuid'
23
+ gem.add_runtime_dependency 'addressable'
23
24
  end
@@ -15,6 +15,14 @@ describe Sixpack do
15
15
  ['trolled', 'not-trolled'].should include(alternative)
16
16
  end
17
17
 
18
+ it "should allow ip and user agent to be passed to a session" do
19
+ params = {:ip_address => '8.8.8.8', :user_agent => 'FirChromari'}
20
+ session = Sixpack::Session.new('client_id', {}, params)
21
+ session.ip_address.should == '8.8.8.8'
22
+ session.user_agent.should == 'FirChromari'
23
+
24
+ end
25
+
18
26
  it "should auto generate a client_id" do
19
27
  alternative = Sixpack.simple_participate('show-bieber', ['trolled', 'not-trolled'], nil)
20
28
  ['trolled', 'not-trolled'].should include(alternative)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sixpack-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-05 00:00:00.000000000 Z
12
+ date: 2013-02-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -75,6 +75,22 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: addressable
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
78
94
  description: Ruby library for interacting with SeatGeek's sixpack.
79
95
  email:
80
96
  - hi@seatgeek.com