eventify 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ require "simple-rss"
2
+ require "open-uri"
3
+
4
+ module Eventify::Provider
5
+ class Ticketpro < Base
6
+ URL = "http://www.ticketpro.ee/jnp/rss/index.xml"
7
+
8
+ class << self
9
+ def fetch
10
+ rss = SimpleRSS.parse open(URL)
11
+ rss.entries.map { |entry| new id: entry.guid, title: entry.title, link: entry.link, date: entry.pubDate }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,31 @@
1
+ require "logger"
2
+ require File.expand_path("../eventify", __dir__)
3
+
4
+ class Eventify::Scheduler
5
+ def call(_)
6
+ L("Fetch events.")
7
+ eventify = Eventify.new
8
+ eventify.process_new_events
9
+ L("Fetch done with #{eventify.new_events.size} new events out of #{eventify.all_events.size} events.")
10
+ rescue Exception => e
11
+ L("Fetch failed with an error \"#{e.message}\": #{e.backtrace.join("\n")}")
12
+ end
13
+
14
+ private
15
+
16
+ def L(message)
17
+ FileUtils.mkdir_p File.expand_path("../../logs", __dir__)
18
+ @logger ||= Logger.new(File.expand_path("../../logs/eventify.log", __dir__))
19
+ @logger.debug message
20
+ end
21
+
22
+ end
23
+
24
+ if $PROGRAM_NAME == __FILE__
25
+ require "rufus-scheduler"
26
+
27
+ scheduler = Rufus::Scheduler.new
28
+ scheduler.every("6h", Eventify::Scheduler, first_in: Time.now + 5)
29
+
30
+ scheduler.join
31
+ end
@@ -0,0 +1,3 @@
1
+ class Eventify
2
+ VERSION = "0.9.0"
3
+ end
@@ -0,0 +1,46 @@
1
+ require "spec_helper"
2
+
3
+ describe Eventify::Configuration do
4
+ before do
5
+ stub_const "Eventify::Configuration::PATH", File.join(Dir.tmpdir, "eventify-config.yml")
6
+ File.delete Eventify::Configuration::PATH if File.exists? Eventify::Configuration::PATH
7
+ end
8
+
9
+ context "#initialize" do
10
+ it "has default settings by default" do
11
+ configuration = Eventify::Configuration.new
12
+ configuration[:subscribers].should == ["user@example.org"]
13
+ configuration[:mail].should == Mail.delivery_method.settings
14
+ end
15
+
16
+ it "allows to override settings" do
17
+ File.open(Eventify::Configuration::PATH, "w") { |f| f.write YAML.dump(foo: "baz") }
18
+
19
+ configuration = Eventify::Configuration.new(foo: "bar")
20
+ configuration[:foo].should == "bar"
21
+ configuration[:subscribers].should == ["user@example.org"]
22
+ end
23
+
24
+ it "loads settings from file too" do
25
+ File.open(Eventify::Configuration::PATH, "w") { |f| f.write YAML.dump(bar: "foo") }
26
+
27
+ configuration = Eventify::Configuration.new(foo: "bar")
28
+ configuration[:foo].should == "bar"
29
+ configuration[:bar].should == "foo"
30
+ configuration[:subscribers].should == ["user@example.org"]
31
+ end
32
+ end
33
+
34
+ context "#save" do
35
+ it "saves config as yaml" do
36
+ Eventify::Configuration.new(foo: "bar").save
37
+ YAML.load(File.read(Eventify::Configuration::PATH))[:foo].should == "bar"
38
+ end
39
+ end
40
+
41
+ context "#[]" do
42
+ it "retrieves configuration setting" do
43
+ Eventify::Configuration.new(baz: "bar")[:baz].should == "bar"
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,142 @@
1
+ require "spec_helper"
2
+
3
+ describe Eventify::Database do
4
+ context ".save" do
5
+ it "saves events into database" do
6
+ event1 = Eventify::Provider::Base.new(
7
+ id: "86362",
8
+ title: "Koit Toome ja Karl-Erik Taukar 17.01.2014 - 21:00 - Rock Cafe, Tallinn, Eesti",
9
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138090",
10
+ date: Time.parse("2013-12-27 12:30:11"),
11
+ )
12
+ Eventify::Database.save event1
13
+
14
+ event2 = Eventify::Provider::Base.new(
15
+ id: "86363",
16
+ title: "Second event",
17
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138080",
18
+ date: Time.parse("2013-12-27 12:30:12"),
19
+ )
20
+ Eventify::Database.save event2
21
+
22
+ Eventify::Database.events.size.should == 2
23
+ Eventify::Database.events.should == [event1, event2]
24
+ end
25
+
26
+ it "raises an error when event already exists" do
27
+ event = Eventify::Provider::Base.new(
28
+ id: "86362",
29
+ title: "Koit Toome ja Karl-Erik Taukar 17.01.2014 - 21:00 - Rock Cafe, Tallinn, Eesti",
30
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138090",
31
+ date: Time.parse("2013-12-27 12:30:11"),
32
+ )
33
+ Eventify::Database.save event
34
+
35
+ expect {
36
+ Eventify::Database.save event
37
+ }.to raise_error
38
+ end
39
+ end
40
+
41
+ context ".exists?" do
42
+ it "true when event exists" do
43
+ event = Eventify::Provider::Base.new(
44
+ id: "86362",
45
+ title: "Koit Toome ja Karl-Erik Taukar 17.01.2014 - 21:00 - Rock Cafe, Tallinn, Eesti",
46
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138090",
47
+ date: Time.parse("2013-12-27 12:30:11"),
48
+ )
49
+ Eventify::Database.save(event)
50
+
51
+ Eventify::Database.should exist(event)
52
+ end
53
+
54
+ it "false when event does not exist" do
55
+ event = Eventify::Provider::Base.new(
56
+ id: "86362",
57
+ title: "Koit Toome ja Karl-Erik Taukar 17.01.2014 - 21:00 - Rock Cafe, Tallinn, Eesti",
58
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138090",
59
+ date: Time.parse("2013-12-27 12:30:11"),
60
+ )
61
+
62
+ Eventify::Database.should_not exist(event)
63
+ end
64
+
65
+ it "false when event id is different" do
66
+ event1 = Eventify::Provider::Base.new(
67
+ id: "86362",
68
+ title: "Koit Toome ja Karl-Erik Taukar 17.01.2014 - 21:00 - Rock Cafe, Tallinn, Eesti",
69
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138090",
70
+ date: Time.parse("2013-12-27 12:30:11"),
71
+ )
72
+ Eventify::Database.save(event1)
73
+
74
+ event2 = Eventify::Provider::Base.new(
75
+ id: "86363",
76
+ title: "Koit Toome ja Karl-Erik Taukar 17.01.2014 - 21:00 - Rock Cafe, Tallinn, Eesti",
77
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138090",
78
+ date: Time.parse("2013-12-27 12:30:11"),
79
+ )
80
+
81
+ Eventify::Database.should_not exist(event2)
82
+ end
83
+
84
+ it "false when event link is different" do
85
+ event1 = Eventify::Provider::Base.new(
86
+ id: "86362",
87
+ title: "Koit Toome ja Karl-Erik Taukar 17.01.2014 - 21:00 - Rock Cafe, Tallinn, Eesti",
88
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138090",
89
+ date: Time.parse("2013-12-27 12:30:11"),
90
+ )
91
+ Eventify::Database.save(event1)
92
+
93
+ event2 = Eventify::Provider::Base.new(
94
+ id: "86362",
95
+ title: "Koit Toome ja Karl-Erik Taukar 17.01.2014 - 21:00 - Rock Cafe, Tallinn, Eesti",
96
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138099",
97
+ date: Time.parse("2013-12-27 12:30:11"),
98
+ )
99
+
100
+ Eventify::Database.should_not exist(event2)
101
+ end
102
+
103
+ it "false when event provider is different" do
104
+ event1 = Eventify::Provider::Base.new(
105
+ id: "86362",
106
+ title: "Koit Toome ja Karl-Erik Taukar 17.01.2014 - 21:00 - Rock Cafe, Tallinn, Eesti",
107
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138090",
108
+ date: Time.parse("2013-12-27 12:30:11"),
109
+ )
110
+ Eventify::Database.save(event1)
111
+
112
+ event2 = Eventify::Provider::Piletilevi.new(
113
+ id: "86362",
114
+ title: "Koit Toome ja Karl-Erik Taukar 17.01.2014 - 21:00 - Rock Cafe, Tallinn, Eesti",
115
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138090",
116
+ date: Time.parse("2013-12-27 12:30:11"),
117
+ )
118
+
119
+ Eventify::Database.should_not exist(event2)
120
+ end
121
+ end
122
+
123
+ it ".events loads events from database" do
124
+ event1 = Eventify::Provider::Base.new(
125
+ id: "86362",
126
+ title: "Koit Toome ja Karl-Erik Taukar 17.01.2014 - 21:00 - Rock Cafe, Tallinn, Eesti",
127
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138090",
128
+ date: Time.parse("2013-12-27 12:30:11"),
129
+ )
130
+ Eventify::Database.save(event1)
131
+
132
+ event2 = Eventify::Provider::Piletilevi.new(
133
+ id: "86363",
134
+ title: "Koit Toome ja Karl-Erik Taukar 17.01.2014 - 21:00 - Rock Cafe, Tallinn, Eesti",
135
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138090",
136
+ date: Time.parse("2013-12-27 12:30:22"),
137
+ )
138
+ Eventify::Database.save(event2)
139
+
140
+ Eventify::Database.events.should == [event1, event2]
141
+ end
142
+ end
@@ -0,0 +1,33 @@
1
+ require "spec_helper"
2
+
3
+ describe Eventify::Mail do
4
+ context ".deliver" do
5
+ it "sends e-mail to all subscribers" do
6
+ ::Mail.should_receive(:deliver).twice
7
+
8
+ Eventify::Mail.deliver([], Eventify::Configuration.new(subscribers: ["foo@example.org", "bar@example.org"]))
9
+ end
10
+ end
11
+
12
+ it ".format" do
13
+ new_events = [
14
+ Eventify::Provider::Base.new(id: "123", title: "foo", link: "http://example.org/1", date: Time.now),
15
+ Eventify::Provider::Piletilevi.new(id: "456", title: "bar", link: "http://example.org/2", date: Time.now),
16
+ Eventify::Provider::Base.new(id: "456", title: "bar3", link: "http://example.org/3", date: Time.now)
17
+ ]
18
+
19
+ Eventify::Mail.format(new_events).should == "There are some rumours going on about 3 possible awesome events:
20
+
21
+ * bar
22
+ http://example.org/2
23
+
24
+ * bar3
25
+ http://example.org/3
26
+
27
+ * foo
28
+ http://example.org/1
29
+
30
+ Your Humble Servant,
31
+ Eventify"
32
+ end
33
+ end
@@ -0,0 +1,145 @@
1
+ require "spec_helper"
2
+
3
+ describe Eventify::Provider::Base do
4
+ context ".fetch" do
5
+ it "needs to be implemented" do
6
+ class Eventify::Provider::CustomEvent < Eventify::Provider::Base; end
7
+
8
+ expect {
9
+ Eventify::Provider::CustomEvent.fetch
10
+ }.to raise_error(NotImplementedError)
11
+ end
12
+ end
13
+
14
+ context "#initialize" do
15
+ it "parses raw event" do
16
+ event = {
17
+ id: "86362",
18
+ title: "Koit Toome ja Karl-Erik Taukar 17.01.2014 - 21:00 - Rock Cafe, Tallinn, Eesti",
19
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138090",
20
+ date: Time.parse("2013-12-27 12:30:11"),
21
+ }
22
+
23
+ parsed_event = Eventify::Provider::Base.new(event)
24
+ parsed_event.id.should == "86362"
25
+ parsed_event.provider.should == "Eventify::Provider::Base"
26
+ parsed_event.title.should == event[:title]
27
+ parsed_event.link.should == event[:link]
28
+ parsed_event.date.should == event[:date]
29
+ end
30
+
31
+ it "raises an error when id is missing" do
32
+ event = {
33
+ title: "Koit Toome ja Karl-Erik Taukar 17.01.2014 - 21:00 - Rock Cafe, Tallinn, Eesti",
34
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138090",
35
+ date: Time.parse("2013-12-27 12:30:11"),
36
+ }
37
+
38
+ expect {
39
+ Eventify::Provider::Base.new(event)
40
+ }.to raise_error(Eventify::Provider::Base::MissingAttributeError)
41
+ end
42
+
43
+ it "raises an error when title is missing" do
44
+ event = {
45
+ id: "86362",
46
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138090",
47
+ date: Time.parse("2013-12-27 12:30:11"),
48
+ }
49
+
50
+ expect {
51
+ Eventify::Provider::Base.new(event)
52
+ }.to raise_error(Eventify::Provider::Base::MissingAttributeError)
53
+ end
54
+
55
+ it "raises an error when link is missing" do
56
+ event = {
57
+ id: "86362",
58
+ title: "Koit Toome ja Karl-Erik Taukar 17.01.2014 - 21:00 - Rock Cafe, Tallinn, Eesti",
59
+ date: Time.parse("2013-12-27 12:30:11"),
60
+ }
61
+
62
+ expect {
63
+ Eventify::Provider::Base.new(event)
64
+ }.to raise_error(Eventify::Provider::Base::MissingAttributeError)
65
+ end
66
+ end
67
+
68
+ context "#provider" do
69
+ it "uses class name" do
70
+ class Eventify::Provider::CustomEvent < Eventify::Provider::Base; end
71
+ Eventify::Provider::CustomEvent.new(id: "123", title: "foo", link: "http://example.org").provider.should == "Eventify::Provider::CustomEvent"
72
+ end
73
+ end
74
+
75
+ context "#==" do
76
+ it "true when id and provider match" do
77
+ Eventify::Provider::Base.new(id: "123", title: "foo", link: "http://example.org").should == Eventify::Provider::Base.new(id: "123", title: "foo", link: "http://example.org")
78
+ end
79
+
80
+ it "false when id does not match" do
81
+ Eventify::Provider::Base.new(id: "123", title: "foo", link: "http://example.org").should_not == Eventify::Provider::Base.new(id: "321", title: "foo", link: "http://example.org")
82
+ end
83
+
84
+ it "false when class does not match" do
85
+ class Eventify::Provider::CustomEvent < Eventify::Provider::Base; end
86
+ Eventify::Provider::CustomEvent.new(id: "123", title: "foo", link: "http://example.org").should_not == Eventify::Provider::Base.new(id: "123", title: "foo", link: "http://example.org")
87
+ end
88
+ end
89
+
90
+ context "#save" do
91
+ it "saves event into database" do
92
+ event = {
93
+ id: "86362",
94
+ title: "Koit Toome ja Karl-Erik Taukar 17.01.2014 - 21:00 - Rock Cafe, Tallinn, Eesti",
95
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138090",
96
+ date: Time.parse("2013-12-27 12:30:11"),
97
+ }
98
+ Eventify::Provider::Base.new(event).save
99
+
100
+ Eventify::Database.events.size.should == 1
101
+ end
102
+
103
+ it "returns self" do
104
+ event = {
105
+ id: "86362",
106
+ title: "Koit Toome ja Karl-Erik Taukar 17.01.2014 - 21:00 - Rock Cafe, Tallinn, Eesti",
107
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138090",
108
+ date: Time.parse("2013-12-27 12:30:11"),
109
+ }
110
+ event_instance = Eventify::Provider::Base.new(event)
111
+ event_instance.save.should == event_instance
112
+ end
113
+ end
114
+
115
+ context "#exists?" do
116
+ it "true for existing event" do
117
+ event = {
118
+ id: "86362",
119
+ title: "Koit Toome ja Karl-Erik Taukar 17.01.2014 - 21:00 - Rock Cafe, Tallinn, Eesti",
120
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138090",
121
+ date: Time.parse("2013-12-27 12:30:11"),
122
+ }
123
+ Eventify::Provider::Base.new(event).save
124
+
125
+ Eventify::Provider::Base.new(event).should exist
126
+ end
127
+
128
+ it "false for not existing event" do
129
+ event = {
130
+ id: "86362",
131
+ title: "Koit Toome ja Karl-Erik Taukar 17.01.2014 - 21:00 - Rock Cafe, Tallinn, Eesti",
132
+ link: "http://www.piletilevi.ee/est/piletid/muusika/rock_ja_pop/?concert=138090",
133
+ date: Time.parse("2013-12-27 12:30:11"),
134
+ }
135
+
136
+ Eventify::Provider::Base.new(event).should_not exist
137
+ end
138
+ end
139
+
140
+ it "sorts by title" do
141
+ event1 = Eventify::Provider::Base.new(id: "123", title: "foo", link: "http://example.org")
142
+ event2 = Eventify::Provider::Piletilevi.new(id: "123", title: "bar", link: "http://example.org")
143
+ [event1, event2].sort.should == [event2, event1]
144
+ end
145
+ end
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ describe Eventify::Provider::FBI do
4
+ context "#fetch" do
5
+ it "fetches events" do
6
+ stub_request(:get, Eventify::Provider::FBI::URL).to_return(body: File.read(File.expand_path("../../support/fbi.html", __dir__)))
7
+
8
+ events = Eventify::Provider::FBI.fetch
9
+ events.should == [
10
+ Eventify::Provider::FBI.new(
11
+ title: "BONOBO Tallinna kontsert on välja müüdud",
12
+ link: "http://www.fbi.ee/bonobo-tallinna-kontsert-on-valja-muudud/",
13
+ date: Time.now,
14
+ id: "http://www.fbi.ee/bonobo-tallinna-kontsert-on-valja-muudud/"
15
+ ),
16
+ Eventify::Provider::FBI.new(
17
+ title: "Rokilegend ROBERT PLANT esineb oma bändiga Tallinnas",
18
+ link: "http://www.fbi.ee/rokilegend-robert-plant-esineb-oma-bandiga-tallinnas/",
19
+ date: Time.now,
20
+ id: "http://www.fbi.ee/rokilegend-robert-plant-esineb-oma-bandiga-tallinnas/"
21
+ )
22
+ ]
23
+ end
24
+ end
25
+ end