Class: Rerout::Models::AbVariant

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

Overview

One A/B-test variant attached to a Smart Link. ‘id` is assigned server-side (read-only); `weight` controls the relative traffic share and defaults to `1`.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_url:, weight: 1, id: nil) ⇒ AbVariant

Returns a new instance of AbVariant.



151
152
153
154
155
156
# File 'lib/rerout/models.rb', line 151

def initialize(target_url:, weight: 1, id: nil)
  @id = id
  @target_url = target_url
  @weight = weight
  freeze
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



149
150
151
# File 'lib/rerout/models.rb', line 149

def id
  @id
end

#target_urlObject (readonly)

Returns the value of attribute target_url.



149
150
151
# File 'lib/rerout/models.rb', line 149

def target_url
  @target_url
end

#weightObject (readonly)

Returns the value of attribute weight.



149
150
151
# File 'lib/rerout/models.rb', line 149

def weight
  @weight
end

Class Method Details

.from_hash(hash) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/rerout/models.rb', line 158

def self.from_hash(hash)
  new(
    id: hash['id'],
    target_url: hash['target_url'],
    weight: hash.fetch('weight', 1)
  )
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



171
172
173
174
# File 'lib/rerout/models.rb', line 171

def ==(other)
  other.is_a?(AbVariant) && other.id == id &&
    other.target_url == target_url && other.weight == weight
end

#hashObject



177
178
179
# File 'lib/rerout/models.rb', line 177

def hash
  [self.class, id, target_url, weight].hash
end

#to_hObject

Render for create/update. The server-assigned ‘id` is never sent.



167
168
169
# File 'lib/rerout/models.rb', line 167

def to_h
  { 'target_url' => target_url, 'weight' => weight }
end