chinese_permalink 1.1.3 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -14,6 +14,18 @@ h2. Install
14
14
 
15
15
  ***************************************************************************
16
16
 
17
+ h2. Config
18
+
19
+ As google translator api begins to charge, I decide to use bing translator api, it's free. You have to add a config file to tell chinese_permalink what's your bing app id in <code>config/chinese_permalink.yml</code>
20
+
21
+ <pre><code>
22
+ bing:
23
+ app_id: xxx
24
+ language: zh-cht (chinese traditional, default is zh-chs, chinese simplified)
25
+ </code></pre>
26
+
27
+ ***************************************************************************
28
+
17
29
  h2. Example
18
30
 
19
31
  *1. Define chinese_permalink to model*
data/Rakefile CHANGED
@@ -30,7 +30,6 @@ Jeweler::Tasks.new do |gemspec|
30
30
  gemspec.email = "flyerhzm@gmail.com"
31
31
  gemspec.homepage = "http://github.com/flyerhzm/chinese_permalink"
32
32
  gemspec.authors = ["Richard Huang"]
33
- gemspec.add_dependency 'sishen-rtranslate'
34
33
  gemspec.files.exclude '.gitignore'
35
34
  gemspec.files.exclude 'log/*'
36
35
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.3
1
+ 1.2.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{chinese_permalink}
8
- s.version = "1.1.3"
8
+ s.version = "1.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Richard Huang"]
12
- s.date = %q{2010-02-02}
12
+ s.date = %q{2011-09-03}
13
13
  s.description = %q{This plugin adds a capability for AR model to create a seo permalink with your chinese text. It will translate your chinese text to english url based on google translate.}
14
14
  s.email = %q{flyerhzm@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -32,23 +32,19 @@ Gem::Specification.new do |s|
32
32
  s.homepage = %q{http://github.com/flyerhzm/chinese_permalink}
33
33
  s.rdoc_options = ["--charset=UTF-8"]
34
34
  s.require_paths = ["lib"]
35
- s.rubygems_version = %q{1.3.5}
35
+ s.rubygems_version = %q{1.6.2}
36
36
  s.summary = %q{This plugin adds a capability for AR model to create a seo permalink with your chinese text.}
37
37
  s.test_files = [
38
38
  "test/chines_permalink_test.rb"
39
39
  ]
40
40
 
41
41
  if s.respond_to? :specification_version then
42
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
42
  s.specification_version = 3
44
43
 
45
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
- s.add_runtime_dependency(%q<sishen-rtranslate>, [">= 0"])
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
45
  else
48
- s.add_dependency(%q<sishen-rtranslate>, [">= 0"])
49
46
  end
50
47
  else
51
- s.add_dependency(%q<sishen-rtranslate>, [">= 0"])
52
48
  end
53
49
  end
54
50
 
@@ -1,20 +1,14 @@
1
- begin
2
- # rtranslate is a gem that can translate text based on google translate
3
- require 'rtranslate'
4
- rescue Object
5
- puts "no rtranslate, you might want to look into it."
6
- end
1
+ require 'net/http'
7
2
 
8
3
  module ChinesePermalink
9
-
10
4
  def self.included(base)
11
5
  base.class_eval do
12
6
  class_inheritable_accessor :permalink_attrs, :permalink_field, :before_methods, :after_methods
13
7
  end
14
8
  base.extend ClassMethods
15
- end
9
+ end
16
10
 
17
- private
11
+ private
18
12
  def create_permalink
19
13
  if self.permalink.nil?
20
14
  chinese_permalink = self.class.permalink_attrs.collect do |attr_name|
@@ -24,7 +18,7 @@ module ChinesePermalink
24
18
  chinese_permalink = self.send(method, chinese_permalink)
25
19
  end
26
20
 
27
- english_permalink = Translate.t(chinese_permalink, 'CHINESE', 'ENGLISH')
21
+ english_permalink = Translate.t(chinese_permalink)
28
22
  self.class.after_methods.each do |method|
29
23
  english_permalink = self.send(method, english_permalink)
30
24
  end
@@ -45,9 +39,9 @@ module ChinesePermalink
45
39
  def remove_space(text)
46
40
  text.gsub(/\s+/, '-')
47
41
  end
48
-
42
+
49
43
  def remove_punctuation(text)
50
- text.gsub(/&#39;|&amp;|&quot;|&lt;|&gt;|\/|/, '')
44
+ text.gsub(/&#39;|&amp;|&quot;|&lt;|&gt;/, '').gsub(/\//, '-')
51
45
  end
52
46
 
53
47
  def remove_duplicate_dash(text)
@@ -58,7 +52,6 @@ module ChinesePermalink
58
52
  end
59
53
 
60
54
  module ClassMethods
61
-
62
55
  def chinese_permalink(attr_names, options = {})
63
56
  options = {:permalink_field => 'permalink'}.merge(options)
64
57
  self.permalink_attrs = Array(attr_names)
@@ -69,4 +62,23 @@ module ChinesePermalink
69
62
  after_save :create_permalink
70
63
  end
71
64
  end
65
+
66
+ class Translate
67
+ class <<self
68
+ def t(text)
69
+ response = Net::HTTP.get(URI.parse(URI.encode(translate_url + text)))
70
+ response =~ %r|<string.*?>(.*?)</string>|
71
+ $1
72
+ end
73
+
74
+ def translate_url
75
+ @translate_url ||= begin
76
+ config = YAML.load(File.open(File.join(Rails.root, "config/chinese_permalink.yml")))
77
+ app_id = config['bing']['app_id']
78
+ language = config['bing']['language']
79
+ "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=#{app_id}&from=#{language}&to=en&text="
80
+ end
81
+ end
82
+ end
83
+ end
72
84
  end
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'test/unit'
2
3
 
3
4
  require 'rubygems'
@@ -6,14 +7,20 @@ require 'active_record'
6
7
  require File.join(File.dirname(__FILE__), '../lib/chinese_permalink')
7
8
  require File.join(File.dirname(__FILE__), '../init')
8
9
 
10
+ class Rails
11
+ def self.root
12
+ File.expand_path(__FILE__).split('/')[0..-3].join('/')
13
+ end
14
+ end
15
+
9
16
  ActiveRecord::Base.establish_connection(
10
- :adapter => 'sqlite3',
17
+ :adapter => 'sqlite3',
11
18
  :database => ':memory:'
12
19
  )
13
20
 
14
21
  def setup_db
15
22
  ActiveRecord::Migration.verbose = false
16
-
23
+
17
24
  ActiveRecord::Schema.define(:version => 1) do
18
25
  create_table :posts do |t|
19
26
  t.string :title
@@ -64,15 +71,15 @@ class ChinesePermalinkTest < Test::Unit::TestCase
64
71
 
65
72
  def test_simple_chinese_title
66
73
  post = Post.create(:title => '中国人')
67
- assert_equal "chinese", post.permalink
74
+ assert_equal "the-chinese-people", post.permalink
68
75
 
69
76
  post = Post.create(:title => '我是中国人')
70
- assert_equal "i-am-a-chinese", post.permalink
77
+ assert_equal "i-am-chinese", post.permalink
71
78
  end
72
79
 
73
80
  def test_chinese_title_with_dash
74
81
  post = Post.create(:title => '我是中国人——上海')
75
- assert_equal "i-am-a-chinese-shanghai", post.permalink
82
+ assert_equal "i-am-from-china-shanghai", post.permalink
76
83
 
77
84
  post = Post.create(:title => '我是中国人──上海')
78
85
  assert_equal "i-am-a-chinese-shanghai", post.permalink
@@ -85,20 +92,20 @@ class ChinesePermalinkTest < Test::Unit::TestCase
85
92
 
86
93
  post = Post.create(:title => '“工作”')
87
94
  assert_equal "work", post.permalink
88
-
95
+
89
96
  post = Post.create(:title => '妈妈的礼物')
90
- assert_equal "moms-gift", post.permalink
97
+ assert_equal "mothers-gift", post.permalink
91
98
 
92
99
  post = Post.create(:title => '宝洁')
93
- assert_equal "procter-gamble", post.permalink
100
+ assert_equal "procter", post.permalink
94
101
 
95
102
  post = Post.create(:title => '自我介绍')
96
- assert_equal "self-introduction", post.permalink
103
+ assert_equal "introducing-myself", post.permalink
97
104
  end
98
105
 
99
106
  def test_chinese_category_and_title
100
107
  post = CategoryPost.create(:title => '我是中国人', :category => '介绍')
101
- assert_equal "introduction-i-am-a-chinese", post.permalink
108
+ assert_equal "introduction-i-am-from-china", post.permalink
102
109
  end
103
110
 
104
111
  def test_complicated_title_with_before_methods
@@ -108,6 +115,6 @@ class ChinesePermalinkTest < Test::Unit::TestCase
108
115
 
109
116
  def test_complicated_title_with_after_methods
110
117
  post = ComplicatedAfterPost.create(:title => '宝洁')
111
- assert_equal "pg", post.permalink
118
+ assert_equal "procter", post.permalink
112
119
  end
113
120
  end
metadata CHANGED
@@ -1,7 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chinese_permalink
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ prerelease:
5
+ version: 1.2.0
5
6
  platform: ruby
6
7
  authors:
7
8
  - Richard Huang
@@ -9,19 +10,10 @@ autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
 
12
- date: 2010-02-02 00:00:00 +08:00
13
+ date: 2011-09-03 00:00:00 +08:00
13
14
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: sishen-rtranslate
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "0"
24
- version:
15
+ dependencies: []
16
+
25
17
  description: This plugin adds a capability for AR model to create a seo permalink with your chinese text. It will translate your chinese text to english url based on google translate.
26
18
  email: flyerhzm@gmail.com
27
19
  executables: []
@@ -53,21 +45,21 @@ rdoc_options:
53
45
  require_paths:
54
46
  - lib
55
47
  required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
56
49
  requirements:
57
50
  - - ">="
58
51
  - !ruby/object:Gem::Version
59
52
  version: "0"
60
- version:
61
53
  required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
62
55
  requirements:
63
56
  - - ">="
64
57
  - !ruby/object:Gem::Version
65
58
  version: "0"
66
- version:
67
59
  requirements: []
68
60
 
69
61
  rubyforge_project:
70
- rubygems_version: 1.3.5
62
+ rubygems_version: 1.6.2
71
63
  signing_key:
72
64
  specification_version: 3
73
65
  summary: This plugin adds a capability for AR model to create a seo permalink with your chinese text.