Class: Euston::Websites::ApiCommandRequestParsing::AbstractParser

Inherits:
Object
  • Object
show all
Includes:
Hollywood
Defined in:
lib/euston-websites/api_command_request_parsing/abstract_parser.rb

Direct Known Subclasses

BatchParser, DiscreteParser

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ AbstractParser

Returns a new instance of AbstractParser.



7
8
9
# File 'lib/euston-websites/api_command_request_parsing/abstract_parser.rb', line 7

def initialize request
  @body = request.body.read
end

Instance Method Details

#parseObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/euston-websites/api_command_request_parsing/abstract_parser.rb', line 11

def parse
  valid = true

  begin
    commands = [transform_hash_to_commands(ActiveSupport::JSON.decode(@body))].flatten
  rescue StandardError => e
    callback :not_parseable
    valid = false
  end

  return unless valid

  invalid_commands = commands.reject { |c| c.valid? }

  if invalid_commands.any?
    callback :invalid, invalid_commands
  else
    callback :valid, commands
  end
end