Class: WWWJDic::Splitter
- Inherits:
-
Object
- Object
- WWWJDic::Splitter
- Defined in:
- lib/wwwjdic/utils/splitter.rb
Overview
This class is a simple API to interact with WWWJDic Backboor Entry/API.
- Author
- Copyright
-
Copyright © 2014, 2015, 2016, 2017, 2018, 2019 Marco Bresciani
- License
-
GNU General Public License version 3
Instance Method Summary collapse
- #content ⇒ Object
-
#initialize(translation) ⇒ Splitter
constructor
Creates a
Splitterobject. - #lines ⇒ Object
- #message ⇒ Object
- #title ⇒ Object
- #translation ⇒ Object
Constructor Details
#initialize(translation) ⇒ Splitter
Creates a Splitter object.
- Usage
-
new_splitter = Splitter.new translation_content - Returns
-
a
Splitterobject.
37 38 39 |
# File 'lib/wwwjdic/utils/splitter.rb', line 37 def initialize(translation) @translation = translation.force_encoding('UTF-8') end |
Instance Method Details
#content ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/wwwjdic/utils/splitter.rb', line 63 def content result = Array.new lines.each do |a_line| inner = Hash.new if /(.*)\[/m.match a_line inner[:word] = $1.strip inner[:kana] = $1.strip if /\[(.*)\]/m.match a_line inner[:text] = $1.strip if /\] \/(.*)\//m.match a_line else inner[:kana] = $1.strip if /^(.*)(\s)\//m.match a_line inner[:word] = inner[:kana] inner[:text] = $1.strip if /\/(.*)\//m.match a_line end inner[:meanings] = inner[:text].split('/') if inner[:text].include? '/' result.push inner unless inner.empty? end result unless result.empty? end |
#lines ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/wwwjdic/utils/splitter.rb', line 54 def lines result = Array.new translation.each_line do |a_line| stripped_line = a_line.strip result.push stripped_line unless stripped_line.empty? end result unless result.empty? end |
#message ⇒ Object
49 50 51 52 |
# File 'lib/wwwjdic/utils/splitter.rb', line 49 def return $1.strip if /<p>(.*)<p>/m.match @translation unless @translation.nil? $1 if /<p>(.*)<pre>/m.match @translation if @translation.nil? end |
#title ⇒ Object
41 42 43 |
# File 'lib/wwwjdic/utils/splitter.rb', line 41 def title $1 if /<TITLE>(.*)<\/TITLE>/.match @translation end |
#translation ⇒ Object
45 46 47 |
# File 'lib/wwwjdic/utils/splitter.rb', line 45 def translation $1.strip if /<pre>(.*)<\/pre>/m.match @translation end |