mime_builder 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +53 -0
- data/LICENSE.txt +22 -0
- data/README.md +92 -0
- data/Rakefile +20 -0
- data/lib/mime_builder.rb +6 -0
- data/lib/mime_builder/file.rb +76 -0
- data/lib/mime_builder/text.rb +15 -0
- data/test/test_base.rb +6 -0
- data/test/test_file.pdf +0 -0
- data/test/test_file.rb +65 -0
- data/test/test_text.rb +19 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e1b242e017dbe6bc717e94d6ef217bab7a7ff6d2
|
4
|
+
data.tar.gz: fefd7653cf050420e71d3f1434376a8793a9759b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1f7e5c60b154659606764c9fb73cf333c194e0aec35ca931a37e292a16f4188b01907ad88e7efdeb6bf1b0088600150b0bb3cf947626804f6fda31d1c2fcf2ea
|
7
|
+
data.tar.gz: 1e9ff54c3742e05f4be0ddfa5ee9fa2efd9b6cc64b38f19c0d81ff513a4d1b009dd6015e3f17e9860ceaaaf4322a4040e8a1739870dd33ebe986b19ab4bc0319
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,53 @@
|
|
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
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 John Wang
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
MIMEBuilder Ruby
|
2
|
+
================
|
3
|
+
|
4
|
+
[![Gem Version][gem-version-svg]][gem-version-link]
|
5
|
+
[![Build Status][build-status-svg]][build-status-link]
|
6
|
+
[![Coverage Status][coverage-status-svg]][coverage-status-link]
|
7
|
+
[![Dependency Status][dependency-status-svg]][dependency-status-link]
|
8
|
+
[![Code Climate][codeclimate-status-svg]][codeclimate-status-link]
|
9
|
+
[![Scrutinizer Code Quality][scrutinizer-status-svg]][scrutinizer-status-link]
|
10
|
+
[![Downloads][downloads-svg]][downloads-link]
|
11
|
+
[![Docs][docs-rubydoc-svg]][docs-rubydoc-link]
|
12
|
+
[![License][license-svg]][license-link]
|
13
|
+
|
14
|
+
## Overview
|
15
|
+
|
16
|
+
This library creates `mime` parts from limited information.
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
### From Filepath
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
builder = MIMEBuilder::Filepath('/path/to/file')
|
24
|
+
mime_part = builder.mime
|
25
|
+
```
|
26
|
+
|
27
|
+
Options:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
builder = MIMEBuilder::Filepath(
|
31
|
+
'/path/to/file',
|
32
|
+
{
|
33
|
+
:base64_encode => true, # base64 encode part
|
34
|
+
:content_id_disable => true, # remove auto-generated Content-Id header
|
35
|
+
:content_type => 'text/plain', # override auto-generated Content-Type
|
36
|
+
:is_attachment => true # add 'attachment' disposition
|
37
|
+
}
|
38
|
+
)
|
39
|
+
```
|
40
|
+
|
41
|
+
### From String
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
builder = MIMEBuilder::Filepath('Hi there!')
|
45
|
+
mime_part = builder.mime
|
46
|
+
```
|
47
|
+
|
48
|
+
Options:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
builder = MIMEBuilder::Filepath(
|
52
|
+
'Hi there!',
|
53
|
+
{
|
54
|
+
:content_id_disable => true, # remove auto-generated Content-Id header
|
55
|
+
}
|
56
|
+
)
|
57
|
+
```
|
58
|
+
|
59
|
+
## Change Log
|
60
|
+
|
61
|
+
See [CHANGELOG.md](CHANGELOG.md)
|
62
|
+
|
63
|
+
## Contributions
|
64
|
+
|
65
|
+
Any reports of problems, comments or suggestions are most welcome.
|
66
|
+
|
67
|
+
Please report these on [Github](https://github.com/grokify/mime-builder-ruby)
|
68
|
+
|
69
|
+
## License
|
70
|
+
|
71
|
+
MIMEBuilder is available under an MIT-style license. See [LICENSE.txt](LICENSE.txt) for details.
|
72
|
+
|
73
|
+
MIMEBuilder © 2016 by John Wang
|
74
|
+
|
75
|
+
[gem-version-svg]: https://badge.fury.io/rb/mime_builder.svg
|
76
|
+
[gem-version-link]: http://badge.fury.io/rb/mime_builder
|
77
|
+
[downloads-svg]: http://ruby-gem-downloads-badge.herokuapp.com/mime_builder
|
78
|
+
[downloads-link]: https://rubygems.org/gems/mime_builder
|
79
|
+
[build-status-svg]: https://api.travis-ci.org/grokify/mime-builder-ruby.svg?branch=master
|
80
|
+
[build-status-link]: https://travis-ci.org/grokify/mime-builder-ruby
|
81
|
+
[coverage-status-svg]: https://coveralls.io/repos/grokify/mime-builder-ruby/badge.svg?branch=master
|
82
|
+
[coverage-status-link]: https://coveralls.io/r/grokify/mime-builder-ruby?branch=master
|
83
|
+
[dependency-status-svg]: https://gemnasium.com/grokify/mime-builder-ruby.svg
|
84
|
+
[dependency-status-link]: https://gemnasium.com/grokify/mime-builder-ruby
|
85
|
+
[codeclimate-status-svg]: https://codeclimate.com/github/grokify/mime-builder-ruby/badges/gpa.svg
|
86
|
+
[codeclimate-status-link]: https://codeclimate.com/github/grokify/mime-builder-ruby
|
87
|
+
[scrutinizer-status-svg]: https://scrutinizer-ci.com/g/grokify/mime-builder-ruby/badges/quality-score.png?b=master
|
88
|
+
[scrutinizer-status-link]: https://scrutinizer-ci.com/g/grokify/mime-builder-ruby/?branch=master
|
89
|
+
[docs-rubydoc-svg]: https://img.shields.io/badge/docs-rubydoc-blue.svg
|
90
|
+
[docs-rubydoc-link]: http://www.rubydoc.info/gems/mime_builder/
|
91
|
+
[license-svg]: https://img.shields.io/badge/license-MIT-blue.svg
|
92
|
+
[license-link]: https://github.com/grokify/mime-builder-ruby/blob/master/LICENSE.txt
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
desc 'Default: run unit tests.'
|
5
|
+
task :default => :test
|
6
|
+
|
7
|
+
desc 'Test the library.'
|
8
|
+
Rake::TestTask.new do |t|
|
9
|
+
t.libs << 'lib'
|
10
|
+
t.pattern = 'test/**/test_*.rb'
|
11
|
+
t.verbose = false
|
12
|
+
end
|
13
|
+
|
14
|
+
desc 'Generate YARD documentation.'
|
15
|
+
task :gendoc do
|
16
|
+
# puts 'yard doc generation disabled until JRuby build native
|
17
|
+
# extensions for redcarpet or yard removes the dependency.'
|
18
|
+
system 'yardoc'
|
19
|
+
system 'yard stats --list-undoc'
|
20
|
+
end
|
data/lib/mime_builder.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'mime'
|
3
|
+
require 'mime/types'
|
4
|
+
|
5
|
+
module MIMEBuilder
|
6
|
+
class Filepath
|
7
|
+
attr_accessor :mime
|
8
|
+
attr_accessor :filepath
|
9
|
+
|
10
|
+
# {:base64_encode bool, :content_type string, :content_id_disable bool}
|
11
|
+
def initialize(filepath, opts = {})
|
12
|
+
if opts.key?(:base64_encode) && opts[:base64_encode]
|
13
|
+
@base64_encode = true
|
14
|
+
else
|
15
|
+
@base64_encode = false
|
16
|
+
end
|
17
|
+
|
18
|
+
@mime = create_mime(filepath, opts[:content_id_disable])
|
19
|
+
|
20
|
+
@mime.headers.set(
|
21
|
+
'Content-Type',
|
22
|
+
get_file_content_type(filepath, opts[:content_type]))
|
23
|
+
|
24
|
+
set_attachment_content_disposition(filepath, opts[:is_attachment])
|
25
|
+
end
|
26
|
+
|
27
|
+
def create_mime(filepath, content_id_disable)
|
28
|
+
bytes = read_file_bytes(filepath)
|
29
|
+
|
30
|
+
mime = nil
|
31
|
+
|
32
|
+
if @base64_encode
|
33
|
+
mime = MIME::Text.new(Base64.encode64(bytes))
|
34
|
+
mime.headers.set('Content-Transfer-Encoding', 'base64')
|
35
|
+
else
|
36
|
+
mime = MIME::Application.new(bytes)
|
37
|
+
end
|
38
|
+
|
39
|
+
mime.headers.delete('Content-Id') if content_id_disable
|
40
|
+
|
41
|
+
mime
|
42
|
+
end
|
43
|
+
|
44
|
+
def read_file_bytes(filepath = nil)
|
45
|
+
unless File.file?(filepath)
|
46
|
+
fail "File \"#{filepath}\" does not exist or cannot be read"
|
47
|
+
end
|
48
|
+
|
49
|
+
#File.open(filepath, 'rb:BINARY') { |f| f.read }
|
50
|
+
File.read(filepath)
|
51
|
+
end
|
52
|
+
|
53
|
+
def get_file_content_type(filepath = nil, content_type = nil)
|
54
|
+
if content_type.is_a?(String) && content_type =~ %r{^[^/\s]+/[^/\s]+}
|
55
|
+
return content_type
|
56
|
+
end
|
57
|
+
MIME::Types.type_for(filepath).first.content_type \
|
58
|
+
|| 'application/octet-stream'
|
59
|
+
end
|
60
|
+
|
61
|
+
def set_attachment_content_disposition(filepath, is_attachment)
|
62
|
+
@mime.headers.set(
|
63
|
+
'Content-Disposition',
|
64
|
+
get_attachment_content_disposition(filepath)
|
65
|
+
) if is_attachment
|
66
|
+
end
|
67
|
+
|
68
|
+
def get_attachment_content_disposition(filepath = nil)
|
69
|
+
filename = File.basename(filepath.to_s)
|
70
|
+
if filename.is_a?(String) && filename.length > 0
|
71
|
+
return "attachment; filename=\"#{filename}\""
|
72
|
+
end
|
73
|
+
'attachment'
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'mime'
|
2
|
+
|
3
|
+
module MIMEBuilder
|
4
|
+
class Text
|
5
|
+
attr_accessor :mime
|
6
|
+
attr_accessor :text
|
7
|
+
|
8
|
+
def initialize(text, opts = {})
|
9
|
+
@text = text
|
10
|
+
@mime = MIME::Text.new(text, 'plain')
|
11
|
+
@mime.headers.delete('Content-Id') \
|
12
|
+
if opts.key?(:content_id_disable) && opts[:content_id_disable]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/test/test_base.rb
ADDED
data/test/test_file.pdf
ADDED
Binary file
|
data/test/test_file.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require './test/test_base.rb'
|
2
|
+
|
3
|
+
class MIMEBuilderFileTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_file_one
|
6
|
+
#assert_equal "bXlfYXBwX2tleTpteV9hcHBfc2VjcmV0", @rcsdk.send(:get_api_key)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_basic
|
10
|
+
filepath = './test/test_file.pdf'
|
11
|
+
filename = 'test_file.pdf'
|
12
|
+
|
13
|
+
filepart = MIMEBuilder::Filepath.new(filepath)
|
14
|
+
|
15
|
+
assert_equal 'application/pdf', filepart.get_file_content_type(filepath)
|
16
|
+
assert_equal 'application/pdf', filepart.mime.headers.get('Content-Type')
|
17
|
+
|
18
|
+
assert_equal 'attachment', filepart.get_attachment_content_disposition()
|
19
|
+
assert_equal 'attachment; filename="test_file.pdf"', filepart.get_attachment_content_disposition(filename)
|
20
|
+
assert_equal 'attachment; filename="test_file.pdf"', filepart.get_attachment_content_disposition(filepath)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_base64
|
24
|
+
filepath = './test/test_file.pdf'
|
25
|
+
filename = 'test_file.pdf'
|
26
|
+
|
27
|
+
filepart = MIMEBuilder::Filepath.new(filepath, {:base64_encode => true})
|
28
|
+
|
29
|
+
assert_equal 'application/pdf', filepart.get_file_content_type(filepath)
|
30
|
+
assert_equal 'application/pdf', filepart.mime.headers.get('Content-Type')
|
31
|
+
|
32
|
+
assert_equal 'base64', filepart.mime.headers.get('Content-Transfer-Encoding')
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_content_type
|
36
|
+
filepath = './test/test_file.pdf'
|
37
|
+
filename = 'test_file.pdf'
|
38
|
+
|
39
|
+
filepart = MIMEBuilder::Filepath.new(filepath, {:content_type => 'application/pdf'})
|
40
|
+
|
41
|
+
assert_equal 'application/pdf', filepart.get_file_content_type(filepath)
|
42
|
+
assert_equal 'application/pdf', filepart.mime.headers.get('Content-Type')
|
43
|
+
|
44
|
+
assert_equal 'attachment', filepart.get_attachment_content_disposition()
|
45
|
+
assert_equal 'attachment; filename="test_file.pdf"', filepart.get_attachment_content_disposition(filename)
|
46
|
+
assert_equal 'attachment; filename="test_file.pdf"', filepart.get_attachment_content_disposition(filepath)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_attachment
|
50
|
+
filepath = './test/test_file.pdf'
|
51
|
+
filename = 'test_file.pdf'
|
52
|
+
|
53
|
+
filepart = MIMEBuilder::Filepath.new(filepath, {:is_attachment => true})
|
54
|
+
|
55
|
+
assert_equal 'attachment; filename="test_file.pdf"', filepart.mime.headers.get('Content-Disposition')
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_nonexistent_file
|
59
|
+
filepath = './test/test_file_nonexistent.pdf'
|
60
|
+
|
61
|
+
assert_raise do
|
62
|
+
filepart = MIMEBuilder::Filepath.new(filepath)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/test/test_text.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require './test/test_base.rb'
|
2
|
+
|
3
|
+
class MIMEBuilderTextTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_file_one
|
6
|
+
#assert_equal "bXlfYXBwX2tleTpteV9hcHBfc2VjcmV0", @rcsdk.send(:get_api_key)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_contenttype
|
10
|
+
test_text = 'Hello world!'
|
11
|
+
|
12
|
+
filepart = MIMEBuilder::Text.new(test_text)
|
13
|
+
|
14
|
+
assert_equal 'text/plain', filepart.mime.headers.get('Content-Type')
|
15
|
+
|
16
|
+
assert_equal test_text, filepart.mime.body.to_s
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mime_builder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Wang
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mime
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mime-types
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.25'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.25'
|
41
|
+
description: Helper library to build MIME parts
|
42
|
+
email: johncwang@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- lib/mime_builder/file.rb
|
48
|
+
- lib/mime_builder/text.rb
|
49
|
+
- lib/mime_builder.rb
|
50
|
+
- CHANGELOG.md
|
51
|
+
- Gemfile
|
52
|
+
- Gemfile.lock
|
53
|
+
- LICENSE.txt
|
54
|
+
- Rakefile
|
55
|
+
- README.md
|
56
|
+
- test/test_base.rb
|
57
|
+
- test/test_file.pdf
|
58
|
+
- test/test_file.rb
|
59
|
+
- test/test_text.rb
|
60
|
+
homepage: https://github.com/grokify/
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.1.5
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: MIME Builder is a helper to build MIME parts
|
84
|
+
test_files: []
|