fy 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 486c79dbc7ba58fb0c39eb345d0f0ab44106f764
4
+ data.tar.gz: e83cee2f16f3ee354e4560f7cb6bfcf0f2ae52b6
5
+ SHA512:
6
+ metadata.gz: 6f88b86a61a6330dac8c419975dd38f765a5b24f61dd5cd0d94730272f4ed374b2850af89747b91e88f977269c65210cd4b70d6998545a3a97ff4fcfbac10710
7
+ data.tar.gz: aa8518654f9a0cc149c1609099ee39fd5a64c4a24898dc2d91a9d79e0f4b7722c0446c1f00858b96e48f3ae0a09ed2609d4e2af8cdeddc349c8860d065fb4e71
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://ruby.taobao.org'
2
+
3
+ # Specify your gem's dependencies in fanyi.gemspec
4
+ gemspec
5
+ gem 'rainbow'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 JIANG Di
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.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # Fy
2
+
3
+ A translate tool in your command line, inspired by [Fanyi](https://github.com/afc163/fanyi).
4
+
5
+ ![](http://dd-personal.qiniudn.com/Screenshot from 2014-11-25 17:21:23.png)
6
+
7
+ ---
8
+
9
+ ## Installation
10
+ ```bash
11
+ $ gem install fy
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ Translation data is fetched from [fanyi.youdao.com](http://fanyi.youdao.com),
17
+ and only support translation between Chinese and English.
18
+
19
+ Translate one word.
20
+
21
+ ```bash
22
+ $ fy love
23
+ ```
24
+
25
+ ```
26
+ love [ lʌv ]
27
+
28
+ - n. 恋爱;亲爱的;酷爱;喜爱的事物;爱情,爱意;疼爱;热爱;爱人,所爱之物
29
+ - v. 爱,热爱;爱戴;赞美,称赞;喜爱;喜好;喜欢;爱慕
30
+ - n. (英)洛夫(人名)
31
+
32
+ 1. Love
33
+ 爱,爱情,恋爱
34
+ 2. Endless Love
35
+ 无尽的爱,不了情,蓝色生死恋
36
+ 3. puppy love
37
+ 早恋,青春期恋爱,初恋
38
+ ```
39
+
40
+ More words.
41
+
42
+ ```bash
43
+ $ fy make love
44
+ ```
45
+
46
+ Support Chinese, even sentence.
47
+
48
+ ```bash
49
+ $ fy 和谐
50
+ ```
51
+
52
+ ```bash
53
+ $ fy 子非鱼焉知鱼之乐
54
+ ```
55
+
56
+ ## LICENSE
57
+
58
+ MIT.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/fy ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'fy/fanyi_result'
4
+
5
+ help = <<HELP
6
+ fanyi.rb: Translate tools in the command line
7
+ $ fy word
8
+ $ fy world peace
9
+ $ fy chinglish
10
+ HELP
11
+
12
+ if ARGV.empty?
13
+ puts help
14
+ else
15
+ fy_result = Fy::FanyiResult.new(ARGV)
16
+ fy_result.puts_result
17
+ end
data/fy.gemspec ADDED
@@ -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 'fy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'fy'
8
+ spec.version = Fy::VERSION
9
+ spec.authors = ['JIANG Di']
10
+ spec.email = ['dd19941105@gmail.com']
11
+ spec.summary = %q{A translate tool in your command line}
12
+ spec.homepage = 'https://github.com/dd1994/fy'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = '>=2.1.5'
15
+ spec.platform = Gem::Platform::RUBY
16
+ spec.has_rdoc = false
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = ['fy']
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.7'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_runtime_dependency 'rainbow'
25
+ end
@@ -0,0 +1,133 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+ require 'json'
4
+ require 'rainbow/ext/string'
5
+
6
+ module Fy
7
+ class FanyiResult
8
+
9
+ attr_reader :words_array
10
+
11
+ def initialize(words_array)
12
+ @words_array = words_array
13
+ end
14
+
15
+ def api_url
16
+ 'http://fanyi.youdao.com/openapi.do?keyfrom=JIANGDi&key=891853312&type=data&doctype=json&version=1.1&q='
17
+ end
18
+
19
+ def query_words
20
+ words_array.join(' ')
21
+ end
22
+
23
+ def escape_words
24
+ URI.escape(words_array.join('+'))
25
+ end
26
+
27
+ def query_url
28
+ api_url + escape_words
29
+ end
30
+
31
+ def query_uri
32
+ URI(query_url)
33
+ end
34
+
35
+ def result_json
36
+ Net::HTTP.get(query_uri)
37
+ end
38
+
39
+ def result_hash
40
+ JSON.parse(result_json)
41
+ end
42
+
43
+ def yd_translations_array
44
+ result_hash['translation']
45
+ end
46
+
47
+ def fancy_translation(yd_translation)
48
+ " #{yd_translation}".color(:green)
49
+ end
50
+
51
+ def puts_yd_translations
52
+ yd_translations_array.each do |translation|
53
+ puts fancy_translation(translation)
54
+ end
55
+ puts
56
+ end
57
+
58
+ def dict_info
59
+ result_hash['basic']
60
+ end
61
+
62
+ def phonetic
63
+ dict_info['phonetic'] if dict_info
64
+ end
65
+
66
+ def fancy_phonetic
67
+ " [ #{phonetic} ]".color(:magenta)
68
+ end
69
+
70
+ def puts_first_line
71
+ first_line = ' '
72
+ first_line += query_words
73
+ first_line += fancy_phonetic if phonetic
74
+ puts first_line, ''
75
+ end
76
+
77
+ def dict_explains_array
78
+ dict_info['explains'] if dict_info
79
+ end
80
+
81
+ def fancy_dict_explain(explain)
82
+ ' - ' + "#{explain}".color(:green)
83
+ end
84
+
85
+ def puts_dict_explains
86
+ dict_explains_array.each do |explain|
87
+ puts fancy_dict_explain(explain)
88
+ end
89
+ puts
90
+ end
91
+
92
+ def web_result_array
93
+ result_hash['web']
94
+ end
95
+
96
+ def fancy_web_result_value(web_result_value)
97
+ ' ' + web_result_value.join(', ').color(:cyan)
98
+ end
99
+
100
+ def highlight_words(sentence, words)
101
+ sentence.gsub(/#{words}/i, words.color(:yellow))
102
+ end
103
+
104
+ def fancy_web_result_key(web_result_key)
105
+ highlight_words(web_result_key, query_words)
106
+ end
107
+
108
+ def puts_web_result_array
109
+ web_result_array.each_with_index do |web_result_hash, i|
110
+ puts " #{i+1}. #{fancy_web_result_key(web_result_hash['key'])}"
111
+ puts fancy_web_result_value(web_result_hash['value'])
112
+ end
113
+ puts
114
+ end
115
+
116
+ # 一般来说,有道词典解释的比较好
117
+ # 但是长一点的句子有道词典没有结果,需要用有道翻译
118
+ # 所以如果有道词典有结果就只用词典的结果,否则用有道翻译
119
+ def puts_yd_result
120
+ if dict_info.nil?
121
+ puts_yd_translations
122
+ else
123
+ puts_dict_explains
124
+ end
125
+ end
126
+
127
+ def puts_result
128
+ puts_first_line
129
+ puts_yd_result
130
+ puts_web_result_array if web_result_array
131
+ end
132
+ end
133
+ end
data/lib/fy/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Fy
2
+ VERSION = '0.0.5'
3
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - JIANG Di
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-28 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rainbow
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - dd19941105@gmail.com
58
+ executables:
59
+ - fy
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/fy
69
+ - fy.gemspec
70
+ - lib/fy/fanyi_result.rb
71
+ - lib/fy/version.rb
72
+ homepage: https://github.com/dd1994/fy
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 2.1.5
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.4.4
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: A translate tool in your command line
96
+ test_files: []
97
+ has_rdoc: false