Class: Shoryuken::BodyParser

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

Overview

Parses SQS message bodies according to worker configuration. Supports JSON parsing, text extraction, custom Procs, and any object that responds to parse or load methods.

Class Method Summary collapse

Class Method Details

.parse(worker_class, sqs_msg) ⇒ Object

Parses the body of an SQS message according to the worker’s body_parser option

Parameters:

  • worker_class (Class)

    the worker class with shoryuken options

  • sqs_msg (Shoryuken::Message)

    the SQS message to parse

Returns:

  • (Object)

    the parsed message body



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/shoryuken/body_parser.rb', line 14

def parse(worker_class, sqs_msg)
  body_parser = worker_class.get_shoryuken_options['body_parser']

  case body_parser
  when :json
    JSON.parse(sqs_msg.body)
  when Proc
    body_parser.call(sqs_msg)
  when :text, nil
    sqs_msg.body
  else
    if body_parser.respond_to?(:parse)
      # JSON.parse
      body_parser.parse(sqs_msg.body)
    elsif body_parser.respond_to?(:load)
      # see https://github.com/ruby-shoryuken/shoryuken/pull/91
      # JSON.load
      body_parser.load(sqs_msg.body)
    end
  end
end