-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.rb
49 lines (42 loc) · 1.01 KB
/
product.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Product
attr_writer :available, :new
attr_reader :updated_at
attr_accessor :title, :url, :price
def initialize(title:, price:, url:, available:)
@title = title
@price = price
@url = url
@available = available
@new = true
@updated_at = Time.now.round
end
def self.from_results(results)
results.inject([]) do |memo, result|
memo << Product.new(title: result[:title],
url: result[:url],
price: result[:price],
available: result[:available])
end
end
def available?
@available
end
def new?
@new
end
def formatted_price
"€%.2f" % price
end
def equal?(product)
return false if product.url.empty?
url.downcase == product.url.downcase
end
def update(product)
@title = product.title
@price = product.price
@available = product.available?
@new = false
@updated_at = Time.now.round
self
end
end