sendgrid-parse 0.1.6 → 0.1.7

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.
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  require 'json'
2
3
  require 'delegate'
3
4
  require 'iconv' unless RUBY_VERSION >= '1.9'
@@ -25,6 +26,8 @@ module Sendgrid
25
26
  end
26
27
 
27
28
  def encode(to_encoding, ignore=[])
29
+ _test_encoding(to_encoding)
30
+
28
31
  component = __getobj__.dup
29
32
  ignore.each { |e| component.delete(e) if component.has_key?(e)}
30
33
 
@@ -36,8 +39,12 @@ module Sendgrid
36
39
  component.each do |key, value|
37
40
  if charsets.has_key?(key)
38
41
  from_encoding = charsets[key]
39
- component[key] = _encode(component[key], from_encoding, to_encoding)
40
- charsets[key] = to_encoding
42
+ transcoded_value = _encode(component[key], from_encoding, to_encoding) rescue nil
43
+
44
+ if transcoded_value
45
+ component[key] = transcoded_value
46
+ charsets[key] = to_encoding
47
+ end
41
48
  else
42
49
  # If we weren't told, set it to the target encoding type.
43
50
  component[key] = _encode(component[key], to_encoding, to_encoding)
@@ -53,19 +60,22 @@ module Sendgrid
53
60
  end
54
61
 
55
62
  protected
63
+ def _test_encoding(to)
64
+ if RUBY_VERSION >= '1.9'
65
+ "".force_encoding("UTF-8").encode(to)
66
+ else
67
+ Iconv.conv("#{to}", "UTF-8", "")
68
+ end
69
+ end
70
+
56
71
  def _encode(value, from, to)
57
72
  if RUBY_VERSION >= '1.9'
58
73
  if value.respond_to? :force_encoding
59
- from_enc = Encoding.find(from) rescue nil
60
-
61
- value = value.force_encoding(from) if from_enc
74
+ value = value.force_encoding(from)
62
75
  value = value.encode(to, :invalid => :replace, :undef => :replace, :replace => '')
63
76
  end
64
77
  else
65
- # Iconv doesn't have a way to find the charset, so we have to just try it and rescue
66
- from_enc = Iconv.conv("UTF-8", from, "test string") rescue nil
67
-
68
- value = Iconv.conv("#{to}//IGNORE", from, value) if from_enc
78
+ value = Iconv.conv("#{to}//IGNORE", from, value)
69
79
  end
70
80
 
71
81
  value
@@ -1,5 +1,5 @@
1
1
  module Sendgrid
2
2
  module Parse
3
- VERSION = "0.1.6"
3
+ VERSION = "0.1.7"
4
4
  end
5
5
  end
@@ -47,6 +47,17 @@ describe Sendgrid::Parse::EncodableHash do
47
47
  new_params[:text].should eql(test_string_1)
48
48
  end
49
49
 
50
+ it "should not claim to have encoded skipped 'charsets' encoding types" do
51
+ params = {
52
+ :charsets => '{"text":"x-user-defined"}',
53
+ :text => test_string_1
54
+ }
55
+
56
+ new_params = Sendgrid::Parse::EncodableHash.new(params).encode("UTF-8")
57
+ new_params[:text].should eql(test_string_1)
58
+ new_params[:charsets].should eql "{\"text\":\"x-user-defined\"}"
59
+ end
60
+
50
61
  it "should strip unconvertible characters" do
51
62
  params = {
52
63
  :charsets => '{"text":"UTF-8"}',
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sendgrid-parse
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-06-12 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &70101262154740 !ruby/object:Gem::Requirement
16
+ requirement: &70112196603160 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.7.3
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70101262154740
24
+ version_requirements: *70112196603160
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70101262154220 !ruby/object:Gem::Requirement
27
+ requirement: &70112196602620 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 2.10.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70101262154220
35
+ version_requirements: *70112196602620
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: active_support
38
- requirement: &70101262153760 !ruby/object:Gem::Requirement
38
+ requirement: &70112196602160 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 3.0.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70101262153760
46
+ version_requirements: *70112196602160
47
47
  description: Library to dynamically set or change the encoding type for fields, ie
48
48
  params from SendGrid Parse API.
49
49
  email: