Class: EightBall::Marshallers::Json

Inherits:
Object
  • Object
show all
Defined in:
lib/eight_ball/marshallers/json.rb

Instance Method Summary collapse

Instance Method Details

#marshall(features) ⇒ String

Convert the given Features into a JSON array.

Examples:

json_string = <Read from somewhere>

marshaller = EightBall::Marshallers::Json.new
marshaller.marshall [Array<EightBall::Feature>] => json

Parameters:

Returns:

  • (String)

    The resulting JSON string.



50
51
52
# File 'lib/eight_ball/marshallers/json.rb', line 50

def marshall(features)
  JSON.generate(features.map { |feature| feature_to_hash(feature).to_camelback_keys })
end

#unmarshall(json) ⇒ Array<EightBall::Feature>

Convert the given JSON into a list of Features.

Examples:

json_string = <Read from somewhere>

marshaller = EightBall::Marshallers::Json.new
marshaller.unmarshall json_string => [Features]

Parameters:

  • json (String)

    The JSON string to convert.

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/eight_ball/marshallers/json.rb', line 64

def unmarshall(json)
  parsed = JSON.parse(json, symbolize_names: true).to_snake_keys

  raise ArgumentError, 'JSON input was not an array' unless parsed.is_a? Array

  parsed.map do |feature|
    enabled_for = create_conditions_from_json feature[:enabled_for]
    disabled_for = create_conditions_from_json feature[:disabled_for]

    EightBall::Feature.new feature[:name], enabled_for, disabled_for
  end
rescue JSON::ParserError => e
  EightBall.logger.error { "Failed to parse JSON: #{e.message}" }
  []
end