Last active
November 28, 2015 23:25
-
-
Save MiroslavCsonka/6919b62c222bf76ba13f to your computer and use it in GitHub Desktop.
json expression example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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