I implemented this for a new API I'm building and my tests caught what I think might be a bug in the case where your default is an earlier version but you want to make later versions available. For example, you have v1, v2, and v3. v2 is still the default and you're testing v3. Because the matcher goes in order, when it hits the default (v2) it will match to that, even if you specified 'v3' in the Accepts header.
Here is how I solved it. I'm a bit of a n00b, so I'm sure you can make it prettier/more efficient.
Created an ACTIVE_API_VERSIONS constant so I know whether the version specified is active, in which case I can ignore the default.
ruby
ACTIVE_API_VERSIONS = ['v0', 'v1']
Modified the class like so
ruby
classApiConstraintsdefinitialize(options)
@version = options[:version]
@default = options[:default]
enddefmatches?(req)
if req.headers['Accept'] =~ /application\/vnd.example.v([0-9]+)/
ver = $1.to_i
ACTIVE_API_VERSIONS.include?("v#{ver}") ? @version == ver : @defaultelse@defaultend# @default || req.headers['Accept'].include?("application/vnd.linguixlabs.v#{@version}")endend
Typo in ASCII-Cast
gsg-keypair.gem should be gsg-keypair.pem
I implemented this for a new API I'm building and my tests caught what I think might be a bug in the case where your default is an earlier version but you want to make later versions available. For example, you have v1, v2, and v3. v2 is still the default and you're testing v3. Because the matcher goes in order, when it hits the default (v2) it will match to that, even if you specified 'v3' in the Accepts header.
Here is how I solved it. I'm a bit of a n00b, so I'm sure you can make it prettier/more efficient.
Created an ACTIVE_API_VERSIONS constant so I know whether the version specified is active, in which case I can ignore the default.
Modified the class like so