Class: Coupon

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item, total_price_without_tax) ⇒ Coupon

Returns a new instance of Coupon.



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
# File 'lib/order_coupon.rb', line 55

def initialize(item, total_price_without_tax)
  name_element_text = item.css("h4").text
  coupon_name = name_element_text.sub("\\", "¥").sub("Expires soon", "")
  expires_soon = name_element_text.include?("Expires soon") ? "Expires soon" : ""

  coupon_link = item.css(".jso-userCuponUse").first || {}

  yen_value = coupon_name.scan(/¥(\d+)/).flatten.first.to_i
  percent_value = coupon_name.scan(/(\d+)%/).flatten.first.to_i

  if yen_value != 0
    real_value = yen_value * 1.08 # 8% tax
  elsif percent_value != 0
    real_value = (total_price_without_tax / (100 / percent_value)) * 1.08 # 8% tax
  end

  error = item.css(".m-input__error").text
  error = error && error.strip != "" ? "\n    #{error}" : nil

  self.name = coupon_name
  self.expiry = item.css(".m-entryPeriod").text.scan(/\d{4}-\d{2}-\d{2}/).first
  self.error = error
  self.couponcd = coupon_link["couponcd"]
  self.couponseq = coupon_link["couponseq"]
  self.expires_soon = expires_soon
  self.real_value = real_value.to_i
end

Instance Attribute Details

#couponcdObject

Returns the value of attribute couponcd.



53
54
55
# File 'lib/order_coupon.rb', line 53

def couponcd
  @couponcd
end

#couponseqObject

Returns the value of attribute couponseq.



53
54
55
# File 'lib/order_coupon.rb', line 53

def couponseq
  @couponseq
end

#errorObject

Returns the value of attribute error.



53
54
55
# File 'lib/order_coupon.rb', line 53

def error
  @error
end

#expires_soonObject

Returns the value of attribute expires_soon.



53
54
55
# File 'lib/order_coupon.rb', line 53

def expires_soon
  @expires_soon
end

#expiryObject

Returns the value of attribute expiry.



53
54
55
# File 'lib/order_coupon.rb', line 53

def expiry
  @expiry
end

#nameObject

Returns the value of attribute name.



53
54
55
# File 'lib/order_coupon.rb', line 53

def name
  @name
end

#real_valueObject

Returns the value of attribute real_value.



53
54
55
# File 'lib/order_coupon.rb', line 53

def real_value
  @real_value
end

Instance Method Details

#list_itemObject



91
92
93
94
# File 'lib/order_coupon.rb', line 91

def list_item
  "#{name.colorize(:blue)} (-¥#{real_value.to_s.colorize(:green)}) "\
  "#{expires_soon.colorize(:yellow)} #{expiry} #{error.to_s.colorize(:red)}".strip
end

#paramsObject



87
88
89
# File 'lib/order_coupon.rb', line 87

def params
  { couponcd: couponcd, couponseq: couponseq }.compact
end

#usable?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/order_coupon.rb', line 83

def usable?
  couponcd && couponseq
end