Class: Coupons

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from(source, total_price_without_tax) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/order_coupon.rb', line 33

def self.from(source, total_price_without_tax)
  doc = Nokogiri::HTML(source)
  coupons = doc.css("li").map { |item| Coupon.new(item, total_price_without_tax) }

  # Sort coupons by real value, expiration date while deranking those that cannot be used (error)
  coupons = [coupons.reject(&:error), coupons.select(&:error)].map do |coups|
    coups.group_by(&:real_value).sort.reverse.map do |_real_value, same_value_coupons|
      same_value_coupons.sort_by(&:expiry)
    end
  end.flatten

  Coupons.new(coupons)
end

Instance Method Details

#selection_listObject



47
48
49
# File 'lib/order_coupon.rb', line 47

def selection_list
  map(&:list_item)
end