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.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sixpack.rb', line 24

def initialize(client_id=nil, options={}, params={})
  default_options = {:base_url => Sixpack.base_url}
  options = default_options.merge(options)
  @base_url = options[: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_urlObject

Returns the value of attribute base_url.



22
23
24
# File 'lib/sixpack.rb', line 22

def base_url
  @base_url
end

#client_idObject

Returns the value of attribute client_id.



22
23
24
# File 'lib/sixpack.rb', line 22

def client_id
  @client_id
end

#ip_addressObject

Returns the value of attribute ip_address.



22
23
24
# File 'lib/sixpack.rb', line 22

def ip_address
  @ip_address
end

#user_agentObject

Returns the value of attribute user_agent.



22
23
24
# File 'lib/sixpack.rb', line 22

def user_agent
  @user_agent
end

Instance Method Details

#build_params(params) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/sixpack.rb', line 82

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



74
75
76
77
78
79
80
# File 'lib/sixpack.rb', line 74

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

#get_response(endpoint, params) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/sixpack.rb', line 92

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sixpack.rb', line 42

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)
    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