Class: Rocrad

Inherits:
Object
  • Object
show all
Defined in:
lib/rocrad.rb,
lib/rocrad/mixed.rb,
lib/rocrad/binary.rb,
lib/rocrad/errors.rb

Direct Known Subclasses

Binary, Mixed

Defined Under Namespace

Classes: Binary, ImageNotSelectedError, Mixed, UnsupportedFileTypeError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src = "") ⇒ Rocrad

Returns a new instance of Rocrad.



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

def initialize(src="")
  @uid = UUID.new
  @src = build_source src
  @txt = ""
  @tmp = nil
end

Instance Attribute Details

#srcObject

Returns the value of attribute src.



13
14
15
# File 'lib/rocrad.rb', line 13

def src
  @src
end

#tmpObject (readonly)

Returns the value of attribute tmp.



14
15
16
# File 'lib/rocrad.rb', line 14

def tmp
  @tmp
end

#txtObject (readonly)

Returns the value of attribute txt.



14
15
16
# File 'lib/rocrad.rb', line 14

def txt
  @txt
end

Instance Method Details

#crop!(x, y, w, h) ⇒ Object

Crop image to convert



46
47
48
49
50
51
52
53
# File 'lib/rocrad.rb', line 46

def crop!(x, y, w, h)
  @txt = ""
  src  = Magick::Image.read(@src.to_s).first
  src.crop!(x, y, w, h)
  @tmp = Pathname.new(Dir::tmpdir).join("#{@uid.generate}_#{@src.sub(@src.extname, "-crop#{@src.extname}").basename}")
  src.write @tmp.to_s
  self
end

#ocr!Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rocrad.rb', line 28

def ocr!
  if @src.instance_of? Pathname and @src.file?
    ocr_via_path
    @txt
  elsif @src.instance_of? URI::HTTP
    ocr_via_http
    @txt
  else
    raise ImageNotSelectedError
  end
end

#to_sObject

Output value



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

def to_s
  @txt != "" ? @txt : ocr!
end