demoji 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 68a7fd5b7f58ef59a78bbae302093589d7f1a35b
4
+ data.tar.gz: 5f11762095f2934cb57f55bb42f306f1ef199dce
5
+ SHA512:
6
+ metadata.gz: b7ac06c2ab0ce1612f9b514f0df5f175611f263aae348481558d8ba5101868ffee49f9988f8f4e358921c04c395ad8d75071013e103c2025cc54f0f53d380972
7
+ data.tar.gz: 1ce9da5d44f537ce7b1649c8c282012c79126e174085b52b07774f8692840c28f3b0558039f9ae17263f58c788815f4725cb5514acc26550803fdb8b5cde7fb8
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ spec/config/database.yml
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in demoji.gemspec
4
+ gemspec
5
+
6
+ gem 'mysql2'
7
+ gem 'rspec'
8
+ gem 'debugger'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 David Jairala
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.
@@ -0,0 +1,35 @@
1
+ # Demoji
2
+
3
+ MySQL configured with utf-8 encoding blows up when trying to save text rows containing emojis, etc., to address this, Demoji rescues from that specific exception and replaces the culprit chars with empty spaces.
4
+
5
+ This is a workaround until Rails adds support for UTF8MB4 in migrations, schema, etc.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'demoji'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install demoji
20
+
21
+ ## Usage
22
+
23
+ Write an initializer in: `config/initializers/demoji.rb`:
24
+
25
+ ```ruby
26
+ ActiveRecord::Base.send :include, Demoji
27
+ ```
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'demoji/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "demoji"
8
+ spec.version = Demoji::VERSION
9
+ spec.authors = ["David Jairala"]
10
+ spec.email = ["davidjairala@gmail.com"]
11
+ spec.description = %q{Replace emojis as to not blow up utf8 MySQL}
12
+ spec.summary = %q{MySQL configured with utf-8 encoding blows up when trying to save text rows containing emojis, etc., to address this, Demoji rescues from that specific exception and replaces the culprit chars with empty spaces. This is a workaround until Rails adds support for UTF8MB4 in migrations, schema, etc.}
13
+ spec.homepage = "https://github.com/taskrabbit/demoji"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "activesupport"
24
+ spec.add_development_dependency "activerecord"
25
+ end
@@ -0,0 +1,52 @@
1
+ require "demoji/version"
2
+ require 'active_support/concern'
3
+ require 'active_support/core_ext/module/aliasing'
4
+
5
+ module Demoji
6
+
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ alias_method_chain :create_or_update, :utf8_rescue
11
+ end
12
+
13
+ private
14
+
15
+ def create_or_update_with_utf8_rescue
16
+ _rescued_counter ||= 0
17
+
18
+ create_or_update_without_utf8_rescue
19
+ rescue ActiveRecord::StatementInvalid => ex
20
+ raise ex unless ex.message.match /Mysql2::Error: Incorrect string value:/
21
+
22
+ _rescued_counter += 1
23
+
24
+ raise ex if _rescued_counter > 1
25
+
26
+ _fix_utf8_attributes
27
+ retry
28
+ end
29
+
30
+ def _fix_utf8_attributes
31
+ self.attributes.each do |k, v|
32
+ next if v.blank? || !v.is_a?(String)
33
+ self.send "#{k}=", _fix_chars(v)
34
+ end
35
+ end
36
+
37
+ def _fix_chars(str)
38
+ "".tap do |out_str|
39
+
40
+ # for instead of split and joins for perf
41
+ for i in (0...str.length)
42
+ char = str[i]
43
+ char = 32.chr if char.ord >= 10000
44
+ out_str << char
45
+ end
46
+
47
+ end
48
+ end
49
+
50
+ # /private
51
+
52
+ end
@@ -0,0 +1,3 @@
1
+ module Demoji
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ test:
2
+ adapter: mysql2
3
+ host: localhost
4
+ username: root
5
+ password:
6
+ database: demoji_test
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe TestUser do
4
+
5
+ it "doesn't blow up when trying to save emoji" do
6
+ u = TestUser.new
7
+ u.name = "😊"
8
+ debugger
9
+ expect{ u.save }.to_not raise_error
10
+ expect(u).to be_persisted
11
+ expect(u.reload.name.strip).to eql ""
12
+ end
13
+
14
+ it "only tries to fix once" do
15
+ TestUser.any_instance.stub(:create).and_raise(ActiveRecord::StatementInvalid.new("Mysql2::Error: Incorrect string value: things!"))
16
+ TestUser.any_instance.should_receive(:_fix_utf8_attributes).once
17
+
18
+ u = TestUser.new
19
+ u.name = "😊"
20
+ expect{ u.save }.to raise_error
21
+ expect(u).to_not be_persisted
22
+ end
23
+
24
+ it "leaves other chars alone" do
25
+ u = TestUser.new
26
+ u.name = "Peter Perez\n😊"
27
+ expect{ u.save }.to_not raise_error
28
+ expect(u).to be_persisted
29
+ expect(u.reload.name.strip).to eql "Peter Perez"
30
+ end
31
+
32
+ it "doesn't mess up with valid language specific chars" do
33
+ u = TestUser.new
34
+ u.name = "üç£"
35
+ expect { u.save }.to_not raise_error
36
+ expect(u).to be_persisted
37
+ expect(u.reload.name).to eql "üç£"
38
+ end
39
+
40
+ end
@@ -0,0 +1,31 @@
1
+ require 'yaml'
2
+
3
+ ENV['RACK_ENV'] ||= 'test'
4
+
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ APP_DIR ||= File.expand_path('../../', __FILE__)
8
+
9
+ require 'demoji'
10
+ require 'rspec'
11
+ require 'active_record'
12
+
13
+ ActiveRecord::Base.send :include, Demoji
14
+
15
+ Dir["#{APP_DIR}/spec/support/**/*.rb"].each {|f| require f}
16
+
17
+ RSpec.configure do |config|
18
+
19
+ config.before(:suite) do
20
+ db_config = YAML.load_file(File.join(APP_DIR, 'spec', 'config', 'database.yml'))[ENV['RACK_ENV']]
21
+ ActiveRecord::Base.establish_connection db_config
22
+
23
+ ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS `test_users`")
24
+ ActiveRecord::Base.connection.execute("CREATE TABLE IF NOT EXISTS `test_users` (`id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, PRIMARY KEY (`id`)) DEFAULT CHARSET=utf8;")
25
+ end
26
+
27
+ config.before(:each) do
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,4 @@
1
+ require 'demoji'
2
+
3
+ class TestUser < ActiveRecord::Base
4
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: demoji
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - David Jairala
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Replace emojis as to not blow up utf8 MySQL
70
+ email:
71
+ - davidjairala@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - demoji.gemspec
83
+ - lib/demoji.rb
84
+ - lib/demoji/version.rb
85
+ - spec/config/database.example.yml
86
+ - spec/models/test_user_spec.rb
87
+ - spec/spec_helper.rb
88
+ - spec/support/test_user.rb
89
+ homepage: https://github.com/taskrabbit/demoji
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.0.6
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: MySQL configured with utf-8 encoding blows up when trying to save text rows
113
+ containing emojis, etc., to address this, Demoji rescues from that specific exception
114
+ and replaces the culprit chars with empty spaces. This is a workaround until Rails
115
+ adds support for UTF8MB4 in migrations, schema, etc.
116
+ test_files:
117
+ - spec/config/database.example.yml
118
+ - spec/models/test_user_spec.rb
119
+ - spec/spec_helper.rb
120
+ - spec/support/test_user.rb