youdao_fanyi 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. data/.gitignore +25 -0
  2. data/CHANGELOG +4 -0
  3. data/Gemfile +4 -0
  4. data/Guardfile +22 -0
  5. data/README.markdown +79 -0
  6. data/Rakefile +2 -0
  7. data/bin/youdao_fanyi +48 -0
  8. data/examples/translate.rb +170 -0
  9. data/lib/generators/youdao_fanyi/install/USAGE +2 -0
  10. data/lib/generators/youdao_fanyi/install/install_generator.rb +14 -0
  11. data/lib/generators/youdao_fanyi/install/templates/youdao_fanyi.rb +7 -0
  12. data/lib/generators/youdao_fanyi/install/templates/youdao_fanyi_api_keys.yml +9 -0
  13. data/lib/youdao_fanyi/configuration.rb +14 -0
  14. data/lib/youdao_fanyi/connector.rb +42 -0
  15. data/lib/youdao_fanyi/errors.rb +15 -0
  16. data/lib/youdao_fanyi/translator.rb +43 -0
  17. data/lib/youdao_fanyi/version.rb +6 -0
  18. data/lib/youdao_fanyi.rb +37 -0
  19. data/spec/rails3_0_app/.gitignore +4 -0
  20. data/spec/rails3_0_app/Gemfile +7 -0
  21. data/spec/rails3_0_app/Rakefile +7 -0
  22. data/spec/rails3_0_app/app/controllers/application_controller.rb +3 -0
  23. data/spec/rails3_0_app/app/helpers/application_helper.rb +2 -0
  24. data/spec/rails3_0_app/app/views/layouts/application.html.erb +14 -0
  25. data/spec/rails3_0_app/config/application.rb +42 -0
  26. data/spec/rails3_0_app/config/boot.rb +6 -0
  27. data/spec/rails3_0_app/config/database.yml +22 -0
  28. data/spec/rails3_0_app/config/environment.rb +5 -0
  29. data/spec/rails3_0_app/config/environments/development.rb +26 -0
  30. data/spec/rails3_0_app/config/environments/production.rb +49 -0
  31. data/spec/rails3_0_app/config/environments/test.rb +35 -0
  32. data/spec/rails3_0_app/config/initializers/backtrace_silencers.rb +7 -0
  33. data/spec/rails3_0_app/config/initializers/inflections.rb +10 -0
  34. data/spec/rails3_0_app/config/initializers/mime_types.rb +5 -0
  35. data/spec/rails3_0_app/config/initializers/secret_token.rb +7 -0
  36. data/spec/rails3_0_app/config/initializers/session_store.rb +8 -0
  37. data/spec/rails3_0_app/config/initializers/youdao_fanyi.rb +7 -0
  38. data/spec/rails3_0_app/config/locales/en.yml +5 -0
  39. data/spec/rails3_0_app/config/routes.rb +58 -0
  40. data/spec/rails3_0_app/config.ru +4 -0
  41. data/spec/rails3_0_app/db/seeds.rb +7 -0
  42. data/spec/rails3_0_app/lib/tasks/.gitkeep +0 -0
  43. data/spec/rails3_0_app/public/404.html +26 -0
  44. data/spec/rails3_0_app/public/422.html +26 -0
  45. data/spec/rails3_0_app/public/500.html +26 -0
  46. data/spec/rails3_0_app/public/favicon.ico +0 -0
  47. data/spec/rails3_0_app/public/images/rails.png +0 -0
  48. data/spec/rails3_0_app/public/index.html +239 -0
  49. data/spec/rails3_0_app/public/javascripts/.gitkeep +0 -0
  50. data/spec/rails3_0_app/public/javascripts/application.js +0 -0
  51. data/spec/rails3_0_app/public/robots.txt +5 -0
  52. data/spec/rails3_0_app/public/stylesheets/.gitkeep +0 -0
  53. data/spec/rails3_0_app/script/rails +6 -0
  54. data/spec/rails3_0_app/vendor/plugins/.gitkeep +0 -0
  55. data/spec/spec_helper.rb +16 -0
  56. data/spec/support/youdao_fanyi_api_keys.yml.example +9 -0
  57. data/spec/youdao_fanyi/configuration_spec.rb +16 -0
  58. data/spec/youdao_fanyi/connector_spec.rb +86 -0
  59. data/spec/youdao_fanyi/translator_spec.rb +39 -0
  60. data/spec/youdao_fanyi_spec.rb +28 -0
  61. data/youdao_fanyi.gemspec +32 -0
  62. metadata +260 -0
@@ -0,0 +1,16 @@
1
+ #encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe YoudaoFanyi::Configuration do
5
+
6
+ let(:default_configuration) { [{ "keyfrom" => "test.vkill.net", "key" => "1234567890" }] }
7
+
8
+ it "default configuration should defined" do
9
+ YoudaoFanyi::Configuration.api_keys.should == default_configuration
10
+
11
+ YoudaoFanyi::Configuration.http_proxy.should == nil
12
+ end
13
+
14
+
15
+ end
16
+
@@ -0,0 +1,86 @@
1
+ #encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe YoudaoFanyi::Connector do
5
+
6
+ before {
7
+ YoudaoFanyi::Configuration.api_keys = YOUDAO_FANYI_API_KEYS
8
+ YoudaoFanyi.reload!
9
+ }
10
+
11
+ let(:base_uri) { 'http://fanyi.youdao.com/fanyiapi.do' }
12
+ let(:default_params) { {:type => :data, :doctype => :json, :version => 1.1 } }
13
+
14
+ def translate(response, query, translation)
15
+ results = JSON.load(response.body)
16
+ results["errorCode"].to_i.should == 0
17
+ results["query"].should == query.to_s
18
+ results["translation"].include?(translation.to_s).should be_true
19
+ end
20
+
21
+ it "should included HTTParty" do
22
+ YoudaoFanyi::Connector.include?(HTTParty).should be_true
23
+ end
24
+
25
+ it "base_uri should defined" do
26
+ YoudaoFanyi::Connector.default_options[:base_uri].should == base_uri
27
+ end
28
+
29
+ it "default_params should defined" do
30
+ YoudaoFanyi::Connector.default_params[:type].should == default_params[:type]
31
+ YoudaoFanyi::Connector.default_params[:doctype].should == default_params[:doctype]
32
+ YoudaoFanyi::Connector.default_params[:version].should == default_params[:version]
33
+ end
34
+
35
+ it "use proxy" do
36
+ YoudaoFanyi::Configuration.http_proxy = 'http://127.0.0.1:8118'
37
+ YoudaoFanyi.reload!
38
+ YoudaoFanyi::Connector.default_options[:http_proxyaddr] = '127.0.0.1'
39
+ YoudaoFanyi::Connector.default_options[:http_proxyport] = '8118'
40
+ YoudaoFanyi::Configuration.http_proxy = ''
41
+ YoudaoFanyi.reload!
42
+ #ENV['http_proxy']
43
+ end
44
+
45
+ it "translate china should return 中国 by class method" do
46
+ response = YoudaoFanyi::Connector.get(
47
+ '', :query => {:q => :china}.merge(YoudaoFanyi.api_key())
48
+ )
49
+ translate(response, :china, "中国")
50
+ end
51
+
52
+ it "when initialize, @key and @keyfrom should defined" do
53
+ connector = YoudaoFanyi::Connector.new
54
+ connector.key.to_s.empty?.should be_false
55
+ connector.keyfrom.to_s.empty?.should be_false
56
+ end
57
+
58
+ it "translate china should return 中国 by instance method" do
59
+ connector = YoudaoFanyi::Connector.new
60
+ response = connector.class.get(
61
+ '', :query => {:q => :china}.merge(:key => connector.key, :keyfrom => connector.keyfrom)
62
+ )
63
+ translate(response, :china, "中国")
64
+ end
65
+
66
+ it "use translate function" do
67
+ connector = YoudaoFanyi::Connector.new.request(:china)
68
+ if connector.error_code == 0
69
+ connector.translation.should == "中国"
70
+ else
71
+ connector.translation.should_not == "中国"
72
+ connector.translation.should match(/^ERROR:/i)
73
+ end
74
+ connector.response.should_not be_nil
75
+ connector.results.should_not be_nil
76
+ end
77
+
78
+ it "translate function should escape string" do
79
+ connector = YoudaoFanyi::Connector.new.request(:china)
80
+ connector.q.should == "china"
81
+ connector = YoudaoFanyi::Connector.new.request("中国")
82
+ # connector.q.should == "%E4%B8%AD%E5%9B%BD" #CGI::escape
83
+ connector.q.should == "中国"
84
+ end
85
+ end
86
+
@@ -0,0 +1,39 @@
1
+ #encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe YoudaoFanyi::Translator do
5
+
6
+ before {
7
+ YoudaoFanyi::Configuration.api_keys = YOUDAO_FANYI_API_KEYS
8
+ YoudaoFanyi.reload!
9
+ }
10
+
11
+ it "should has translate" do
12
+ YoudaoFanyi::Translator.should respond_to(:translate)
13
+ end
14
+
15
+ it "translate return a instance of YoudaoFanyi::Connector" do
16
+ YoudaoFanyi::Translator.translate(:china).first.should be_instance_of(YoudaoFanyi::Connector)
17
+ end
18
+
19
+ it "!translate return a instance of self" do
20
+ YoudaoFanyi::Translator.translate(:china, :not_translate => true).should be_instance_of(YoudaoFanyi::Translator)
21
+ end
22
+
23
+ it "translate special word" do
24
+ translator = YoudaoFanyi::Translator.translate(nil, true, false, "", :not_translate => true)
25
+ translator.queries.should == ['nil', 'true', 'false', 'nil']
26
+ end
27
+
28
+ it "should has t" do
29
+ YoudaoFanyi::Translator.should respond_to(:t)
30
+ end
31
+
32
+ it "only return translation when use t()" do
33
+ YoudaoFanyi::Translator.t(:china).should == ["中国"]
34
+ YoudaoFanyi::Translator.t(:china, :i).should == ["中国", "我"]
35
+ end
36
+
37
+
38
+ end
39
+
@@ -0,0 +1,28 @@
1
+ #encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe YoudaoFanyi do
5
+ it "should has reload!" do
6
+ YoudaoFanyi.should respond_to(:reload!)
7
+ end
8
+
9
+ it "should has api_key" do
10
+ YoudaoFanyi.should respond_to(:api_key)
11
+ end
12
+
13
+ it "should fast define api_keys and rand use api_key from api_keys" do
14
+ YoudaoFanyi.configure do |config|
15
+ config.api_keys = YOUDAO_FANYI_API_KEYS
16
+ end
17
+ YoudaoFanyi::Configuration.api_keys.should include(YoudaoFanyi.api_key())
18
+ end
19
+
20
+ it "should has t" do
21
+ YoudaoFanyi.should respond_to(:t)
22
+ end
23
+
24
+ it "t alias YoudaoFanyi::Translator.t" do
25
+ YoudaoFanyi.t(:china).should == ["中国"]
26
+ end
27
+ end
28
+
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/youdao_fanyi/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["vkill"]
6
+ gem.email = ["vkill.net@gmail.com"]
7
+ gem.description = "A http://fanyi.youdao.com api library for Ruby."
8
+ gem.summary = "A http://fanyi.youdao.com api library for Ruby."
9
+ gem.homepage = "https://github.com/vkill/youdao_fanyi"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "youdao_fanyi"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = YoudaoFanyi::VERSION
17
+
18
+ gem.add_development_dependency 'bundler'
19
+ gem.add_development_dependency "rake"
20
+ gem.add_development_dependency "rspec"
21
+ gem.add_development_dependency "guard-rspec"
22
+ gem.add_development_dependency "pry"
23
+ gem.add_development_dependency "rspec-rails"
24
+
25
+ #use in spec/rails3_0_app
26
+ gem.add_development_dependency "rails", "~> 3.0"
27
+ gem.add_development_dependency "sqlite3-ruby"
28
+
29
+ gem.add_dependency "httparty", "~> 0.8.1"
30
+ gem.add_dependency "activesupport", "~> 3.0"
31
+ end
32
+
metadata ADDED
@@ -0,0 +1,260 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: youdao_fanyi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - vkill
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: &68883460 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *68883460
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &69062350 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *69062350
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &69061280 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *69061280
47
+ - !ruby/object:Gem::Dependency
48
+ name: guard-rspec
49
+ requirement: &69060680 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *69060680
58
+ - !ruby/object:Gem::Dependency
59
+ name: pry
60
+ requirement: &69059670 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *69059670
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
71
+ requirement: &69058440 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *69058440
80
+ - !ruby/object:Gem::Dependency
81
+ name: rails
82
+ requirement: &69056950 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ version: '3.0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *69056950
91
+ - !ruby/object:Gem::Dependency
92
+ name: sqlite3-ruby
93
+ requirement: &69055940 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *69055940
102
+ - !ruby/object:Gem::Dependency
103
+ name: httparty
104
+ requirement: &69052440 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 0.8.1
110
+ type: :runtime
111
+ prerelease: false
112
+ version_requirements: *69052440
113
+ - !ruby/object:Gem::Dependency
114
+ name: activesupport
115
+ requirement: &69051600 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ~>
119
+ - !ruby/object:Gem::Version
120
+ version: '3.0'
121
+ type: :runtime
122
+ prerelease: false
123
+ version_requirements: *69051600
124
+ description: A http://fanyi.youdao.com api library for Ruby.
125
+ email:
126
+ - vkill.net@gmail.com
127
+ executables:
128
+ - youdao_fanyi
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - .gitignore
133
+ - CHANGELOG
134
+ - Gemfile
135
+ - Guardfile
136
+ - README.markdown
137
+ - Rakefile
138
+ - bin/youdao_fanyi
139
+ - examples/translate.rb
140
+ - lib/generators/youdao_fanyi/install/USAGE
141
+ - lib/generators/youdao_fanyi/install/install_generator.rb
142
+ - lib/generators/youdao_fanyi/install/templates/youdao_fanyi.rb
143
+ - lib/generators/youdao_fanyi/install/templates/youdao_fanyi_api_keys.yml
144
+ - lib/youdao_fanyi.rb
145
+ - lib/youdao_fanyi/configuration.rb
146
+ - lib/youdao_fanyi/connector.rb
147
+ - lib/youdao_fanyi/errors.rb
148
+ - lib/youdao_fanyi/translator.rb
149
+ - lib/youdao_fanyi/version.rb
150
+ - spec/rails3_0_app/.gitignore
151
+ - spec/rails3_0_app/Gemfile
152
+ - spec/rails3_0_app/Rakefile
153
+ - spec/rails3_0_app/app/controllers/application_controller.rb
154
+ - spec/rails3_0_app/app/helpers/application_helper.rb
155
+ - spec/rails3_0_app/app/views/layouts/application.html.erb
156
+ - spec/rails3_0_app/config.ru
157
+ - spec/rails3_0_app/config/application.rb
158
+ - spec/rails3_0_app/config/boot.rb
159
+ - spec/rails3_0_app/config/database.yml
160
+ - spec/rails3_0_app/config/environment.rb
161
+ - spec/rails3_0_app/config/environments/development.rb
162
+ - spec/rails3_0_app/config/environments/production.rb
163
+ - spec/rails3_0_app/config/environments/test.rb
164
+ - spec/rails3_0_app/config/initializers/backtrace_silencers.rb
165
+ - spec/rails3_0_app/config/initializers/inflections.rb
166
+ - spec/rails3_0_app/config/initializers/mime_types.rb
167
+ - spec/rails3_0_app/config/initializers/secret_token.rb
168
+ - spec/rails3_0_app/config/initializers/session_store.rb
169
+ - spec/rails3_0_app/config/initializers/youdao_fanyi.rb
170
+ - spec/rails3_0_app/config/locales/en.yml
171
+ - spec/rails3_0_app/config/routes.rb
172
+ - spec/rails3_0_app/db/seeds.rb
173
+ - spec/rails3_0_app/lib/tasks/.gitkeep
174
+ - spec/rails3_0_app/public/404.html
175
+ - spec/rails3_0_app/public/422.html
176
+ - spec/rails3_0_app/public/500.html
177
+ - spec/rails3_0_app/public/favicon.ico
178
+ - spec/rails3_0_app/public/images/rails.png
179
+ - spec/rails3_0_app/public/index.html
180
+ - spec/rails3_0_app/public/javascripts/.gitkeep
181
+ - spec/rails3_0_app/public/javascripts/application.js
182
+ - spec/rails3_0_app/public/robots.txt
183
+ - spec/rails3_0_app/public/stylesheets/.gitkeep
184
+ - spec/rails3_0_app/script/rails
185
+ - spec/rails3_0_app/vendor/plugins/.gitkeep
186
+ - spec/spec_helper.rb
187
+ - spec/support/youdao_fanyi_api_keys.yml.example
188
+ - spec/youdao_fanyi/configuration_spec.rb
189
+ - spec/youdao_fanyi/connector_spec.rb
190
+ - spec/youdao_fanyi/translator_spec.rb
191
+ - spec/youdao_fanyi_spec.rb
192
+ - youdao_fanyi.gemspec
193
+ homepage: https://github.com/vkill/youdao_fanyi
194
+ licenses: []
195
+ post_install_message:
196
+ rdoc_options: []
197
+ require_paths:
198
+ - lib
199
+ required_ruby_version: !ruby/object:Gem::Requirement
200
+ none: false
201
+ requirements:
202
+ - - ! '>='
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ required_rubygems_version: !ruby/object:Gem::Requirement
206
+ none: false
207
+ requirements:
208
+ - - ! '>='
209
+ - !ruby/object:Gem::Version
210
+ version: '0'
211
+ requirements: []
212
+ rubyforge_project:
213
+ rubygems_version: 1.8.10
214
+ signing_key:
215
+ specification_version: 3
216
+ summary: A http://fanyi.youdao.com api library for Ruby.
217
+ test_files:
218
+ - spec/rails3_0_app/.gitignore
219
+ - spec/rails3_0_app/Gemfile
220
+ - spec/rails3_0_app/Rakefile
221
+ - spec/rails3_0_app/app/controllers/application_controller.rb
222
+ - spec/rails3_0_app/app/helpers/application_helper.rb
223
+ - spec/rails3_0_app/app/views/layouts/application.html.erb
224
+ - spec/rails3_0_app/config.ru
225
+ - spec/rails3_0_app/config/application.rb
226
+ - spec/rails3_0_app/config/boot.rb
227
+ - spec/rails3_0_app/config/database.yml
228
+ - spec/rails3_0_app/config/environment.rb
229
+ - spec/rails3_0_app/config/environments/development.rb
230
+ - spec/rails3_0_app/config/environments/production.rb
231
+ - spec/rails3_0_app/config/environments/test.rb
232
+ - spec/rails3_0_app/config/initializers/backtrace_silencers.rb
233
+ - spec/rails3_0_app/config/initializers/inflections.rb
234
+ - spec/rails3_0_app/config/initializers/mime_types.rb
235
+ - spec/rails3_0_app/config/initializers/secret_token.rb
236
+ - spec/rails3_0_app/config/initializers/session_store.rb
237
+ - spec/rails3_0_app/config/initializers/youdao_fanyi.rb
238
+ - spec/rails3_0_app/config/locales/en.yml
239
+ - spec/rails3_0_app/config/routes.rb
240
+ - spec/rails3_0_app/db/seeds.rb
241
+ - spec/rails3_0_app/lib/tasks/.gitkeep
242
+ - spec/rails3_0_app/public/404.html
243
+ - spec/rails3_0_app/public/422.html
244
+ - spec/rails3_0_app/public/500.html
245
+ - spec/rails3_0_app/public/favicon.ico
246
+ - spec/rails3_0_app/public/images/rails.png
247
+ - spec/rails3_0_app/public/index.html
248
+ - spec/rails3_0_app/public/javascripts/.gitkeep
249
+ - spec/rails3_0_app/public/javascripts/application.js
250
+ - spec/rails3_0_app/public/robots.txt
251
+ - spec/rails3_0_app/public/stylesheets/.gitkeep
252
+ - spec/rails3_0_app/script/rails
253
+ - spec/rails3_0_app/vendor/plugins/.gitkeep
254
+ - spec/spec_helper.rb
255
+ - spec/support/youdao_fanyi_api_keys.yml.example
256
+ - spec/youdao_fanyi/configuration_spec.rb
257
+ - spec/youdao_fanyi/connector_spec.rb
258
+ - spec/youdao_fanyi/translator_spec.rb
259
+ - spec/youdao_fanyi_spec.rb
260
+ has_rdoc: