Class: FloorCalculator::Pricer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(offer, weight, markups) ⇒ Pricer

Returns a new instance of Pricer.



7
8
9
10
11
12
13
14
15
# File 'lib/floor_calculator/pricer.rb', line 7

def initialize(offer, weight, markups)
  @partial_results = {}
  @markups = markups
  @weight = weight
  @offer = offer
  @source_catalog = offer[:source_catalog]
  @types = markups['pipelines']['floor_calculation']
  @destination = offer.catalog
end

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



5
6
7
# File 'lib/floor_calculator/pricer.rb', line 5

def destination
  @destination
end

#markupsObject (readonly)

Returns the value of attribute markups.



5
6
7
# File 'lib/floor_calculator/pricer.rb', line 5

def markups
  @markups
end

#offerObject (readonly)

Returns the value of attribute offer.



5
6
7
# File 'lib/floor_calculator/pricer.rb', line 5

def offer
  @offer
end

#partial_resultsObject (readonly)

Returns the value of attribute partial_results.



5
6
7
# File 'lib/floor_calculator/pricer.rb', line 5

def partial_results
  @partial_results
end

#source_catalogObject (readonly)

Returns the value of attribute source_catalog.



5
6
7
# File 'lib/floor_calculator/pricer.rb', line 5

def source_catalog
  @source_catalog
end

#weightObject (readonly)

Returns the value of attribute weight.



5
6
7
# File 'lib/floor_calculator/pricer.rb', line 5

def weight
  @weight
end

Instance Method Details

#calculate_floorObject



17
18
19
20
21
22
23
# File 'lib/floor_calculator/pricer.rb', line 17

def calculate_floor
  floor = offer.price
  @types.each do |method|
    floor = cost_method(method, prepare_options(offer, floor))
  end
  round_to_20s(floor.fractional)
end

#cost_method(name, options) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/floor_calculator/pricer.rb', line 25

def cost_method(name, options)
  calculated_value =
    Solver.calculate(markups, name, options).round(4)
  @partial_results[name] = calculated_value
  result = options[:price] + calculated_value
  result
end

#prepare_options(offer, price) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/floor_calculator/pricer.rb', line 37

def prepare_options(offer, price)
  {
    source: offer[:source_catalog],
    country: offer.country.to_s.downcase,
    destination: offer.catalog,
    price: price,
    condition: offer.condition,
    weight: weight,
    fast: offer[:fast]
  }
end

#round_to_20s(price) ⇒ Object



33
34
35
# File 'lib/floor_calculator/pricer.rb', line 33

def round_to_20s(price)
  (price % 20) > 0 ? ((price.to_i / 20) + 1) * 20 : price
end