jeudi 12 novembre 2015

How to serve the latest API through application mime - Rails

Consider the following

namespace :api, defaults: {format: 'json'} do
    namespace :v1 do
      resources :users
      resources :profiles
    end
    namespace :v2 do
      resources :users
      resources :profiles
    end
  end

instead of routing to api/v1 and api/v2, what i would like is set the accept header to use which ever i want it to serve and use the path api neglecting the version.

So if i set it to serve api/v1 the path should just be localhost:3000/api

i was going through this railcasts video

http://ift.tt/1Qk3Ak7

but looking in comments there are variable issues with the code and the solution at time.

What is the best solution at the moment?

Replies from that link

    Each time you create a new MIME type name (application/vnd.example.v1, application/vnd.example.v2, ...),
 a kitten is hurt! don't do that! that's bad!

    Use MIME parameters instead: application/vnd.example; version=1, application/vnd.example; version=2, ... 
(version=, or level=, or v= or whatever you want, this is part of your design).

    The semantic of a MIME type is that every document "flagged" with that type share the same "lineage", 
and that two different names are not related, even if they share a same prefix.

    By incorporating a version identifier into the MIME type name,
 you are making the names distinct and are in the same case as if your V1 API was using text/html documents 
whereas your V2 now uses image/png pictures instead.





The problem with the default is it will accept any mime type, as it will only check the boolean and not the string:

@default || req.headers['Accept'].include?("application/vnd.example.v#{@version}")
I mean, if we're expecting "application/vnd.github.v1", it will also accept "application/vnd.twitter.v1"

Aucun commentaire:

Enregistrer un commentaire