batch_push_notification 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1ca2f7e4293f508c8a9c376d2cfa58d0a0ad10c2
4
- data.tar.gz: 84153b63904d792a69a87913e61390b3b98418ed
3
+ metadata.gz: 495fe8b6384ffbdb641be6697bdf83dace3a5e86
4
+ data.tar.gz: 2124f138cd0de9aa3d28e9e0848a0875086f4e91
5
5
  SHA512:
6
- metadata.gz: 7cdacb064d1a0710d35675f52f89b4d9d97d70b2d4b9f79d9cb2c7d67877410cb5db85e58fb98484e5a289dea042252e8b14a6b5889cf8e3f5185e8ea46313ef
7
- data.tar.gz: f2b7a637d8fc50141952905661288e5abcf78182b15f6844411b5c1946f26fa91f7c83329c579a2555d3f4d15d65db2306134764003f4cecc54c7206831e9c0b
6
+ metadata.gz: 7390b12ef0fc909d2c21941a65bd6a6d79020b31feaf8b434a0491340ff9e408aae27e2906fd63e33dae69e5b4aa65ec4d320d9f43de5461a05178115bb0d17f
7
+ data.tar.gz: 3a47a2641bc0f5b0bc032de88c18de6a2e3ea27a38c1bd8738c7e32aae55043756a7390f0d6b780a7f88a11a76ec53e6863f4a12fb9118485606abb27275835e
@@ -1,9 +1,21 @@
1
1
  module BatchPushNotification
2
2
  autoload :Configurable, 'batch_push_notification/configurable'
3
- autoload :Client, 'batch_push_notification/client'
3
+ autoload :Client, 'batch_push_notification/client'
4
4
  autoload :Notification, 'batch_push_notification/notification'
5
5
 
6
6
  class << self
7
7
  include BatchPushNotification::Configurable
8
+
9
+ attr_accessor :logger
10
+
11
+ def new(options = {})
12
+ BatchPushNotification::Client.new(options)
13
+ end
14
+
15
+ def logger
16
+ @logger ||= Logger.new($stdout).tap do |log|
17
+ log.progname = self.name
18
+ end
19
+ end
8
20
  end
9
21
  end
@@ -3,12 +3,25 @@ require 'faraday'
3
3
 
4
4
  module BatchPushNotification
5
5
  class Client
6
- def initialize
6
+ attr_accessor :api_key, :endpoint, :rest_api_key, :sandbox
7
+
8
+ def initialize(options)
9
+ self.api_key = BatchPushNotification.api_key
10
+ self.endpoint = BatchPushNotification.endpoint
11
+ self.rest_api_key = BatchPushNotification.rest_api_key
12
+ self.sandbox = BatchPushNotification.sandbox
13
+
14
+ # overwrite settings:
15
+ options.each do |key, value|
16
+ instance_variable_set("@#{key}", value)
17
+ end
18
+
7
19
  # fail if the required params are not set:
8
- raise(StandardError, 'Configuration is missing') unless BatchPushNotification.endpoint && BatchPushNotification.api_key && BatchPushNotification.rest_api_key && !BatchPushNotification.sandbox.nil?
20
+ raise(StandardError, 'Configuration is missing') unless self.api_key && self.endpoint&& self.rest_api_key && !self.sandbox.nil?
9
21
  end
10
22
 
11
23
  def send_notification(notification, &on_complete)
24
+ BatchPushNotification.logger.debug("Sending Notification")
12
25
  send_asynchronously(notification) do |response|
13
26
  on_complete.call(response)
14
27
  end
@@ -16,24 +29,26 @@ module BatchPushNotification
16
29
 
17
30
  def send_asynchronously(notification, &on_complete)
18
31
  Thread.new do
19
- response = connection.post send_url, notification.payload
32
+ BatchPushNotification.logger.debug("Started new thread")
33
+ response = connection.post send_url, notification.payload(self.sandbox)
34
+ BatchPushNotification.logger.debug("Post successful, handling body next")
20
35
  on_complete.call(JSON.parse(response.body))
21
36
  end
22
37
  end
23
38
 
24
39
 
25
40
  def connection
26
- return Faraday.new(:url => BatchPushNotification.endpoint) do |faraday|
41
+ return Faraday.new(:url => self.endpoint) do |faraday|
27
42
  faraday.response :logger # log requests to STDOUT
28
43
  faraday.headers['Content-Type'] = 'application/json'
29
- faraday.headers['X-Authorization'] = BatchPushNotification.rest_api_key
44
+ faraday.headers['X-Authorization'] = self.rest_api_key
30
45
 
31
46
  faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
32
47
  end
33
48
  end
34
49
 
35
50
  def send_url
36
- BatchPushNotification.api_key + '/transactional/send'
51
+ self.api_key + '/transactional/send'
37
52
  end
38
53
  end
39
54
  end
@@ -9,7 +9,7 @@ module BatchPushNotification
9
9
  @custom_payload = args[:custom_payload]
10
10
  end
11
11
 
12
- def payload
12
+ def payload(sandbox)
13
13
  {
14
14
  "group_id": @group_id,
15
15
  "recipients": {
@@ -21,7 +21,7 @@ module BatchPushNotification
21
21
  "body": @body
22
22
  },
23
23
  "custom_payload": @custom_payload.to_json.to_s, # the API expects a string instead of a JSON object
24
- sandbox: BatchPushNotification.sandbox
24
+ sandbox: sandbox
25
25
  }.to_json
26
26
  end
27
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: batch_push_notification
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicholas Wittstruck
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-04 00:00:00.000000000 Z
11
+ date: 2016-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday