digitsend 0.0.6 → 0.0.8

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.
@@ -14,7 +14,13 @@ module DigitSend
14
14
  'Accept' => 'application/vnd.digitsend.v1',
15
15
  'Authorization' => %Q[Token token="#{Config.api_token}"]
16
16
 
17
- response.body.empty? ? nil : JSON.parse(response.body)
17
+ hash = JSON.parse(response.body)
18
+
19
+ if response.code != "200"
20
+ raise exception_for_response(hash)
21
+ else
22
+ hash["data"]
23
+ end
18
24
  end
19
25
 
20
26
  def upload_s3_file(path, data)
@@ -57,6 +63,13 @@ module DigitSend
57
63
  data
58
64
  end
59
65
  end
66
+
67
+ def exception_for_response!(hash)
68
+ raise case hash["message"]
69
+ when "Missing phone numbers" then MissingPhoneNumbers.new(hash)
70
+ else DigitSend::Exception.new(hash)
71
+ end
72
+ end
60
73
  end
61
74
  end
62
75
  end
@@ -0,0 +1,10 @@
1
+ module DigitSend
2
+ class Exception < ::Exception
3
+ def initialize(hash)
4
+ super(hash["message"])
5
+ @data = hash["data"]
6
+ end
7
+
8
+ attr_reader :data
9
+ end
10
+ end
@@ -1,26 +1,46 @@
1
1
  module DigitSend
2
2
  class Message
3
3
  def initialize
4
+ @to = []
5
+ @cc = []
4
6
  @attachments = []
7
+ @phone_numbers = {}
5
8
  end
6
9
 
7
10
  def self.send(&block)
8
11
  new.tap { |m| yield(m) }.send
9
12
  end
10
13
 
11
- attr_accessor :to, :cc, :subject, :body
14
+ def to(email, phone = nil)
15
+ @to << email
16
+ @phone_numbers[email] = phone if phone
17
+ end
18
+
19
+ def cc(email, phone = nil)
20
+ @cc << email
21
+ @phone_numbers[email] = phone if phone
22
+ end
23
+
24
+ def subject(text)
25
+ @subject = text
26
+ end
27
+
28
+ def body(text)
29
+ @body = text
30
+ end
12
31
 
13
- def add_file(filename, data = nil)
32
+ def attach(filename, data = nil)
14
33
  @attachments << [ filename, data ]
15
34
  end
16
35
 
17
36
  def send
18
37
  Client.call '/messages', message: {
19
- to: to,
20
- cc: cc,
21
- subject: subject,
22
- body: body,
23
- s3_file_uuids: s3_file_uuids
38
+ to: @to.join(', '),
39
+ cc: @cc.join(', '),
40
+ subject: @subject,
41
+ body: @body,
42
+ s3_file_uuids: s3_file_uuids,
43
+ phone_numbers: @phone_numbers
24
44
  }
25
45
  end
26
46
 
@@ -0,0 +1,7 @@
1
+ module DigitSend
2
+ class MissingPhoneNumbers < DigitSend::Exception
3
+ def email_addresses
4
+ data["email_addresses"]
5
+ end
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module DigitSend
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.8"
3
3
  end
data/lib/digitsend.rb CHANGED
@@ -3,3 +3,5 @@ require 'digitsend/config'
3
3
  require 'digitsend/client'
4
4
  require 'digitsend/message'
5
5
  require 'digitsend/repository'
6
+ require 'digitsend/exception'
7
+ require 'digitsend/missing_phone_numbers'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: digitsend
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -27,7 +27,9 @@ files:
27
27
  - lib/digitsend.rb
28
28
  - lib/digitsend/client.rb
29
29
  - lib/digitsend/config.rb
30
+ - lib/digitsend/exception.rb
30
31
  - lib/digitsend/message.rb
32
+ - lib/digitsend/missing_phone_numbers.rb
31
33
  - lib/digitsend/repository.rb
32
34
  - lib/digitsend/version.rb
33
35
  homepage: ''