Class: Sixpack::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/sixpack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={}, params={})
  # options supplied directly will override the configured options
  options = Sixpack.configuration.to_hash.merge(options)

  @base_url = options[:base_url]
  @user = options[:user]
  @password = options[: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_urlObject (readonly)

Returns the value of attribute base_url.



28
29
30
# File 'lib/sixpack.rb', line 28

def base_url
  @base_url
end

#client_idObject

Returns the value of attribute client_id.



29
30
31
# File 'lib/sixpack.rb', line 29

def client_id
  @client_id
end

#ip_addressObject

Returns the value of attribute ip_address.



29
30
31
# File 'lib/sixpack.rb', line 29

def ip_address
  @ip_address
end

#user_agentObject

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



94
95
96
97
98
99
100
101
102
# File 'lib/sixpack.rb', line 94

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, kpi = nil) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/sixpack.rb', line 85

def convert(experiment_name, kpi = nil)
  params = {
    :client_id => @client_id,
    :experiment => experiment_name
  }
  params = params.merge(kpi: kpi) if kpi
  self.get_response("/convert", params)
end

#get_response(endpoint, params) ⇒ Object



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
131
132
# File 'lib/sixpack.rb', line 104

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 = 1.0
  http.read_timeout = 1.0
  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



134
135
136
137
138
# File 'lib/sixpack.rb', line 134

def parse_response(res)
  JSON.parse(res.body)
rescue JSON::ParserError
  {"status" => "failed", "response" => res.body}
end

#participate(experiment_name, alternatives, force = nil, kpi = 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
83
# File 'lib/sixpack.rb', line 52

def participate(experiment_name, alternatives, force=nil, kpi=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
  }
  params = params.merge(kpi: kpi) if kpi
  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