Class: Sixpack::Session
- Inherits:
-
Object
- Object
- Sixpack::Session
- Defined in:
- lib/sixpack.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#ip_address ⇒ Object
Returns the value of attribute ip_address.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
Instance Method Summary collapse
- #build_params(params) ⇒ Object
- #convert(experiment_name) ⇒ Object
- #get_response(endpoint, params) ⇒ Object
-
#initialize(client_id = nil, options = {}, params = {}) ⇒ Session
constructor
A new instance of Session.
- #participate(experiment_name, alternatives, force = nil) ⇒ Object
Constructor Details
#initialize(client_id = nil, options = {}, params = {}) ⇒ Session
Returns a new instance of Session.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/sixpack.rb', line 35 def initialize(client_id=nil, ={}, params={}) = {:base_url => Sixpack.base_url} = .merge() @base_url = [:base_url] default_params = {:ip_address => nil, :user_agent => :nil} params = default_params.merge(params) @ip_address = params[:ip_address] @user_agent = params[:user_agent] if client_id.nil? @client_id = Sixpack::generate_client_id() else @client_id = client_id end end |
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
33 34 35 |
# File 'lib/sixpack.rb', line 33 def base_url @base_url end |
#client_id ⇒ Object
Returns the value of attribute client_id.
33 34 35 |
# File 'lib/sixpack.rb', line 33 def client_id @client_id end |
#ip_address ⇒ Object
Returns the value of attribute ip_address.
33 34 35 |
# File 'lib/sixpack.rb', line 33 def ip_address @ip_address end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
33 34 35 |
# File 'lib/sixpack.rb', line 33 def user_agent @user_agent end |
Instance Method Details
#build_params(params) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/sixpack.rb', line 93 def build_params(params) unless @ip_address.nil? params[:ip_address] = @ip_address end unless @user_agent.nil? params[:user_agent] = @user_agent end params end |
#convert(experiment_name) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/sixpack.rb', line 85 def convert(experiment_name) params = { :client_id => @client_id, :experiment => experiment_name } self.get_response("/convert", params) end |
#get_response(endpoint, params) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/sixpack.rb', line 103 def get_response(endpoint, params) uri = URI.parse(@base_url) http = Net::HTTP.new(uri.host, uri.port) if uri.scheme == "https" http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end http.open_timeout = 0.25 http.read_timeout = 0.25 query = Addressable::URI.form_encode(self.build_params(params)) begin res = http.start do |http| http.get(uri.path + endpoint + "?" + query) end rescue return {"status" => "failed", "error" => "http error"} end if res.code == "500" {"status" => "failed", "response" => res.body} else JSON.parse(res.body) end end |
#participate(experiment_name, alternatives, force = nil) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/sixpack.rb', line 53 def participate(experiment_name, alternatives, force=nil) if !(experiment_name =~ /^[a-z0-9][a-z0-9\-_ ]*$/) raise ArgumentError, "Bad experiment_name" end if alternatives.length < 2 raise ArgumentError, "Must specify at least 2 alternatives" end alternatives.each { |alt| if !(alt =~ /^[a-z0-9][a-z0-9\-_ ]*$/) raise ArgumentError, "Bad alternative name: #{alt}" end } params = { :client_id => @client_id, :experiment => experiment_name, :alternatives => alternatives } if !force.nil? && alternatives.include?(force) params[:force] = force end res = self.get_response("/participate", params) # On server failure use control if res["status"] == "failed" res["alternative"] = {:name => alternatives[0]} end res end |