notifiable-sender 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/console +1 -1
- data/lib/notifiable/sender.rb +26 -29
- data/lib/notifiable/sender/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a249ebe19e8abf711ae1830ed30878b403a786a8
|
4
|
+
data.tar.gz: 2ec98a7335cee4311398278f650484804298bd7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: affe2b40c14243ce23a772079fc055fff40754ba16d4fa9a1b07a2690b36bbb90753685bbb8962091113a43b3dafd3a9f2306f92ee8c084525872c5cc4953d8c
|
7
|
+
data.tar.gz: 5f3ee8e9d2af9980fffc648e435ad3a21c77811e3dd0e80a0bdcc0eee51a448ab32b07befee39d4473d40116a39f861dd346bf2ecbb99f695542063fbbfe48b8
|
data/bin/console
CHANGED
@@ -12,7 +12,7 @@ require "notifiable/sender"
|
|
12
12
|
|
13
13
|
# convenience method
|
14
14
|
def send_notification(recipient_email_address, message, access_id = ENV['access_id'], secret_key = ENV['secret_key'])
|
15
|
-
sender = Notifiable::Sender
|
15
|
+
sender = Notifiable::Sender.new 'https://notifiable-staging.herokuapp.com', access_id, secret_key: secret_key
|
16
16
|
sender.send_notification_to_user(recipient_email_address, message: message)
|
17
17
|
end
|
18
18
|
|
data/lib/notifiable/sender.rb
CHANGED
@@ -5,37 +5,34 @@ require 'json'
|
|
5
5
|
require 'logger'
|
6
6
|
|
7
7
|
module Notifiable
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
raise 'access_id cannot be nil' if access_id.empty?
|
13
|
-
|
14
|
-
@base_uri, @access_id, @secret_key, @logger = base_uri, access_id, secret_key, logger
|
15
|
-
end
|
16
|
-
|
17
|
-
def send_notification_to_user(user_alias, message: nil, parameters: nil, content_available: nil)
|
18
|
-
filters = [{property: "user_alias", predicate: "eq", value: user_alias}]
|
19
|
-
send_notification(message: message, parameters: parameters, content_available: content_available, filters: filters)
|
20
|
-
end
|
8
|
+
class Sender
|
9
|
+
def initialize(base_uri, access_id, secret_key: nil, logger: Logger.new(STDOUT))
|
10
|
+
raise 'base_uri cannot be nil' if base_uri.empty?
|
11
|
+
raise 'access_id cannot be nil' if access_id.empty?
|
21
12
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
13
|
+
@base_uri, @access_id, @secret_key, @logger = base_uri, access_id, secret_key, logger
|
14
|
+
end
|
15
|
+
|
16
|
+
def send_notification_to_user(user_alias, message: nil, parameters: nil, content_available: nil)
|
17
|
+
filters = [{property: "user_alias", predicate: "eq", value: user_alias}]
|
18
|
+
send_notification(message: message, parameters: parameters, content_available: content_available, filters: filters)
|
19
|
+
end
|
20
|
+
|
21
|
+
def send_notification(message: nil, parameters: nil, filters: nil, content_available: nil)
|
22
|
+
body = {}
|
23
|
+
body[:message] = message unless message.nil?
|
24
|
+
body[:parameters] = parameters.to_json unless parameters.nil?
|
25
|
+
body[:filters] = filters.to_json unless filters.nil?
|
26
|
+
body[:content_available] = content_available unless content_available.nil?
|
27
|
+
headers = {}
|
28
|
+
headers[:authorization] = @access_id unless @secret_key
|
29
|
+
@request = RestClient::Request.new url: "#{@base_uri}/api/v1/notifications", payload: {notification: body}, method: :post, headers: headers
|
30
|
+
@request = ApiAuth.sign!(@request, @access_id, @secret_key) if @secret_key
|
31
|
+
begin
|
32
|
+
@request.execute
|
33
|
+
rescue RestClient::ExceptionWithResponse => e
|
34
|
+
@logger.warn "Request failed with code: #{e.response.code} body: #{e.response.body}"
|
37
35
|
end
|
38
|
-
|
39
36
|
end
|
40
37
|
end
|
41
38
|
end
|