forked from hSATAC/ruten-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.rb
51 lines (45 loc) · 1.26 KB
/
item.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
50
51
class Item < ActiveRecord::Base
OUT_OF_STOCK_PRICE = 50000
STATUS_UNCHANGED = -1
STATUS_NEW = 0
STATUS_REFILL = 1
STATUS_OUT_OF_STOCK = 2
STATUS_CHANGE_PRICE = 3
def self.import(data)
item = Item.where(:ruten_id => data[:ruten_id]).first_or_initialize
item.name = data[:name]
item.price = data[:price]
item.url = data[:url]
data[:status] = item.status_before_save
item.save
logger.info "Item #{item.name} #{item.url} imported."
end
def self.batch_import(dataset)
dataset.each { |data| Item.import(data) }
end
def status_before_save
return STATUS_NEW unless self.persisted?
return STATUS_UNCHANGED if self.price == self.price_was
if self.price > OUT_OF_STOCK_PRICE && self.price_was < OUT_OF_STOCK_PRICE
return STATUS_OUT_OF_STOCK
elsif self.price < OUT_OF_STOCK_PRICE && self.price_was > OUT_OF_STOCK_PRICE
return STATUS_REFILL
else
return STATUS_CHANGE_PRICE
end
end
end
class CreateItemMigration < ActiveRecord::Migration
def change
create_table :items do |t|
t.string :name
t.string :url
t.integer :price
t.integer :ruten_id
t.timestamps
end
add_index :items, :name
add_index :items, :price
add_index :items, :ruten_id
end
end