Class: Sixpack::Session
- Inherits:
-
Object
- Object
- Sixpack::Session
- Defined in:
- lib/sixpack.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
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.
- #parse_response(res) ⇒ Object
- #participate(experiment_name, alternatives, force = nil) ⇒ Object
Constructor Details
#initialize(client_id = nil, options = {}, params = {}) ⇒ Session
Returns a new instance of Session.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/sixpack.rb', line 31 def initialize(client_id=nil, ={}, params={}) # options supplied directly will override the configured options = Sixpack.configuration.to_hash.merge() @base_url = [:base_url] @user = [:user] @password = [:password] 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 (readonly)
Returns the value of attribute base_url.
28 29 30 |
# File 'lib/sixpack.rb', line 28 def base_url @base_url end |
#client_id ⇒ Object
Returns the value of attribute client_id.
29 30 31 |
# File 'lib/sixpack.rb', line 29 def client_id @client_id end |
#ip_address ⇒ Object
Returns the value of attribute ip_address.
29 30 31 |
# File 'lib/sixpack.rb', line 29 def ip_address @ip_address end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
29 30 31 |
# File 'lib/sixpack.rb', line 29 def user_agent @user_agent end |
Instance Method Details
#build_params(params) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/sixpack.rb', line 92 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
84 85 86 87 88 89 90 |
# File 'lib/sixpack.rb', line 84 def convert(experiment_name) params = { :client_id => @client_id, :experiment => experiment_name } self.get_response("/convert", params) end |
#get_response(endpoint, params) ⇒ Object
102 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 129 130 |
# File 'lib/sixpack.rb', line 102 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 req = Net::HTTP::Get.new(uri.path + endpoint + "?" + query) # basic auth if @user && @password req.basic_auth(@user, @password) end res = http.request(req) rescue return {"status" => "failed", "error" => "http error"} end if res.code == "500" {"status" => "failed", "response" => res.body} else parse_response(res) end end |
#parse_response(res) ⇒ Object
132 133 134 135 136 |
# File 'lib/sixpack.rb', line 132 def parse_response(res) JSON.parse(res.body) rescue JSON::ParserError {"status" => "failed", "response" => res.body} end |
#participate(experiment_name, alternatives, force = nil) ⇒ Object
52 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 |
# File 'lib/sixpack.rb', line 52 def participate(experiment_name, alternatives, force=nil) if !(experiment_name =~ /^[a-z0-9][a-z0-9\-_ ]*$/) raise ArgumentError, "Bad experiment_name, must be lowercase, start with an alphanumeric and contain alphanumerics, dashes and underscores" 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}, must be lowercase, start with an alphanumeric and contain alphanumerics, dashes and underscores" end } params = { :client_id => @client_id, :experiment => experiment_name, :alternatives => alternatives } if !force.nil? && alternatives.include?(force) return {"status" => "ok", "alternative" => {"name" => force}, "experiment" => {"version" => 0, "name" => experiment_name}, "client_id" => @client_id} end res = self.get_response("/participate", params) # On server failure use control if res["status"] == "failed" res["alternative"] = {"name" => alternatives[0]} end res end |