lita-youdao 0.1.0

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: ca2d5fafbde0ebe9e74bb33cce76db26f9074d2b
4
+ data.tar.gz: 09c05d23ce132976f290bc17da8a846ea750d6a6
5
+ SHA512:
6
+ metadata.gz: 2f908c26e45b80cdf5e695896e9246ee2590cf5276263522222e84782f34cc7d20e62f2d2fae6311795a7f25236dee91e833ef5989b3d5273ff677fa9019a53d
7
+ data.tar.gz: 31b768720c3affebce8ca8e8bb478277087a9bd45323a4109ff85cc87db674d7b3e353201f501444c0e1972fdbcc6c868da63a0eb0b228d452f56b9f94a9b209
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,19 @@
1
+ # lita-youdao
2
+
3
+ TODO: Add a description of the plugin.
4
+
5
+ ## Installation
6
+
7
+ Add lita-youdao to your Lita instance's Gemfile:
8
+
9
+ ``` ruby
10
+ gem "lita-youdao"
11
+ ```
12
+
13
+ ## Configuration
14
+
15
+ TODO: Describe any configuration attributes the plugin exposes.
16
+
17
+ ## Usage
18
+
19
+ TODO: Describe the plugin's features and how to use them.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,12 @@
1
+ require "lita"
2
+
3
+ Lita.load_locales Dir[File.expand_path(
4
+ File.join("..", "..", "locales", "*.yml"), __FILE__
5
+ )]
6
+
7
+ require "lita/handlers/youdao"
8
+
9
+ Lita::Handlers::Youdao.template_root File.expand_path(
10
+ File.join("..", "..", "templates"),
11
+ __FILE__
12
+ )
@@ -0,0 +1,83 @@
1
+ module Lita
2
+ module Handlers
3
+ class Youdao < Handler
4
+ config :api_key, required: true
5
+ config :key_from, required: true
6
+
7
+ route(/youdao\s+(.+)/, :translate, command: true, help: {
8
+ "youdao" => "translation using youdao api"
9
+ })
10
+
11
+ def translate response
12
+ word = URI.encode response.matches[0][0]
13
+ translation = redis.get word
14
+ translation = access_api(word) unless translation
15
+ response.reply translation
16
+ end
17
+
18
+ def access_api word
19
+ url = 'http://fanyi.youdao.com/openapi.do'
20
+ res = http.get(url,
21
+ keyfrom: config.key_from,
22
+ key: config.api_key,
23
+ type: 'data',
24
+ doctype: 'json',
25
+ version: '1.1',
26
+ q: word)
27
+ hash_res = MultiJson.load(res.body)
28
+ if hash_res['errorCode'] == 0
29
+ format_display hash_res
30
+ else
31
+ error_code[hash_res['errorCode']]
32
+ end
33
+ end
34
+
35
+ def format_display hash_res
36
+ ["*翻译*: #{format_translation hash_res}",
37
+ "*音标*: #{format_phonetic hash_res}",
38
+ "*基本词典*\n#{ format_explain hash_res}",
39
+ "*网络释义*\n#{format_web_explain(hash_res)}"
40
+ ].join("\n")
41
+ end
42
+
43
+ def format_translation hash_res
44
+ if hash_res.fetch('translation')
45
+ hash_res['translation'].map {|trans| URI.decode trans.gsub(' ', '')}.join(", ") rescue nil
46
+ end
47
+ end
48
+
49
+ def format_phonetic hash_res
50
+ if hash_res.fetch('basic', nil)
51
+ hash_res['basic']['phonetic'] rescue ''
52
+ end
53
+ end
54
+
55
+ def format_explain hash_res
56
+ if hash_res.fetch('basic', nil)
57
+ hash_res['basic']['explains'].map {|line| "\t#{line}"}.join("\n") rescue ''
58
+ end
59
+ end
60
+
61
+ def format_web_explain(hash_res)
62
+ if hash_res.fetch('web', nil)
63
+ content = []
64
+ hash_res['web'].each do |explain|
65
+ content << explain['value'].map {|line| "\t#{line}"}.join(', ') rescue nil
66
+ end
67
+ content.join("\n")
68
+ end
69
+ end
70
+
71
+ def error_code
72
+ { 20 => '要翻译的文本过长',
73
+ 30 => "无法进行有效的翻译",
74
+ 40 => "不支持的语言类型",
75
+ 50 => "无效的key",
76
+ 60 => "无词典结果,仅在获取词典结果生效"
77
+ }
78
+ end
79
+
80
+ Lita.register_handler(self)
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "lita-youdao"
3
+ spec.version = "0.1.0"
4
+ spec.authors = ["easonhan"]
5
+ spec.email = ["nbkhic@qq.com"]
6
+ spec.description = "Translation using youdao api--http://fanyi.youdao.com/openapi"
7
+ spec.summary = "Translation using youdao api--http://fanyi.youdao.com/openapi"
8
+ spec.homepage = "https://github.com/easonhan007/lita-youdao"
9
+ spec.license = "TODO: Add a license"
10
+ spec.metadata = { "lita_plugin_type" => "handler" }
11
+
12
+ spec.files = `git ls-files`.split($/)
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ["lib"]
16
+
17
+ spec.add_runtime_dependency "lita", ">= 4.6"
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.3"
20
+ spec.add_development_dependency "pry-byebug"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rack-test"
23
+ spec.add_development_dependency "rspec", ">= 3.0.0"
24
+ end
@@ -0,0 +1,4 @@
1
+ en:
2
+ lita:
3
+ handlers:
4
+ youdao:
@@ -0,0 +1,12 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Handlers::Youdao, lita_handler: true do
4
+ it {is_expected.to route_command("youdao this is a book").to(:translate)}
5
+
6
+ it 'should get content form yoodao' do
7
+ Lita.config.handlers.youdao.api_key = ENV['YOUDAO_API_KEY']
8
+ Lita.config.handlers.youdao.key_from = ENV['YOUDAO_KEY_FROM']
9
+ send_command('youdao alias')
10
+ puts replies.last
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ require "lita-youdao"
2
+ require "lita/rspec"
3
+
4
+ # A compatibility mode is provided for older plugins upgrading from Lita 3. Since this plugin
5
+ # was generated with Lita 4, the compatibility mode should be left disabled.
6
+ Lita.version_3_compatibility_mode = false
File without changes
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lita-youdao
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - easonhan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lita
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
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: rake
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
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 3.0.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 3.0.0
97
+ description: Translation using youdao api--http://fanyi.youdao.com/openapi
98
+ email:
99
+ - nbkhic@qq.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - README.md
107
+ - Rakefile
108
+ - lib/lita-youdao.rb
109
+ - lib/lita/handlers/youdao.rb
110
+ - lita-youdao.gemspec
111
+ - locales/en.yml
112
+ - spec/lita/handlers/youdao_spec.rb
113
+ - spec/spec_helper.rb
114
+ - templates/.gitkeep
115
+ homepage: https://github.com/easonhan007/lita-youdao
116
+ licenses:
117
+ - 'TODO: Add a license'
118
+ metadata:
119
+ lita_plugin_type: handler
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.2.2
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Translation using youdao api--http://fanyi.youdao.com/openapi
140
+ test_files:
141
+ - spec/lita/handlers/youdao_spec.rb
142
+ - spec/spec_helper.rb
143
+ has_rdoc: