fy 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9d40f45c8f927cbb0078290a7eb41ea0bf2fd306
4
- data.tar.gz: 3cc54a73e13493f11ea20a7534221662ee67bf4a
3
+ metadata.gz: 29d895380f7026ce8bd9dec799ea3e0d4ffbd389
4
+ data.tar.gz: b71bdb07fcd3c0a648b2b3d5d6edf554ae769268
5
5
  SHA512:
6
- metadata.gz: dccd1e49c411f805771a3843f69da9fe8e37a658997de3758bb763724dac17fa654c52d45e53534e2548962d996e2f99f0f4042944c44fe4f179fb68161fdd82
7
- data.tar.gz: 20e8bd88579bb40c61091c2b6f3b3a75936a9ae13777bb29c099327861cd26bbe12aa579b4dcbb3d7bbd7d02719edfd5972fd8f4e74c85bf1d8caecb40a7010d
6
+ metadata.gz: 9b8b25be46fbae8520c177c8b33abb9a2f5aa5af82d5ae19c1c07249134a5c568555d63636e1af9eef2277875bcf23036030b9cfbc122944bec7ddc79178cefe
7
+ data.tar.gz: adabe549e5b662e314e9b89a256aeb8cd64856d13d3a008a93ab3feb493e7edc8ebcf59fa92c10cb90e2555abfe887077f5d9d7cdc818223760594e04c62fd57
data/README.md CHANGED
@@ -43,6 +43,15 @@ More words.
43
43
  $ fy make love
44
44
  ```
45
45
 
46
+ ~~More more words.~~ Loop mode, CTRL+C to exit.
47
+ ```bash
48
+ $ fy
49
+ > words
50
+ > to
51
+ > translate
52
+ ```
53
+
54
+
46
55
  Support Chinese, even sentence.
47
56
 
48
57
  ```bash
data/Rakefile CHANGED
@@ -1 +1,9 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ task :default => :test
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.pattern = "test/**/test_*.rb"
9
+ end
data/bin/fy CHANGED
@@ -1,18 +1,30 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require_relative '../lib/fy/fanyi_result'
3
+ require_relative '../lib/fy/fanyi'
4
4
 
5
5
  help = <<HELP
6
6
  fanyi.rb: Translate tools in the command line
7
7
  $ fy word
8
8
  $ fy world peace
9
9
  $ fy chinglish
10
+ $ fy
11
+ > enter the loop mode, ctrl+c to exit
10
12
  HELP
11
13
 
12
14
  if ARGV.empty?
15
+ begin
16
+ loop do
17
+ print '> '
18
+ input = gets.chomp
19
+ # 输入为空时继续循环
20
+ next if input == ''
21
+ puts Fy::Fanyi.new(input).result
22
+ end
23
+ rescue Interrupt
24
+ puts 'bye~'
25
+ end
26
+ elsif %w{-h --help}.any?{ |c| ARGV.include? c }
13
27
  puts help
14
- exit 1
15
28
  else
16
- fy_result = Fy::FanyiResult.new(ARGV)
17
- fy_result.puts_result
29
+ puts Fy::Fanyi.new(ARGV.join(' ')).result
18
30
  end
data/fy.gemspec CHANGED
@@ -20,5 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.7"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
+ # for rubymine
24
+ spec.add_development_dependency "minitest-reporters", ">=1.0.8"
23
25
  spec.add_runtime_dependency "rainbow", ">= 2.0.0"
24
26
  end
@@ -0,0 +1,73 @@
1
+ require "uri"
2
+ require "net/http"
3
+ require "json"
4
+ require "rainbow/ext/string"
5
+
6
+ module Fy
7
+ class Fanyi
8
+
9
+ def initialize(input)
10
+ @words = input
11
+ query_for_hash
12
+ end
13
+
14
+ def api_url
15
+ "http://fanyi.youdao.com/openapi.do?keyfrom=JIANGDi&key=891853312&type=data&doctype=json&version=1.1&q="
16
+ end
17
+
18
+ def query_for_hash
19
+ query_url = api_url + URI.escape( @words.gsub(/ /, '+') )
20
+ result_json = Net::HTTP.get( URI(query_url) )
21
+ @result_hash = JSON.parse(result_json)
22
+ end
23
+
24
+ def translations
25
+ translations = @result_hash["translation"]
26
+ lines = translations.collect do |translation|
27
+ " " + translation.color(:green)
28
+ end
29
+ lines << ""
30
+ end
31
+
32
+ def word_and_phonetic
33
+ line = " " + @words
34
+ phonetic = @result_hash["basic"]["phonetic"] if @result_hash["basic"]
35
+ line += " [ #{phonetic} ]".color(:magenta) if phonetic
36
+ [line, ""]
37
+ end
38
+
39
+ def dict_explains
40
+ dict_explains = @result_hash["basic"]["explains"] if @result_hash["basic"]
41
+ lines = dict_explains.collect do |explain|
42
+ " - " + explain.color(:green)
43
+ end
44
+ lines << ""
45
+ end
46
+
47
+ def web_results
48
+ return [] unless @result_hash["web"]
49
+ lines = []
50
+ web_results = @result_hash["web"]
51
+ web_results.each_with_index do |web_result, i|
52
+ web_result_key = web_result["key"].gsub(/#{@words}/i, @words.color(:yellow))
53
+ web_result_value = web_result["value"].join(', ').color(:cyan)
54
+ lines << " #{i+1}. #{web_result_key}"
55
+ lines << " #{web_result_value}"
56
+ end
57
+ lines << ""
58
+ end
59
+
60
+ # 一般来说,有道词典解释的比较好
61
+ # 但是长一点的句子有道词典没有结果,需要用有道翻译
62
+ # 所以如果有道词典有结果就只用词典的结果,否则用有道翻译
63
+ def yd_result
64
+ @result_hash["basic"].nil? ? translations : dict_explains
65
+ end
66
+
67
+ def result
68
+ output = []
69
+ output << word_and_phonetic << yd_result << web_results
70
+ output.flatten
71
+ end
72
+ end
73
+ end
@@ -1,3 +1,3 @@
1
1
  module Fy
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - JIANG Di
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-04 00:00:00.000000000 Z
11
+ date: 2014-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest-reporters
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.8
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.8
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rainbow
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -68,7 +82,7 @@ files:
68
82
  - bin/fy
69
83
  - fy.gemspec
70
84
  - lib/fy.rb
71
- - lib/fy/fanyi_result.rb
85
+ - lib/fy/fanyi.rb
72
86
  - lib/fy/version.rb
73
87
  homepage: https://github.com/dd1994/fy
74
88
  licenses:
@@ -1,133 +0,0 @@
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