forked from zh/webglue
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevent.rb
More file actions
86 lines (70 loc) · 1.83 KB
/
Copy pathevent.rb
File metadata and controls
86 lines (70 loc) · 1.83 KB
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
52
53
54
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
82
83
84
85
86
module WebGlue
class Event
NEW_TOPIC_EVENT_CODE = 1
NEW_SUBSCRIPTION_EVENT_CODE = 2
FEED_UPDATED_EVENT_CODE = 3
attr_reader :timestamp, :topic_id, :subscription_id
def initialize(timestamp, code, topic_id, subscription_id)
@timestamp = timestamp
@code = code
@topic_id = topic_id
@subscription_id = subscription_id
end
def to_hash
{
:timestamp => @timestamp,
:code => @code,
:topic_id => @topic_id,
:subscription_id => @subscription_id
}
end
def self.from_hash(hash)
case hash[:code]
when NEW_TOPIC_EVENT_CODE
NewTopicEvent.from_hash(hash)
when NEW_SUBSCRIPTION_EVENT_CODE
NewSubscriptionEvent.from_hash(hash)
when FEED_UPDATED_EVENT_CODE
FeedUpdatedEvent.from_hash(hash)
end
end
private
@timestamp
@code
@topic_id
@subscription_id
end
class NewTopicEvent < Event
def initialize(timestamp, topic_id)
super(timestamp, NEW_TOPIC_EVENT_CODE, topic_id, nil)
end
def self.from_hash(hash)
self.new(hash[:timestamp], hash[:topic_id])
end
def to_string
"New topic"
end
end
class NewSubscriptionEvent < Event
def initialize(timestamp, topic_id, subscription_id)
super(timestamp, NEW_SUBSCRIPTION_EVENT_CODE, topic_id, subscription_id)
end
def self.from_hash(hash)
self.new(hash[:timestamp], hash[:topic_id], hash[:subscription_id])
end
def to_string
"New subscription"
end
end
class FeedUpdatedEvent < Event
def initialize(timestamp, topic_id)
super(timestamp, FEED_UPDATED_EVENT_CODE, topic_id, nil)
end
def self.from_hash(hash)
self.new(hash[:timestamp], hash[:topic_id])
end
def to_string
"Feed updated"
end
end
end