Class: MicroMagick::IdentifyParser
- Inherits:
-
Object
- Object
- MicroMagick::IdentifyParser
- Defined in:
- lib/micro_magick/identify_parser.rb
Instance Attribute Summary collapse
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #indent(line) ⇒ Object
-
#initialize(stdout) ⇒ IdentifyParser
constructor
A new instance of IdentifyParser.
- #key_to_sym(key) ⇒ Object
- #parse ⇒ Object
- #split(line) ⇒ Object
Constructor Details
#initialize(stdout) ⇒ IdentifyParser
Returns a new instance of IdentifyParser.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/micro_magick/identify_parser.rb', line 6 def initialize(stdout) cleaned = stdout.encode('UTF-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '') @lines = cleaned.split("\n").select { |ea| ea =~ /\S+:/ } if @lines.empty? @results = {} else @results = parse[:image] # < remove the "image" prefix. if (m = /(\d+)x(\d+)/.match(@results[:geometry])) @results[:width], @results[:height] = m[1].to_i, m[2].to_i end end end |
Instance Attribute Details
#results ⇒ Object (readonly)
Returns the value of attribute results.
4 5 6 |
# File 'lib/micro_magick/identify_parser.rb', line 4 def results @results end |
Instance Method Details
#[](key) ⇒ Object
19 20 21 |
# File 'lib/micro_magick/identify_parser.rb', line 19 def [](key) results[key_to_sym(key)] end |
#indent(line) ⇒ Object
41 42 43 |
# File 'lib/micro_magick/identify_parser.rb', line 41 def indent(line) /\S/.match(line).begin(0) end |
#key_to_sym(key) ⇒ Object
50 51 52 |
# File 'lib/micro_magick/identify_parser.rb', line 50 def key_to_sym(key) key.is_a?(Symbol) ? key : key.strip.gsub(/[\b\W_-]+/, '_').downcase.to_sym end |
#parse ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/micro_magick/identify_parser.rb', line 23 def parse result = {} while true line = @lines.shift key, value = split(line) if @lines.first && indent(line) < indent(@lines.first) # The current line has a sub-section. result[key] = parse() else result[key] = value end # Next line is indented less than the current line: break if @lines.first.nil? || indent(line) > indent(@lines.first) end result end |
#split(line) ⇒ Object
45 46 47 48 |
# File 'lib/micro_magick/identify_parser.rb', line 45 def split(line) k, v = line.split(':', 2).map(&:strip) [key_to_sym(k), v] end |