Module: Wolf::Stomach

Extended by:
Stomach
Included in:
Stomach
Defined in:
lib/wolf/stomach.rb

Instance Method Summary collapse

Instance Method Details

#digest(result, options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wolf/stomach.rb', line 5

def digest(result, options)
  body = ''
  pods = options[:all] ? result.pods :
    result.pods.reject {|e| e.title == 'Input interpretation' || e.plaintext == '' }
  pods = pods.select {|e| e.title[/#{options[:title]}/i] } if options[:title]
  # multiple 1-row tables i.e. math results
  if pods.all? {|e| !e.plaintext.include?('|') }
    body << render_table(pods.map {|e| [e.title, e.plaintext] })
  # one one-row table i.e. word results
  elsif pods.size == 1 && pod_rows(pods[0]).size == 1
    body << pods[0].title.capitalize << "\n"
    body << render_table(pod_rows(pods[0])[0])
  else
    pods.each do |pod|
      body << pod.title.capitalize << "\n"

      # Handle multiple tables divided by graphs i.e. when comparing stocks
      if pod.plaintext.include?("\n\n") && pod.states.empty?
        strip(pod.plaintext).split(/\n{2,}/).each {|text|
          body << render_pod_rows(text_rows(text), text, options)
        }
      else
        body << render_pod_rows(pod_rows(pod), strip(pod.plaintext), options)
      end
    end
  end
  body
end

#pod_rows(pod) ⇒ Object



45
46
47
# File 'lib/wolf/stomach.rb', line 45

def pod_rows(pod)
  text_rows strip(pod.plaintext)
end

#render_pod_rows(rows, text, options) ⇒ Object



34
35
36
37
38
39
# File 'lib/wolf/stomach.rb', line 34

def render_pod_rows(rows, text, options)
  # delete comments
  rows.delete_if {|e| e.size == 1 } if rows.size > 1 && !options[:all]
  headers = text[/^\s*\|/] ? rows.shift : false
  render_table(rows, headers) << "\n\n"
end

#render_table(rows, headers = false) ⇒ Object



41
42
43
# File 'lib/wolf/stomach.rb', line 41

def render_table(rows, headers=false)
  Hirb::Helpers::AutoTable.render(rows, :description => false, :headers => headers)
end

#strip(text) ⇒ Object



49
50
51
# File 'lib/wolf/stomach.rb', line 49

def strip(text)
  text.sub(/\A(\s+\|){2,}/m, '').sub(/(\s+\|){2,}\s*\Z/, '').strip
end

#text_rows(text) ⇒ Object



53
54
55
# File 'lib/wolf/stomach.rb', line 53

def text_rows(text)
  text.split(/\n+/).map {|e| e.split(/\s*\|\s+/) }
end