Class: WWWJDic::Splitter

Inherits:
Object
  • Object
show all
Defined in:
lib/wwwjdic/utils/splitter.rb

Overview

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

Author

Marco Bresciani

Copyright

Copyright © 2014, 2015, 2016, 2017, 2018, 2019 Marco Bresciani

License

GNU General Public License version 3

Instance Method Summary collapse

Constructor Details

#initialize(translation) ⇒ Splitter

Creates a Splitter object.

Usage

new_splitter = Splitter.new translation_content

Returns

a Splitter object.



37
38
39
# File 'lib/wwwjdic/utils/splitter.rb', line 37

def initialize(translation)
  @translation = translation.force_encoding('UTF-8')
end

Instance Method Details

#contentObject



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

#linesObject



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

#messageObject



49
50
51
52
# File 'lib/wwwjdic/utils/splitter.rb', line 49

def message
  return $1.strip if /<p>(.*)<p>/m.match @translation unless @translation.nil?
  $1 if /<p>(.*)<pre>/m.match @translation if @translation.nil?
end

#titleObject



41
42
43
# File 'lib/wwwjdic/utils/splitter.rb', line 41

def title
  $1 if /<TITLE>(.*)<\/TITLE>/.match @translation
end

#translationObject



45
46
47
# File 'lib/wwwjdic/utils/splitter.rb', line 45

def translation
  $1.strip if /<pre>(.*)<\/pre>/m.match @translation
end