Class: Egd::PgnParser
- Inherits:
-
Object
- Object
- Egd::PgnParser
- Defined in:
- lib/egd/pgn_parser.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
This service takes in a PGN string and parses it Returns the game tags (headers of the PGN file) and the actual moves made in SAN.
-
#pgn_content ⇒ Object
readonly
This service takes in a PGN string and parses it Returns the game tags (headers of the PGN file) and the actual moves made in SAN.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(pgn_content) ⇒ PgnParser
constructor
A new instance of PgnParser.
Constructor Details
#initialize(pgn_content) ⇒ PgnParser
Returns a new instance of PgnParser.
10 11 12 13 14 15 |
# File 'lib/egd/pgn_parser.rb', line 10 def initialize(pgn_content) @pgn_content = pgn_content @headers = [] @movelist = [] @game_attributes = {} end |
Instance Attribute Details
#headers ⇒ Object (readonly)
This service takes in a PGN string and parses it Returns the game tags (headers of the PGN file) and the actual moves made in SAN
8 9 10 |
# File 'lib/egd/pgn_parser.rb', line 8 def headers @headers end |
#pgn_content ⇒ Object (readonly)
This service takes in a PGN string and parses it Returns the game tags (headers of the PGN file) and the actual moves made in SAN
8 9 10 |
# File 'lib/egd/pgn_parser.rb', line 8 def pgn_content @pgn_content end |
Instance Method Details
#call ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/egd/pgn_parser.rb', line 17 def call current_index = 0 state = :initial buffer = '' while (current_index < @pgn_content.size) current_char = @pgn_content[current_index] current_index += 1 if state == :initial if current_char == '[' state = :start_parse_header next elsif (current_char == ' ' || current_char == "\n" || current_char == "\r") next else break end end if state == :start_parse_header if current_char == ']' state = :initial hd = parse_header(buffer) @headers << hd @game_attributes[hd[:type]] = hd[:value] buffer = '' next else buffer << current_char next end end end @movelist = simple_parse_moves hash = {moves: @movelist} hash.merge!(game_tags: @game_attributes) if @game_attributes.any? hash end |