RailsCasts Pro episodes are now free!

Learn more or hide this

Will DelHagen's Profile

GitHub User: wdelhagen

Comments by Will DelHagen

Avatar

Typo in ASCII-Cast

terminal
$ mkdir ~/.ec2
$ mv ~/Downloads/gsg-keypair.gem ~/.ec2/gsg-keypair
$ chmod 600 ~/.ec2/gsg-keypair

gsg-keypair.gem should be gsg-keypair.pem

Avatar

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
class ApiConstraints
  def initialize(options)
    @version = options[:version]
    @default = options[:default]
  end

  def matches?(req)
    if req.headers['Accept'] =~ /application\/vnd.example.v([0-9]+)/
      ver = $1.to_i
      ACTIVE_API_VERSIONS.include?("v#{ver}") ? @version == ver : @default
    else
      @default
    end
    # @default || req.headers['Accept'].include?("application/vnd.linguixlabs.v#{@version}")
  end
end