Class: EightBall::Parsers::Json
- Inherits:
-
Object
- Object
- EightBall::Parsers::Json
- Defined in:
- lib/eight_ball/parsers/json.rb
Overview
A JSON parser will parse JSON into a list of Features. The top-level JSON element must be an array and must use camel-case; this will be converted to snake-case by EightBall.
Below are some examples of valid JSON:
Instance Method Summary collapse
-
#parse(json) ⇒ Array<EightBall::Feature>
Convert the JSON into a list of Features.
Instance Method Details
#parse(json) ⇒ Array<EightBall::Feature>
Convert the JSON into a list of Features.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/eight_ball/parsers/json.rb', line 45 def parse(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 feature[:enabled_for] disabled_for = create_conditions 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.}" } [] end |