send_with_us 1.6.0 → 1.7.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d6f051528f2e3214a82b3447fc0d3965dd614848
4
+ data.tar.gz: f743aa1c6a72b8bddb178722c0d55978700283dd
5
+ SHA512:
6
+ metadata.gz: 493cfbea63a120844f4f3bfa6eb72653a618b20bae54e8e71a7f13a8393249db77dc62812fa733240024fd08a8d4510c73b617da6b360e8caa3182d64772d3a4
7
+ data.tar.gz: 068ec297df720fe877cd2b71090f62ac6e5199e7c61baee8f4e5d8f72c458243520428930096eef364f7affc32ebf174d09ba6b406d081426ad1c307636638ab
data/README.md CHANGED
@@ -30,11 +30,12 @@ bundle install
30
30
  - **from** - *hash* - From name/address/reply\_to
31
31
  - **cc** - *array* - array of CC addresses
32
32
  - **bcc** - *array* - array of BCC addresses
33
- - **files** - *array* - array of files to attach
33
+ - **files** - *array* - array of files to attach, as strings or hashes (see below)
34
34
  - **esp\_account** - *string* - ESP account used to send email
35
35
  - **version\_name** - *string* - version of template to send
36
36
  - **headers** - *hash* - custom email headers **NOTE** only supported by some ESPs
37
37
 
38
+
38
39
  For any Ruby project:
39
40
  ```ruby
40
41
  require 'rubygems'
@@ -54,9 +55,12 @@ begin
54
55
  'template_id',
55
56
  { name: 'Matt', address: 'recipient@example.com' },
56
57
  { company_name: 'TestCo' },
57
- { name: 'Company',
58
- address: 'company@example.com',
59
- reply_to: 'info@example.com' },
58
+ {
59
+ name: 'Company',
60
+ address: 'company@example.com',
61
+ reply_to: 'info@example.com'
62
+ },
63
+ ['path/to/attachment.txt'],
60
64
  'esp_MYESPACCOUNT',
61
65
  'v2') # version name
62
66
  puts result
@@ -91,11 +95,17 @@ begin
91
95
  reply_to: 'info@example.com' },
92
96
  [],
93
97
  [],
94
- ['path/to/file.txt'])
98
+ [
99
+ 'path/to/file.txt',
100
+ { filename: 'customfilename.txt', attachment: 'path/to/file.txt' },
101
+ { filename: 'anotherfile.txt', attachment: File.open('path/to/file.txt') },
102
+ { filename: 'unpersistedattachment.txt', attachment: StringIO.new("raw data") }
103
+ ]
104
+ )
95
105
  puts result
96
106
 
97
107
  # Set ESP account
98
- # See: https://help.sendwithus.com/support/solutions/articles/1000088976-set-up-and-use-multiple
108
+ # See: https://help.sendwithus.com/support/solurtions/articles/1000088976-set-up-and-use-multiple
99
109
  result = obj.send_with(
100
110
  'template_id',
101
111
  { name: 'Matt', address: 'recipient@example.com' },
@@ -117,7 +127,7 @@ end
117
127
 
118
128
  - **email\_id** - *string* - Template ID being rendered
119
129
  - **version\_id** - *string* - Version ID to render (optional)
120
- - **data** - *hash* - Email data to render the template with
130
+ - **data** - *hash* - Email data to render the template with (optional)
121
131
 
122
132
  ```ruby
123
133
  require 'rubygems'
data/lib/send_with_us.rb CHANGED
@@ -7,6 +7,7 @@ require 'net/https'
7
7
  require 'uri'
8
8
  require 'json'
9
9
 
10
+ require 'send_with_us/attachment'
10
11
  require 'send_with_us/api'
11
12
  require 'send_with_us/api_request'
12
13
  require 'send_with_us/config'
@@ -51,14 +51,17 @@ module SendWithUs
51
51
  payload[:headers] = headers
52
52
  end
53
53
 
54
- files.each do |path|
55
- file = open(path).read
56
- id = File.basename(path)
57
- data = Base64.encode64(file)
58
- if payload[:files].nil?
59
- payload[:files] = []
54
+ if files.any?
55
+ payload[:files] = []
56
+
57
+ files.each do |file_data|
58
+ if file_data.is_a?(String)
59
+ attachment = SendWithUs::Attachment.new(file_data)
60
+ else
61
+ attachment = SendWithUs::Attachment.new(file_data[:attachment], file_data[:filename])
62
+ end
63
+ payload[:files] << { id: attachment.filename, data: attachment.encoded_data }
60
64
  end
61
- payload[:files] << {id: id, data: data}
62
65
  end
63
66
 
64
67
  payload = payload.to_json
@@ -0,0 +1,16 @@
1
+ module SendWithUs
2
+ class Attachment
3
+ def initialize attachment, filename = nil
4
+ @attachment, @filename = attachment, filename
5
+ end
6
+
7
+ def filename
8
+ @filename ||= @attachment.is_a?(String) ? File.basename(@attachment) : nil
9
+ end
10
+
11
+ def encoded_data
12
+ file_data = @attachment.respond_to?(:read) ? @attachment : open(@attachment)
13
+ Base64.encode64(file_data.read)
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module SendWithUs
2
- VERSION = '1.6.0'
2
+ VERSION = '1.7.0'
3
3
  end
@@ -0,0 +1,71 @@
1
+ require_relative '../../test_helper'
2
+
3
+ class TestAttachment < MiniTest::Unit::TestCase
4
+ describe "#filename" do
5
+ describe "when a filename is explicitly declared" do
6
+ before do
7
+ @attachment = SendWithUs::Attachment.new(
8
+ StringIO.new("some data"),
9
+ "rawr.txt"
10
+ )
11
+ end
12
+
13
+ it "returns the explicit filename" do
14
+ assert_equal "rawr.txt", @attachment.filename
15
+ end
16
+ end
17
+
18
+ describe "when a filename is implied from a path" do
19
+ before do
20
+ @attachment = SendWithUs::Attachment.new(
21
+ "this/is/a/path.txt"
22
+ )
23
+ end
24
+
25
+ it "returns the basename of the path" do
26
+ assert_equal "path.txt", @attachment.filename
27
+ end
28
+ end
29
+
30
+ describe "when a filename is absent" do
31
+ before do
32
+ @attachment = SendWithUs::Attachment.new(
33
+ StringIO.new("")
34
+ )
35
+ end
36
+
37
+ it "returns nil" do
38
+ assert_nil @attachment.filename
39
+ end
40
+ end
41
+ end
42
+
43
+ describe "#encoded_data" do
44
+ describe "when the attachment is an IO object" do
45
+ before do
46
+ @attachment = SendWithUs::Attachment.new(
47
+ StringIO.new("test text")
48
+ )
49
+ end
50
+
51
+ it "returns the base 64'd content of the object" do
52
+ assert_equal Base64.encode64("test text"), @attachment.encoded_data
53
+ end
54
+ end
55
+
56
+ describe "when the attachment is not an IO object" do
57
+ before do
58
+ @attachment = SendWithUs::Attachment.new(
59
+ "path/to/file.txt"
60
+ )
61
+ @attachment.expects(:open).
62
+ with("path/to/file.txt").
63
+ returns(StringIO.new("test text"))
64
+ end
65
+
66
+ it "returns the base 64'd content of the file/url in question" do
67
+ assert_equal Base64.encode64("test text"), @attachment.encoded_data
68
+ end
69
+ end
70
+ end
71
+ end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: send_with_us
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.0
5
- prerelease:
4
+ version: 1.7.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Matt Harris
@@ -11,54 +10,48 @@ authors:
11
10
  autorequire:
12
11
  bindir: bin
13
12
  cert_chain: []
14
- date: 2014-12-17 00:00:00.000000000 Z
13
+ date: 2014-12-19 00:00:00.000000000 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: rake
18
17
  requirement: !ruby/object:Gem::Requirement
19
- none: false
20
18
  requirements:
21
- - - ! '>='
19
+ - - '>='
22
20
  - !ruby/object:Gem::Version
23
21
  version: '0'
24
22
  type: :development
25
23
  prerelease: false
26
24
  version_requirements: !ruby/object:Gem::Requirement
27
- none: false
28
25
  requirements:
29
- - - ! '>='
26
+ - - '>='
30
27
  - !ruby/object:Gem::Version
31
28
  version: '0'
32
29
  - !ruby/object:Gem::Dependency
33
30
  name: shoulda
34
31
  requirement: !ruby/object:Gem::Requirement
35
- none: false
36
32
  requirements:
37
- - - ! '>='
33
+ - - '>='
38
34
  - !ruby/object:Gem::Version
39
35
  version: '0'
40
36
  type: :development
41
37
  prerelease: false
42
38
  version_requirements: !ruby/object:Gem::Requirement
43
- none: false
44
39
  requirements:
45
- - - ! '>='
40
+ - - '>='
46
41
  - !ruby/object:Gem::Version
47
42
  version: '0'
48
43
  - !ruby/object:Gem::Dependency
49
44
  name: mocha
50
45
  requirement: !ruby/object:Gem::Requirement
51
- none: false
52
46
  requirements:
53
- - - ! '>='
47
+ - - '>='
54
48
  - !ruby/object:Gem::Version
55
49
  version: '0'
56
50
  type: :development
57
51
  prerelease: false
58
52
  version_requirements: !ruby/object:Gem::Requirement
59
- none: false
60
53
  requirements:
61
- - - ! '>='
54
+ - - '>='
62
55
  - !ruby/object:Gem::Version
63
56
  version: '0'
64
57
  description: SendWithUs.com Ruby Client
@@ -79,42 +72,44 @@ files:
79
72
  - lib/send_with_us.rb
80
73
  - lib/send_with_us/api.rb
81
74
  - lib/send_with_us/api_request.rb
75
+ - lib/send_with_us/attachment.rb
82
76
  - lib/send_with_us/config.rb
83
77
  - lib/send_with_us/version.rb
84
78
  - send_with_us.gemspec
85
79
  - test/lib/send_with_us/api_request_test.rb
86
80
  - test/lib/send_with_us/api_test.rb
81
+ - test/lib/send_with_us/attachment_test.rb
87
82
  - test/lib/send_with_us/config_test.rb
88
83
  - test/lib/send_with_us/version_test.rb
89
84
  - test/test_helper.rb
90
85
  homepage: https://github.com/sendwithus/sendwithus_ruby
91
86
  licenses:
92
87
  - Apache-2.0
88
+ metadata: {}
93
89
  post_install_message:
94
90
  rdoc_options: []
95
91
  require_paths:
96
92
  - lib
97
93
  required_ruby_version: !ruby/object:Gem::Requirement
98
- none: false
99
94
  requirements:
100
- - - ! '>='
95
+ - - '>='
101
96
  - !ruby/object:Gem::Version
102
97
  version: '0'
103
98
  required_rubygems_version: !ruby/object:Gem::Requirement
104
- none: false
105
99
  requirements:
106
- - - ! '>='
100
+ - - '>='
107
101
  - !ruby/object:Gem::Version
108
102
  version: '0'
109
103
  requirements: []
110
104
  rubyforge_project:
111
- rubygems_version: 1.8.23
105
+ rubygems_version: 2.0.14
112
106
  signing_key:
113
- specification_version: 3
107
+ specification_version: 4
114
108
  summary: SendWithUs.com Ruby Client
115
109
  test_files:
116
110
  - test/lib/send_with_us/api_request_test.rb
117
111
  - test/lib/send_with_us/api_test.rb
112
+ - test/lib/send_with_us/attachment_test.rb
118
113
  - test/lib/send_with_us/config_test.rb
119
114
  - test/lib/send_with_us/version_test.rb
120
115
  - test/test_helper.rb