sixpack-client 0.0.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/sixpack.rb +55 -28
- data/lib/sixpack/version.rb +1 -1
- data/sixpack.gemspec +1 -0
- data/spec/lib/sixpack_spec.rb +8 -0
- metadata +18 -2
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
|
-
|
10
|
-
def self.host
|
11
|
-
@@host
|
12
|
-
end
|
13
|
-
def self.host=(host)
|
14
|
-
@@host = host
|
15
|
-
end
|
10
|
+
extend self
|
16
11
|
|
17
|
-
|
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
|
-
|
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
|
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
|
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 :
|
33
|
+
attr_accessor :base_url, :client_id, :ip_address, :user_agent
|
43
34
|
|
44
|
-
def initialize(client_id=nil, options={})
|
45
|
-
default_options = {:
|
35
|
+
def initialize(client_id=nil, options={}, params={})
|
36
|
+
default_options = {:base_url => Sixpack.base_url}
|
46
37
|
options = default_options.merge(options)
|
47
|
-
@
|
48
|
-
|
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(
|
94
|
-
|
95
|
-
|
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
|
data/lib/sixpack/version.rb
CHANGED
data/sixpack.gemspec
CHANGED
data/spec/lib/sixpack_spec.rb
CHANGED
@@ -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
|
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-
|
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
|