Class: PNGrammar

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

Constant Summary collapse

URL =
"http://www-cgi.uni-regensburg.de/~brf09510/syntax.html"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ PNGrammar

Returns a new instance of PNGrammar.



7
8
9
10
# File 'lib/pngrammar.rb', line 7

def initialize(file)
  @ebnf = File.read(file).gsub(/\n/,' ')
  @ua = Mechanize.new
end

Instance Attribute Details

#ebnfObject (readonly)

Returns the value of attribute ebnf.



5
6
7
# File 'lib/pngrammar.rb', line 5

def ebnf
  @ebnf
end

#uaObject (readonly)

Returns the value of attribute ua.



5
6
7
# File 'lib/pngrammar.rb', line 5

def ua
  @ua
end

Instance Method Details

#imageObject



12
13
14
# File 'lib/pngrammar.rb', line 12

def image
  resize_image ua.get_file(results.at('img')['src'])
end

#resize_image(png) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pngrammar.rb', line 23

def resize_image(png)
  Tempfile.open('pngrammar') do |tempfile|
    tempfile.print png
    tempfile.close
    return begin
      `convert png:#{tempfile.path} -strip -trim -bordercolor White -border 10x10 -`
    rescue Errno::ENOENT
      tempfile.read
    end.force_encoding('binary')
  end
end

#resultsObject



16
17
18
19
20
21
# File 'lib/pngrammar.rb', line 16

def results
  @results ||= ua.get(URL).forms.first.tap do |f| 
    f['ssyntax'] = ebnf
    f['width'] = 1000
  end.submit.parser
end