sendgrid-parse 0.1.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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +47 -0
- data/Rakefile +2 -0
- data/lib/sendgrid-parse.rb +2 -0
- data/lib/sendgrid-parse/encodable_hash.rb +61 -0
- data/lib/sendgrid-parse/version.rb +5 -0
- data/sendgrid-parse.gemspec +20 -0
- data/spec/sendgrid-parse_spec.rb +114 -0
- metadata +121 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Rob Forman
|
2
|
+
|
3
|
+
MIT License
|
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,47 @@
|
|
1
|
+
# Sendgrid::Parse
|
2
|
+
|
3
|
+
Gem to dynamically set or change the encoding type for fields, ie params from SendGrid Parse API (http://docs.sendgrid.com/documentation/api/parse-api-2/)
|
4
|
+
|
5
|
+
Requires JSON for parsing 'charsets' envelope.
|
6
|
+
|
7
|
+
Works on 1.8.x via Iconv, and 1.9.x via built-in encoding support.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'sendgrid-parse'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install sendgrid-parse
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'sendgrid-parse'
|
27
|
+
|
28
|
+
# Example from SendGrid
|
29
|
+
params = {
|
30
|
+
:charsets => '{"text":"windows-1252"}',
|
31
|
+
:text => "Hello Euro \x80"
|
32
|
+
}
|
33
|
+
|
34
|
+
new_params = Sendgrid::Parse::EncodableHash.new(params)
|
35
|
+
new_params.encode!('UTF-8')
|
36
|
+
|
37
|
+
new_params[:text] # => "Hello Euro €"
|
38
|
+
new_params[:text].encoding # => #<Encoding:UTF-8>
|
39
|
+
```
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
1. Fork it
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'delegate'
|
3
|
+
require 'iconv' unless RUBY_VERSION >= '1.9'
|
4
|
+
|
5
|
+
|
6
|
+
module Sendgrid
|
7
|
+
module Parse
|
8
|
+
class EncodableHash < SimpleDelegator
|
9
|
+
attr_accessor :component
|
10
|
+
|
11
|
+
def class
|
12
|
+
__getobj__.class
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(component)
|
16
|
+
raise 'Missing required key :charsets for encoding.' unless component.has_key?(:charsets)
|
17
|
+
super
|
18
|
+
end
|
19
|
+
|
20
|
+
def encode(to_encoding, ignore=[])
|
21
|
+
component = __getobj__.dup
|
22
|
+
ignore.each { |e| component.delete(e) if component.has_key?(e)}
|
23
|
+
|
24
|
+
# Parse and symbolize charsets dictionary
|
25
|
+
charsets = JSON.parse(component[:charsets])
|
26
|
+
charsets.keys.each { |key| charsets[(key.to_sym rescue key) || key] = charsets.delete(key) }
|
27
|
+
|
28
|
+
# Set and/or change all fields according to charsets
|
29
|
+
component.each do |key, value|
|
30
|
+
if charsets.has_key?(key)
|
31
|
+
from_encoding = charsets[key]
|
32
|
+
component[key] = _encode(component[key], from_encoding, to_encoding)
|
33
|
+
charsets[key] = to_encoding
|
34
|
+
else
|
35
|
+
# If we weren't told, set it to the target encoding type.
|
36
|
+
component[key] = _encode(component[key], to_encoding, to_encoding)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
component[:charsets] = charsets.to_json
|
41
|
+
component
|
42
|
+
end
|
43
|
+
|
44
|
+
def encode!(to_encoding, ignore=[])
|
45
|
+
__setobj__ encode(to_encoding, ignore)
|
46
|
+
end
|
47
|
+
|
48
|
+
protected
|
49
|
+
def _encode(value, from, to)
|
50
|
+
if RUBY_VERSION >= '1.9'
|
51
|
+
value = value.force_encoding(from)
|
52
|
+
value = value.encode(to, :invalid => :replace, :undef => :replace, :replace => '')
|
53
|
+
else
|
54
|
+
value = Iconv.conv("#{to}//IGNORE", from, value)
|
55
|
+
end
|
56
|
+
|
57
|
+
value
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/sendgrid-parse/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Rob Forman"]
|
6
|
+
gem.email = ["rob@robforman.com"]
|
7
|
+
gem.description = %q{Library to dynamically set or change the encoding type for fields, ie params from SendGrid Parse API.}
|
8
|
+
gem.summary = %q{Library to dynamically set or change the encoding type for fields, ie params from SendGrid Parse API.}
|
9
|
+
gem.homepage = "https://github.com/robforman/sendgrid-parse"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "sendgrid-parse"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Sendgrid::Parse::VERSION
|
17
|
+
gem.add_dependency "json", "~> 1.7.3"
|
18
|
+
gem.add_dependency "iconv", "~> 0.1" unless RUBY_VERSION >= '1.9'
|
19
|
+
gem.add_development_dependency "rspec", "~> 2.10.0"
|
20
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'sendgrid-parse'
|
3
|
+
|
4
|
+
describe Sendgrid::Parse::EncodableHash do
|
5
|
+
test_string_1 = "Hello €"
|
6
|
+
test_string_1_windows1252_encoded = "Hello \x80"
|
7
|
+
test_string_1_utf8_encoded = "Hello \xE2\x82\xAC"
|
8
|
+
|
9
|
+
it "windows-1252 should encode to utf-8" do
|
10
|
+
params = {
|
11
|
+
:charsets => '{"text":"WINDOWS-1252"}',
|
12
|
+
:text => test_string_1_windows1252_encoded
|
13
|
+
}
|
14
|
+
|
15
|
+
new_params = Sendgrid::Parse::EncodableHash.new(params).encode("UTF-8")
|
16
|
+
new_params[:text].should eql(test_string_1)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "utf-8 should encode to utf-8" do
|
20
|
+
params = {
|
21
|
+
:charsets => '{"text":"UTF-8"}',
|
22
|
+
:text => test_string_1_utf8_encoded
|
23
|
+
}
|
24
|
+
|
25
|
+
new_params = Sendgrid::Parse::EncodableHash.new(params).encode("UTF-8")
|
26
|
+
new_params[:text].should eql(test_string_1)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should not change same encoding strings" do
|
30
|
+
params = {
|
31
|
+
:charsets => '{"text":"UTF-8"}',
|
32
|
+
:text => test_string_1
|
33
|
+
}
|
34
|
+
|
35
|
+
new_params = Sendgrid::Parse::EncodableHash.new(params).encode("UTF-8")
|
36
|
+
new_params[:text].should eql(test_string_1)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should strip unconvertible characters" do
|
40
|
+
params = {
|
41
|
+
:charsets => '{"text":"UTF-8"}',
|
42
|
+
:text => "Résumé"
|
43
|
+
}
|
44
|
+
|
45
|
+
new_params = Sendgrid::Parse::EncodableHash.new(params).encode("ASCII")
|
46
|
+
new_params[:text].should_not eql("Résumé")
|
47
|
+
new_params[:text].should eql("Rsum")
|
48
|
+
end
|
49
|
+
|
50
|
+
its "encode should be non-destructive" do
|
51
|
+
params = {
|
52
|
+
:charsets => '{"text":"UTF-8"}',
|
53
|
+
:text => "Résumé"
|
54
|
+
}
|
55
|
+
|
56
|
+
new_params = Sendgrid::Parse::EncodableHash.new(params).encode("ASCII")
|
57
|
+
new_params[:text].should eql("Rsum")
|
58
|
+
|
59
|
+
params[:text].should eql("Résumé")
|
60
|
+
end
|
61
|
+
|
62
|
+
its "encode! should be destructive" do
|
63
|
+
params = {
|
64
|
+
:charsets => '{"text":"UTF-8"}',
|
65
|
+
:text => "Résumé"
|
66
|
+
}
|
67
|
+
|
68
|
+
new_params = Sendgrid::Parse::EncodableHash.new(params)
|
69
|
+
new_params.encode!("ASCII")
|
70
|
+
new_params[:text].should eql("Rsum")
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should re-write the charsets json envelope upon encoding" do
|
74
|
+
params = {
|
75
|
+
:charsets => '{"text":"WINDOWS-1252"}',
|
76
|
+
:text => test_string_1_windows1252_encoded
|
77
|
+
}
|
78
|
+
|
79
|
+
new_params = Sendgrid::Parse::EncodableHash.new(params).encode("UTF-8")
|
80
|
+
new_params[:text].should eql(test_string_1)
|
81
|
+
new_params[:charsets].should eql "{\"text\":\"UTF-8\"}"
|
82
|
+
end
|
83
|
+
|
84
|
+
if RUBY_VERSION >= '1.9'
|
85
|
+
it "should correctly change encoding types of charsets" do
|
86
|
+
params = {
|
87
|
+
:charsets => '{"text":"WINDOWS-1252"}',
|
88
|
+
:text => test_string_1_windows1252_encoded
|
89
|
+
}
|
90
|
+
|
91
|
+
new_params = Sendgrid::Parse::EncodableHash.new(params).encode("WINDOWS-1252")
|
92
|
+
new_params[:text].encoding.should eql(Encoding::Windows_1252)
|
93
|
+
|
94
|
+
new_params = Sendgrid::Parse::EncodableHash.new(params).encode("UTF-8")
|
95
|
+
new_params[:text].encoding.should eql(Encoding::UTF_8)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should correctly force encoding types of non-specified fields" do
|
99
|
+
params = {
|
100
|
+
:charsets => '{"text":"WINDOWS-1252"}',
|
101
|
+
:text => test_string_1_windows1252_encoded,
|
102
|
+
:subject => "Test subject"
|
103
|
+
}
|
104
|
+
|
105
|
+
new_params = Sendgrid::Parse::EncodableHash.new(params).encode("WINDOWS-1252")
|
106
|
+
new_params[:text].encoding.should eql(Encoding::Windows_1252)
|
107
|
+
new_params[:subject].encoding.should eql(Encoding::Windows_1252)
|
108
|
+
|
109
|
+
new_params = Sendgrid::Parse::EncodableHash.new(params).encode("UTF-8")
|
110
|
+
new_params[:text].encoding.should eql(Encoding::UTF_8)
|
111
|
+
new_params[:subject].encoding.should eql(Encoding::UTF_8)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sendgrid-parse
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Rob Forman
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-05-23 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: json
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 13
|
29
|
+
segments:
|
30
|
+
- 1
|
31
|
+
- 7
|
32
|
+
- 3
|
33
|
+
version: 1.7.3
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: iconv
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 9
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
- 1
|
48
|
+
version: "0.1"
|
49
|
+
type: :runtime
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: rspec
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ~>
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 39
|
60
|
+
segments:
|
61
|
+
- 2
|
62
|
+
- 10
|
63
|
+
- 0
|
64
|
+
version: 2.10.0
|
65
|
+
type: :development
|
66
|
+
version_requirements: *id003
|
67
|
+
description: Library to dynamically set or change the encoding type for fields, ie params from SendGrid Parse API.
|
68
|
+
email:
|
69
|
+
- rob@robforman.com
|
70
|
+
executables: []
|
71
|
+
|
72
|
+
extensions: []
|
73
|
+
|
74
|
+
extra_rdoc_files: []
|
75
|
+
|
76
|
+
files:
|
77
|
+
- .gitignore
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- lib/sendgrid-parse.rb
|
83
|
+
- lib/sendgrid-parse/encodable_hash.rb
|
84
|
+
- lib/sendgrid-parse/version.rb
|
85
|
+
- sendgrid-parse.gemspec
|
86
|
+
- spec/sendgrid-parse_spec.rb
|
87
|
+
homepage: https://github.com/robforman/sendgrid-parse
|
88
|
+
licenses: []
|
89
|
+
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
hash: 3
|
101
|
+
segments:
|
102
|
+
- 0
|
103
|
+
version: "0"
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
hash: 3
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
version: "0"
|
113
|
+
requirements: []
|
114
|
+
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 1.8.24
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: Library to dynamically set or change the encoding type for fields, ie params from SendGrid Parse API.
|
120
|
+
test_files:
|
121
|
+
- spec/sendgrid-parse_spec.rb
|