Class: Epuber::Epubcheck

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

Defined Under Namespace

Classes: Problem, Report

Class Method Summary collapse

Class Method Details

._parse_json(string) ⇒ Report

Parse json from epubcheck

Parameters:

  • string (String)

    json string

Returns:



65
66
67
68
69
70
71
72
73
# File 'lib/epuber/epubcheck.rb', line 65

def _parse_json(string)
  json = JSON.parse(string)
  messages = json['messages']
  problems = messages
             .map { |msg| _parse_locations(msg) }
             .flatten

  Report.new(problems: problems)
end

._parse_locations(json) ⇒ Array<Problem>

Parse all problems from single message

Parameters:

  • json (Hash)

Returns:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/epuber/epubcheck.rb', line 80

def _parse_locations(json)
  json['locations'].map do |json_location|
    location = Epuber::Location.new(
      path: json_location['path'],
      lineno: json_location['line'],
      column: json_location['column'],
    )

    Problem.new(
      level: json['severity'].downcase.to_sym,
      code: json['ID'],
      message: json['message'],
      location: location,
    )
  end
end

.check(path) ⇒ Report

Returns report of the epubcheck.

Parameters:

  • path (String)

    path to file

Returns:

  • (Report)

    report of the epubcheck



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/epuber/epubcheck.rb', line 46

def check(path)
  report = nil

  Dir.mktmpdir('epubcheck-') do |tmpdir|
    json_path = File.join(tmpdir, 'epubcheck.json')
    Open3.popen2('epubcheck', path, '--json', json_path) do |_stdin, _stdout, wait_thr|
      wait_thr.value # wait for the process to finish
      report = _parse_json(File.read(json_path))
    end
  end

  report
end