mime_builder 0.0.3 → 0.0.4

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: 4433b3e98a0fa4631cc3a6b2193a2c5e26687a20
4
- data.tar.gz: 79d171be6bbf4bf3c4350230a2360d5692fb5371
3
+ metadata.gz: d8de3fc319dce616c0b945f1aa9f986618c6f363
4
+ data.tar.gz: 1c1f37d3d4190ef49f741b8a6b9fa4348dcfa3d1
5
5
  SHA512:
6
- metadata.gz: f4e135cd2153477e464ee05b307c7b728938b3a6cff006904d5d77437d5ba2d385ca682a6245268cff521beeef244c3b0593fd83204dbd9b167a93d8e2cea069
7
- data.tar.gz: 7bee0cfe96986ef80484be8bbb552c537ead6aedeb6018b12bd96bad6ce1d773d15493675154a794acafaa688ba74f3c491fa3016858b798bdf0a05a203c57a2
6
+ metadata.gz: 885b5fb3c26a687f828b3257885c18c22225331df3c251c30274172dfe7a4c5adfa3e7e5df43bdaacc642c42d18207e928fa5b53a1b015332577527fd4d1054d
7
+ data.tar.gz: 9ca88ad183f4fad5e3cee88567fe4d6d78f6b67ece89326780c54d43df54c1c5ece04a4093f86236e0b9115af6638749c6b75f07dbf684474d9b0e36c994addd
@@ -1,5 +1,7 @@
1
1
  CHANGELOG
2
2
  ---------
3
+ - **2015-12-14**: 0.0.4
4
+ - Add JSON support
3
5
  - **2015-05-30**: 0.0.3
4
6
  - Add Text support for arbitrary content types
5
7
  - Add Text support for content disposition
data/README.md CHANGED
@@ -13,7 +13,7 @@ MIMEBuilder Ruby
13
13
 
14
14
  ## Overview
15
15
 
16
- This library creates `mime` parts from limited information.
16
+ This library creates MIME parts from limited information. The MIME parts are based on the Ruby `mime` library and are all sub-classes of `MIME::Media`.
17
17
 
18
18
  ## Installation
19
19
 
@@ -54,7 +54,7 @@ mime_part = builder.mime
54
54
  Options:
55
55
 
56
56
  ```ruby
57
- builder = MIMEBuilder::Filepath(
57
+ builder = MIMEBuilder::Filepath.new(
58
58
  '/path/to/file',
59
59
  base64_encode: true, # base64 encode part
60
60
  content_id_disable: true, # remove auto-generated Content-Id header
@@ -65,7 +65,10 @@ builder = MIMEBuilder::Filepath(
65
65
 
66
66
  ### From String
67
67
 
68
- Builds a `MIME::Text` object.
68
+ Builds a `MIME::Text` object. This accepts a string and can optionally populate the following headers:
69
+
70
+ 1. `Content-Disposition`
71
+ 2. `Content-Type`
69
72
 
70
73
  This will optionally delete the following auto-generated header:
71
74
 
@@ -79,13 +82,37 @@ mime_part = builder.mime
79
82
  Options:
80
83
 
81
84
  ```ruby
82
- builder = MIMEBuilder::Text(
85
+ builder = MIMEBuilder::Text.new(
83
86
  'Hi there!',
84
87
  content_id_disable: true, # remove auto-generated Content-Id header
85
88
  content_type: 'text/html' # override auto-generated Content-Type
86
89
  )
87
90
  ```
88
91
 
92
+ ### From JSON
93
+
94
+ Builds a `MIME::Text` object. This accepts a string, hash, or array.
95
+
96
+ This will optionally:
97
+
98
+ 1. delete the following auto-generated `Content-Id`
99
+ 1. base64 encode the content
100
+
101
+ ```ruby
102
+ builder = MIMEBuilder::JSON.new { foo: 'bar' }
103
+ mime_part = builder.mime
104
+ ```
105
+
106
+ Options:
107
+
108
+ ```ruby
109
+ builder = MIMEBuilder::JSON.new(
110
+ 'Hi there!',
111
+ content_id_disable: true, # remove auto-generated Content-Id header
112
+ base64_encode: false # disable default base64 encoding
113
+ )
114
+ ```
115
+
89
116
  ## Change Log
90
117
 
91
118
  See [CHANGELOG.md](CHANGELOG.md)
@@ -96,6 +123,11 @@ Project Repo
96
123
 
97
124
  * https://github.com/grokify/mime-builder-ruby
98
125
 
126
+ MIME Library
127
+
128
+ * https://rubygems.org/gems/mime
129
+ * https://bitbucket.org/pachl/mime/src
130
+
99
131
  ## Contributions
100
132
 
101
133
  Any reports of problems, comments or suggestions are most welcome.
@@ -1,6 +1,7 @@
1
1
  module MIMEBuilder
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
 
4
4
  autoload :Filepath, 'mime_builder/file'
5
+ autoload :JSON, 'mime_builder/json'
5
6
  autoload :Text, 'mime_builder/text'
6
7
  end
@@ -65,10 +65,9 @@ module MIMEBuilder
65
65
 
66
66
  def get_attachment_content_disposition(filepath = nil)
67
67
  filename = File.basename(filepath.to_s)
68
- if filename.is_a?(String) && filename.length > 0
69
- return "attachment; filename=\"#{filename}\""
70
- end
71
- 'attachment'
68
+ return filename.to_s.length > 0 \
69
+ ? "attachment; filename=\"#{filename}\"" \
70
+ : 'attachment'
72
71
  end
73
72
  end
74
73
  end
@@ -0,0 +1,50 @@
1
+ require 'base64'
2
+ require 'mime'
3
+ require 'multi_json'
4
+
5
+ module MIMEBuilder
6
+ class JSON
7
+ BASE64_DEFAULT = true
8
+ CONTENTID_DEFAULT = false
9
+
10
+ attr_accessor :mime
11
+ attr_accessor :json
12
+
13
+ def initialize(content, opts = {})
14
+ @opts = opts
15
+
16
+ @json = content_to_json content
17
+
18
+ @json = Base64.encode64(@json) if encode_base64?
19
+ @mime = build_json_part @json
20
+ end
21
+
22
+ def content_to_json(content)
23
+ content = MultiJson.encode(content) if content.is_a?(Array) || content.is_a?(Hash)
24
+ content = content.to_s unless content.is_a? String
25
+ content
26
+ end
27
+
28
+ def build_json_part(json)
29
+ json_part = MIME::Text.new(json)
30
+ json_part.headers.delete('Content-Id') if disable_content_id?
31
+ json_part.headers.set('Content-Type', 'application/json')
32
+ json_part.headers.set('Content-Transfer-Encoding', 'base64') if encode_base64?
33
+ return json_part
34
+ end
35
+
36
+ def encode_base64?
37
+ if @opts.key? :encode_base64
38
+ return @opts[:encode_base64] ? true : false
39
+ end
40
+ return BASE64_DEFAULT
41
+ end
42
+
43
+ def disable_content_id?
44
+ if @opts.key? :content_id_disable
45
+ return @opts[:content_id_disable] ? true : false
46
+ end
47
+ return !CONTENTID_DEFAULT
48
+ end
49
+ end
50
+ end
@@ -33,7 +33,7 @@ module MIMEBuilder
33
33
  end
34
34
 
35
35
  def get_attachment_content_disposition(filename = nil)
36
- cd = filename.to_s.length > 0 \
36
+ return filename.to_s.length > 0 \
37
37
  ? "attachment; filename=\"#{filename}\"" \
38
38
  : 'attachment'
39
39
  end
@@ -21,8 +21,8 @@ class MIMEBuilderFileTest < Test::Unit::TestCase
21
21
  end
22
22
 
23
23
  def test_base64
24
- filepath = './test/test_file.pdf'
25
24
  filename = 'test_file.pdf'
25
+ filepath = File.join './test', filename
26
26
 
27
27
  filepart = MIMEBuilder::Filepath.new(filepath, {:base64_encode => true})
28
28
 
@@ -47,8 +47,8 @@ class MIMEBuilderFileTest < Test::Unit::TestCase
47
47
  end
48
48
 
49
49
  def test_attachment
50
- filepath = './test/test_file.pdf'
51
50
  filename = 'test_file.pdf'
51
+ filepath = File.join './test', filename
52
52
 
53
53
  filepart = MIMEBuilder::Filepath.new(filepath, {:is_attachment => true})
54
54
 
@@ -59,7 +59,7 @@ class MIMEBuilderFileTest < Test::Unit::TestCase
59
59
  filepath = './test/test_file_nonexistent.pdf'
60
60
 
61
61
  assert_raise do
62
- filepart = MIMEBuilder::Filepath.new(filepath)
62
+ MIMEBuilder::Filepath.new(filepath)
63
63
  end
64
64
  end
65
65
  end
@@ -0,0 +1,24 @@
1
+ require './test/test_base.rb'
2
+
3
+ require 'base64'
4
+
5
+ class MIMEBuilderJsonTest < Test::Unit::TestCase
6
+ def test_base
7
+ test_json = { hello: 'world' }
8
+
9
+ json_part = MIMEBuilder::JSON.new(test_json)
10
+
11
+ assert_equal 'application/json', json_part.mime.headers.get('Content-Type')
12
+ assert_equal Base64.encode64(MultiJson.encode(test_json)), json_part.mime.body.to_s
13
+ assert_equal nil, json_part.mime.headers.get('Content-Id')
14
+ end
15
+
16
+ def test_no_base64
17
+ test_json = { hello: 'world' }
18
+ test_json = MultiJson.encode(test_json)
19
+
20
+ json_part = MIMEBuilder::JSON.new test_json, encode_base64: false
21
+
22
+ assert_equal test_json, json_part.mime.body.to_s
23
+ end
24
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mime_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Wang
@@ -14,30 +14,56 @@ dependencies:
14
14
  name: mime
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.4'
17
20
  - - ">="
18
21
  - !ruby/object:Gem::Version
19
- version: '0'
22
+ version: 0.4.3
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.4'
24
30
  - - ">="
25
31
  - !ruby/object:Gem::Version
26
- version: '0'
32
+ version: 0.4.3
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: mime-types
29
35
  requirement: !ruby/object:Gem::Requirement
30
36
  requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.1'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.1'
47
+ - !ruby/object:Gem::Dependency
48
+ name: multi_json
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1'
31
54
  - - ">="
32
55
  - !ruby/object:Gem::Version
33
- version: '1.25'
56
+ version: 1.12.1
34
57
  type: :runtime
35
58
  prerelease: false
36
59
  version_requirements: !ruby/object:Gem::Requirement
37
60
  requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '1'
38
64
  - - ">="
39
65
  - !ruby/object:Gem::Version
40
- version: '1.25'
66
+ version: 1.12.1
41
67
  description: Helper library to build MIME parts
42
68
  email: johncwang@gmail.com
43
69
  executables: []
@@ -46,16 +72,17 @@ extra_rdoc_files: []
46
72
  files:
47
73
  - CHANGELOG.md
48
74
  - Gemfile
49
- - Gemfile.lock
50
75
  - LICENSE.txt
51
76
  - README.md
52
77
  - Rakefile
53
78
  - lib/mime_builder.rb
54
79
  - lib/mime_builder/file.rb
80
+ - lib/mime_builder/json.rb
55
81
  - lib/mime_builder/text.rb
56
82
  - test/test_base.rb
57
83
  - test/test_file.pdf
58
84
  - test/test_file.rb
85
+ - test/test_json.rb
59
86
  - test/test_text.rb
60
87
  homepage: https://github.com/grokify/
61
88
  licenses:
@@ -77,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
104
  version: '0'
78
105
  requirements: []
79
106
  rubyforge_project:
80
- rubygems_version: 2.5.1
107
+ rubygems_version: 2.6.7
81
108
  signing_key:
82
109
  specification_version: 4
83
110
  summary: MIME Builder is a helper to build MIME parts
@@ -1,53 +0,0 @@
1
- GEM
2
- remote: https://rubygems.org/
3
- specs:
4
- coveralls (0.8.1)
5
- json (~> 1.8)
6
- rest-client (>= 1.6.8, < 2)
7
- simplecov (~> 0.10.0)
8
- term-ansicolor (~> 1.3)
9
- thor (~> 0.19.1)
10
- docile (1.1.5)
11
- domain_name (0.5.24)
12
- unf (>= 0.0.5, < 1.0.0)
13
- http-cookie (1.0.2)
14
- domain_name (~> 0.5)
15
- json (1.8.3)
16
- metaclass (0.0.4)
17
- mime (0.4.2)
18
- mime-types (2.6.2)
19
- mocha (1.1.0)
20
- metaclass (~> 0.0.1)
21
- netrc (0.10.3)
22
- power_assert (0.2.4)
23
- rake (10.4.2)
24
- rest-client (1.8.0)
25
- http-cookie (>= 1.0.2, < 2.0)
26
- mime-types (>= 1.16, < 3.0)
27
- netrc (~> 0.7)
28
- simplecov (0.10.0)
29
- docile (~> 1.1.0)
30
- json (~> 1.8)
31
- simplecov-html (~> 0.10.0)
32
- simplecov-html (0.10.0)
33
- term-ansicolor (1.3.0)
34
- tins (~> 1.0)
35
- test-unit (3.1.4)
36
- power_assert
37
- thor (0.19.1)
38
- tins (1.5.2)
39
- unf (0.1.4)
40
- unf_ext
41
- unf_ext (0.0.7.1)
42
-
43
- PLATFORMS
44
- ruby
45
-
46
- DEPENDENCIES
47
- coveralls
48
- mime
49
- mime-types
50
- mocha
51
- rake
52
- simplecov
53
- test-unit