voltron-notify 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d2ae1869885e5f129a6c20e8e12bfd0263424900
4
- data.tar.gz: c8b75c880495daa9451a31d1552dac891b478b71
3
+ metadata.gz: a04574134da60686957218850a216519895e16b7
4
+ data.tar.gz: 59e67db3dcfc17a5e29f43ff6aa6c289425e9fbe
5
5
  SHA512:
6
- metadata.gz: 31097ac5f69cb936c8255b677e456bb5f320bc7c5cfa9636dbabecead0d8a7c9bd062431b4be5fa2897c76523271c102d8562244a0a14de56e7cafb68cb20730
7
- data.tar.gz: 3851a3f22b201ba6a06bd5cc849700a554ca8405d872f8378616e3fecd801ca571e817c87091b2ecee614ff7f26556f909203a91c189bee543f37934fc5a45f4
6
+ metadata.gz: 7989905b87687408ccb33d4377a9593efc607d855443518e853a3502d8c70bb5c11315156cffd2197218dd010385372ab35b6becce91bf753ef6c1d125d173e5
7
+ data.tar.gz: 7818586e3df9333880066dc55d3667dc3429610b3ea54d9e3118423e5bf5259223904609c2d0ecc80ccac6e09719d613272b6b8db8cf4c012f225ba59118a46e
@@ -14,7 +14,7 @@ class Voltron::Notification::SmsNotification < ActiveRecord::Base
14
14
 
15
15
  after_create :deliver_later, if: :use_queue?
16
16
 
17
- validates :status, presence: false, inclusion: { in: %w( queued failed sent delivered undelivered ), message: "must be one of: queued, failed, sent, delivered, undelivered" }, on: :update
17
+ validates :status, presence: false, inclusion: { in: %w( accepted queued sending sent delivered received failed undelivered unknown ), message: 'must be one of: accepted, queued, sending, sent, delivered, received, failed, undelivered, or unknown' }, on: :update
18
18
 
19
19
  def setup
20
20
  @request = []
@@ -22,29 +22,28 @@ class Voltron::Notification::SmsNotification < ActiveRecord::Base
22
22
  end
23
23
 
24
24
  def request
25
- # Wrap entire request in container hash so that we can call deep_symbolize_keys on it (in case it's an array)
26
- # Wrap entire request in array and flatten so we can be sure the result is an array
27
- [{ request: (JSON.parse(request_json) rescue nil) }.deep_symbolize_keys[:request]].flatten.compact
25
+ # Ensure returned object is an array, whose containing hashes all have symbolized keys, for consistency
26
+ out = Array.wrap((JSON.parse(request_json) rescue nil)).compact
27
+ out.each { |i| i.try(:deep_symbolize_keys!) }
28
+ out
28
29
  end
29
30
 
30
31
  def response
31
- # Wrap entire response in container hash so that we can call deep_symbolize_keys on it (in case it's an array)
32
- # Wrap entire response in array and flatten so we can be sure the result is an array
33
- [{ response: (JSON.parse(response_json) rescue nil) }.deep_symbolize_keys[:response]].flatten.compact
32
+ # Ensure returned object is an array, whose containing hashes all have symbolized keys, for consistency
33
+ out = Array.wrap((JSON.parse(response_json) rescue nil)).compact
34
+ out.each { |i| i.try(:deep_symbolize_keys!) }
35
+ out
34
36
  end
35
37
 
36
38
  def after_deliver
37
- if use_queue?
38
- # if use_queue?, meaning if this was sent via ActiveJob, we need to update ourself
39
- # since we got to here within after_create, meaning setting the attributes alone won't cut it
40
- self.update(request_json: @request.to_json, response_json: @response.to_json, sid: @response.first[:sid], status: @response.first[:status])
41
- else
42
- # We are before_create so we can just set the attribute values, it will be saved after this
43
- self.request_json = @request.to_json
44
- self.response_json = @response.to_json
45
- self.sid = response.first[:sid]
46
- self.status = response.first[:status]
47
- end
39
+ self.request_json = @request.to_json
40
+ self.response_json = @response.to_json
41
+ self.sid = response.first.try(:[], :sid)
42
+ self.status = response.first.try(:[], :status) || 'unknown'
43
+
44
+ # if use_queue?, meaning if this was sent via ActiveJob, we need to save ourself
45
+ # since we got to here within after_create, meaning setting the attributes alone won't cut it
46
+ self.save if use_queue?
48
47
  end
49
48
 
50
49
  def deliver_now
@@ -53,14 +52,14 @@ class Voltron::Notification::SmsNotification < ActiveRecord::Base
53
52
  # If sending more than 1 attachment, iterate through all but one attachment and send each without a body...
54
53
  if all_attachments.count > 1
55
54
  begin
56
- client.messages.create({ from: from_formatted, to: to_formatted, media_url: all_attachments.shift, status_callback: try(:update_voltron_notification_url, host: Voltron.config.base_url) }.compact)
55
+ client.messages.create({ from: from_formatted, to: to_formatted, media_url: all_attachments.shift, status_callback: callback_url }.compact)
57
56
  @request << Rack::Utils.parse_nested_query(client.last_request.body)
58
57
  @response << JSON.parse(client.last_response.body)
59
58
  end until all_attachments.count == 1
60
59
  end
61
60
 
62
61
  # ... Then send the last attachment (if any) with the actual text body. This way we're not sending multiple SMS's with same body
63
- client.messages.create({ from: from_formatted, to: to_formatted, body: message, media_url: all_attachments.shift, status_callback: try(:update_voltron_notification_url, host: Voltron.config.base_url) }.compact)
62
+ client.messages.create({ from: from_formatted, to: to_formatted, body: message, media_url: all_attachments.shift, status_callback: callback_url }.compact)
64
63
  @request << Rack::Utils.parse_nested_query(client.last_request.body)
65
64
  @response << JSON.parse(client.last_response.body)
66
65
  after_deliver
@@ -68,13 +67,13 @@ class Voltron::Notification::SmsNotification < ActiveRecord::Base
68
67
 
69
68
  def deliver_later
70
69
  job = Voltron::SmsJob.set(wait: Voltron.config.notify.delay).perform_later self
71
- @request << job.to_json
72
- @response << { sid: nil, status: "enqueued" }
70
+ @request << job
71
+ @response << { sid: nil, status: 'unknown' }
73
72
  after_deliver
74
73
  end
75
74
 
76
75
  def attach(url)
77
- if url.starts_with? "http"
76
+ if url.starts_with? 'http'
78
77
  attachments.build attachment: url
79
78
  else
80
79
  attachments.build attachment: Voltron.config.base_url + ActionController::Base.helpers.asset_url(url)
@@ -87,7 +86,7 @@ class Voltron::Notification::SmsNotification < ActiveRecord::Base
87
86
  to_formatted
88
87
  true
89
88
  rescue => e
90
- Voltron.log e.message, "Notify", :light_red
89
+ Voltron.log e.message, 'Notify', :light_red
91
90
  false
92
91
  end
93
92
  end
@@ -95,13 +94,20 @@ class Voltron::Notification::SmsNotification < ActiveRecord::Base
95
94
  # TODO: Move this to actual validates_* methods
96
95
  def error_messages
97
96
  output = []
98
- output << "recipient cannot be blank" if to.blank?
99
- output << "recipient is not a valid phone number" unless valid_phone?
100
- output << "sender cannot be blank" if from.blank?
101
- output << "message cannot be blank" if message.blank?
97
+ output << 'recipient cannot be blank' if to.blank?
98
+ output << 'recipient is not a valid phone number' unless valid_phone?
99
+ output << 'sender cannot be blank' if from.blank?
100
+ output << 'message cannot be blank' if message.blank?
102
101
  output
103
102
  end
104
103
 
104
+ def callback_url
105
+ url = try(:update_voltron_notification_url, host: Voltron.config.base_url).to_s
106
+ # Don't allow local or blank urls
107
+ return nil if url.include?('localhost') || url.include?('127.0.0.1') || url.blank?
108
+ url
109
+ end
110
+
105
111
  private
106
112
 
107
113
  def use_queue?
@@ -1,5 +1,5 @@
1
1
  module Voltron
2
2
  module Notify
3
- VERSION = "0.1.7".freeze
3
+ VERSION = '0.1.8'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: voltron-notify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Hainer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-13 00:00:00.000000000 Z
11
+ date: 2017-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -220,7 +220,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
220
220
  version: '0'
221
221
  requirements: []
222
222
  rubyforge_project:
223
- rubygems_version: 2.4.8
223
+ rubygems_version: 2.6.6
224
224
  signing_key:
225
225
  specification_version: 4
226
226
  summary: Send notifications easier