Class: Sparrow::Strategies::JsonFormatStrategy Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/sparrow/strategies/json_format_strategies/json_format_strategy.rb

Overview

This class is abstract.

Not exactly a abstract class but contains no own logic but singleton class methods

Superclass for all JSON format strategies. Contains no own instance logic, but keeps track of the registration of all JSON format strategies with its Singleton class methods.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*_args) ⇒ JsonFormatStrategy

Empty constructor. Does nothing.



12
13
# File 'lib/sparrow/strategies/json_format_strategies/json_format_strategy.rb', line 12

def initialize(*_args)
end

Class Method Details

.convert(body) ⇒ String

Start a JSON conversion by its given string

Parameters:

  • body (Object)

    a JSON object representation. can be any type a JSON format strategy is registered, i.e. an Array, a String or a RackBody

Returns:

  • (String)

    the formatted JSON



31
32
33
34
35
36
# File 'lib/sparrow/strategies/json_format_strategies/json_format_strategy.rb', line 31

def self.convert(body)
  strategy = json_format_strategies.detect do |strategy_candidate|
    strategy_candidate.match?(body)
  end
  strategy.convert(body)
end

.register_json_format(*args) ⇒ Array

Register a new JSON Format strategy

Parameters:

  • args (Object)

    the arguments for the new strategy

Returns:

  • (Array)

    args the updated registered JSON Format strategies available



20
21
22
23
# File 'lib/sparrow/strategies/json_format_strategies/json_format_strategy.rb', line 20

def self.register_json_format(*args)
  init(args)
  @@json_format_strategies << self.new(args)
end