Class: EightBall::Marshallers::Json
- Inherits:
-
Object
- Object
- EightBall::Marshallers::Json
- Defined in:
- lib/eight_ball/marshallers/json.rb
Instance Method Summary collapse
-
#marshall(features) ⇒ String
Convert the given Features into a JSON array.
-
#unmarshall(json) ⇒ Array<EightBall::Feature>
Convert the given JSON into a list of Features.
Instance Method Details
#marshall(features) ⇒ String
Convert the given Features into a JSON array.
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.
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 |