-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier.rb
44 lines (36 loc) · 1002 Bytes
/
notifier.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
require "net/https"
class Notifier
PROWL_APPLICATION = "FinShop"
PROWL_URL = URI("https://api.prowlapp.com/publicapi/add")
PROWL_KEY = ENV["PROWL_KEY"]
def initialize(products)
@products = products
end
def notify(channel = :stdout)
return if @products.empty?
case channel
when :prowl then notify_prowl
else notify_stdout
end
end
def notify_prowl
raise MissingKeyError unless PROWL_KEY
Net::HTTP.post_form(PROWL_URL,
apikey: PROWL_KEY,
application: PROWL_APPLICATION,
description: message)
rescue MissingKeyError
puts "Missing PROWL_KEY, falling back to stdout."
notify_stdout
end
def notify_stdout
puts message
end
def message
message = "New products found:\n"
@products.inject(message) do |memo, product|
memo += "#{product.title} (€#{product.price})\n"
end
end
end
class MissingKeyError < StandardError; end