class RSpec::Oj::Matchers::HaveJsonType

Public Class Methods

new(type) click to toggle source
# File lib/rspec/oj/matchers/have_json_type.rb, line 10
def initialize(type)
  @classes = type_to_classes(type)
  @path = nil
end

Public Instance Methods

at_path(path) click to toggle source
# File lib/rspec/oj/matchers/have_json_type.rb, line 20
def at_path(path)
  @path = path
  self
end
description() click to toggle source
# File lib/rspec/oj/matchers/have_json_type.rb, line 33
def description
  message_with_path(%(have JSON type "#{@classes.join(', ')}"))
end
failure_message() click to toggle source
# File lib/rspec/oj/matchers/have_json_type.rb, line 25
def failure_message
  message_with_path("Expected JSON value type to be #{@classes.join(', ')}, got #{@ruby.class}")
end
failure_message_when_negated() click to toggle source
# File lib/rspec/oj/matchers/have_json_type.rb, line 29
def failure_message_when_negated
  message_with_path("Expected JSON value type to not be #{@classes.join(', ')}, got #{@ruby.class}")
end
matches?(json) click to toggle source
# File lib/rspec/oj/matchers/have_json_type.rb, line 15
def matches?(json)
  @ruby = parse_json(json, @path)
  @classes.any? { |c| c === @ruby }
end

Private Instance Methods

type_to_classes(type) click to toggle source
# File lib/rspec/oj/matchers/have_json_type.rb, line 39
def type_to_classes(type)
  case type
  when Class then [type]
  when Array then type.map { |t| type_to_classes(t) }.flatten
  else
    case type.to_s.downcase
    when 'boolean'     then [TrueClass, FalseClass]
    when 'object'      then [Hash]
    when 'nil', 'null' then [NilClass]
    else [Module.const_get(type.to_s.capitalize)]
    end
  end
end