Module: JsonSpec::Helpers
- Extended by:
- Helpers
- Included in:
- Helpers, Matchers::BeJsonEql, Matchers::HaveJsonPath, Matchers::HaveJsonSize, Matchers::HaveJsonType, Matchers::IncludeJson
- Defined in:
- lib/json_spec/helpers.rb
Instance Method Summary collapse
- #generate_normalized_json(ruby) ⇒ Object
- #load_json(relative_path) ⇒ Object
- #normalize_json(json, path = nil) ⇒ Object
- #parse_json(json, path = nil) ⇒ Object
Instance Method Details
#generate_normalized_json(ruby) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/json_spec/helpers.rb', line 19 def generate_normalized_json(ruby) case ruby when Hash, Array then JSON.pretty_generate(ruby) else ruby.to_json end end |
#load_json(relative_path) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/json_spec/helpers.rb', line 26 def load_json(relative_path) missing_json_directory! if JsonSpec.directory.nil? path = File.join(JsonSpec.directory, relative_path) missing_json_file!(path) unless File.exist?(path) File.read(path) end |
#normalize_json(json, path = nil) ⇒ Object
14 15 16 17 |
# File 'lib/json_spec/helpers.rb', line 14 def normalize_json(json, path = nil) ruby = parse_json(json, path) generate_normalized_json(ruby) end |
#parse_json(json, path = nil) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/json_spec/helpers.rb', line 7 def parse_json(json, path = nil) ruby = MultiJson.decode("[#{json}]").first value_at_json_path(ruby, path) rescue MultiJson::DecodeError MultiJson.decode(json) end |