Skip to content

Instantly share code, notes, and snippets.

@MiroslavCsonka
Last active November 28, 2015 23:25
Show Gist options
  • Save MiroslavCsonka/6919b62c222bf76ba13f to your computer and use it in GitHub Desktop.
Save MiroslavCsonka/6919b62c222bf76ba13f to your computer and use it in GitHub Desktop.
json expression example
class HashBuilder
def self.build &block
hb = HashBuilder.new
hb.instance_eval(&block)
hb.hash
end
attr_reader :hash
def initialize
@hash = {}
end
def method_missing meth, *args, &block
@hash[meth] = block ? HashBuilder.build(&block) : args.first
end
end
class Schemas
class << self
def game(&block)
attrs = { _links: {}.ignore_extra_keys,
id: Fixnum,
name: String,
players: [player]
}
if block_given?
attrs.merge(HashBuilder.build(&block))
else
attrs
end
end
def player(&block)
attrs = { _links: {}.ignore_extra_keys,
id: Fixnum,
name: String,
email: String
}
if block_given?
attrs.merge(HashBuilder.build(&block))
else
attrs
end
end
end
end
# Usage
expect(JSON.parse(response.body)).to match_json_expression(Schemas.game do
id 12
players [ Schemas.player do
name "some name"
end]
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment