Class: BingDictionary::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/bing_dictionary.rb

Constant Summary collapse

COLORS =
%i[ black red green yellow blue magenta cyan white ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word, options = {}) ⇒ Base

Returns a new instance of Base.



31
32
33
34
35
36
# File 'lib/bing_dictionary.rb', line 31

def initialize(word, options = {})
  body = self.request(word)
  self.word = word
  self.doc = Nokogiri::HTML(body)
  self.options = options
end

Instance Attribute Details

#docObject

Returns the value of attribute doc.



10
11
12
# File 'lib/bing_dictionary.rb', line 10

def doc
  @doc
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/bing_dictionary.rb', line 10

def options
  @options
end

#wordObject

Returns the value of attribute word.



10
11
12
# File 'lib/bing_dictionary.rb', line 10

def word
  @word
end

Class Method Details

.translate(word, options = {}) ⇒ Object



12
13
14
15
16
17
# File 'lib/bing_dictionary.rb', line 12

def self.translate(word, options = {})
  self.new(word, options).translate
rescue SocketError
  warn 'Connection failed! Please check your network.'
  exit 1
end

Instance Method Details

#guessObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bing_dictionary.rb', line 76

def guess
  puts
  content = doc.at_css('.content')
  puts content.at_css('.p2-2').text
  puts

  content.css('.dym_area').each do |area|
    puts area.at_css('.df_wb_a').text
    puts area.css('.df_wb_c').map(&:text)
    puts
  end
end

#headObject



46
47
48
49
50
51
52
53
# File 'lib/bing_dictionary.rb', line 46

def head
  puts doc.at_css('#headword').text
  puts with_color(doc.at_css('.hd_tf_lh').text, :green)
  puts
  doc.at_css('.hd_area').next_sibling.css('li').each do |li|
    puts with_color(with_fixed(li.at_css('.pos').text, 5), :blue) + li.at_css('.def').text
  end
end

#machineObject



61
62
63
64
65
# File 'lib/bing_dictionary.rb', line 61

def machine
  puts doc.at_css('.smt_hw').text
  puts doc.at_css('.p1-10').text
  puts with_color(doc.at_css('.p1-11').text, :green)
end

#pronounceObject



55
56
57
58
59
# File 'lib/bing_dictionary.rb', line 55

def pronounce
  url = doc.at_css('.hd_tf_lh .hd_tf a').attr('onclick').match(/http.*mp3/)
  `curl -o /tmp/dict.mp3 #{url} &> /dev/null`
  `afplay /tmp/dict.mp3`
end

#request(word) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bing_dictionary.rb', line 19

def request(word)
  url = URI("https://cn.bing.com/dict/search?q=#{CGI::escape(word)}")

  https = Net::HTTP.new(url.host, url.port)
  https.use_ssl = true
  request = Net::HTTP::Get.new(url)
  request["Cookie"] = "_EDGE_S=mkt=zh-cn;"

  response = https.request(request)
  return response.read_body
end

#sentenceObject



67
68
69
70
71
72
73
74
# File 'lib/bing_dictionary.rb', line 67

def sentence
  puts
  doc.css('#sentenceSeg .se_li').first(4).map do |li|
    puts with_highlight(li.css('.sen_en').text, word)
    puts li.css('.sen_cn').text
    puts
  end
end

#translateObject



38
39
40
41
42
43
44
# File 'lib/bing_dictionary.rb', line 38

def translate
  head if doc.at_css('#headword')
  machine if doc.at_css('.smt_hw')
  sentence if doc.at_css('#sentenceSeg .se_li')
  guess if doc.at_css('.dym_area')
  pronounce if doc.at_css('#headword') && options[:pronounce]
end

#with_color(str, color) ⇒ Object



90
91
92
# File 'lib/bing_dictionary.rb', line 90

def with_color(str, color)
  "\e[3#{COLORS.index(color)}m" << str.to_s << "\e[0m"
end

#with_fixed(str, width) ⇒ Object



94
95
96
97
# File 'lib/bing_dictionary.rb', line 94

def with_fixed(str, width)
  width = width - str.each_char.count { |c| c =~ /\p{Han}/ }
  width > 0 ? ("%-#{width}s" % str) : str
end

#with_highlight(str, keyword) ⇒ Object



99
100
101
# File 'lib/bing_dictionary.rb', line 99

def with_highlight(str, keyword)
  str.gsub(/#{keyword}/i) { |s| with_color(s, :yellow) }
end