class WWWJDic::Splitter

This class is a simple API to interact with WWWJDic Backboor Entry/API.

Author

Marco Bresciani

Copyright

© 2014-2021 Marco Bresciani

License

GNU General Public License version 3

Public Class Methods

new(translation) click to toggle source

Creates a Splitter object.

Usage

new_splitter = Splitter.new translation_content

Returns

a Splitter object.

# File lib/wwwjdic/utils/splitter.rb, line 44
def initialize(translation)
  @translation = translation.force_encoding('UTF-8')
end

Public Instance Methods

content() click to toggle source

@return [Array] the arrays of elements containing the answer

# File lib/wwwjdic/utils/splitter.rb, line 72
def content
  result = []
  lines.each do |a_line|
    inner = {}
    fill_inner_data(a_line, inner)
    inner[:meanings] = inner[:text].split('/') if inner[:text].include? '/'
    result.push inner unless inner.empty?
  end

  result unless result.empty?
end
lines() click to toggle source
# File lib/wwwjdic/utils/splitter.rb, line 62
def lines
  result = []
  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() click to toggle source
# File lib/wwwjdic/utils/splitter.rb, line 56
def message
  return Regexp.last_match(1).strip unless @translation.nil? || !/<p>(.*)<p>/m.match(@translation)

  Regexp.last_match(1) if @translation.nil? && /<p>(.*)<pre>/m.match(@translation)
end
title() click to toggle source
# File lib/wwwjdic/utils/splitter.rb, line 48
def title
  Regexp.last_match(1) if %r{<TITLE>(.*)</TITLE>}.match @translation
end
translation() click to toggle source
# File lib/wwwjdic/utils/splitter.rb, line 52
def translation
  Regexp.last_match(1).strip if %r{<pre>(.*)</pre>}m.match @translation
end

Private Instance Methods

fill_inner_data(a_line, inner) click to toggle source
# File lib/wwwjdic/utils/splitter.rb, line 86
def fill_inner_data(a_line, inner)
  if /(.*)\[/m.match a_line
    inner[:word] = Regexp.last_match(1).strip
    inner[:kana] = Regexp.last_match(1).strip if /\[(.*)\]/m.match a_line
    inner[:text] = Regexp.last_match(1).strip if %r{\] /(.*)/}m.match a_line
  else
    inner[:kana] = Regexp.last_match(1).strip if %r{^(.*)(\s)/}m.match a_line
    inner[:word] = inner[:kana]
    inner[:text] = Regexp.last_match(1).strip if %r{/(.*)/}m.match a_line
  end
end