bploetz / versionist

A plugin for versioning Rails based RESTful APIs.
MIT License
972 stars 51 forks source link

List versions #54

Closed gdlx closed 9 years ago

gdlx commented 11 years ago

Hi !

Is there a vay to list defined api versions ?

It would be useful, for example to generate the root of a hypermedia API.

bploetz commented 11 years ago

Something like this should work (just printing out info as an example):

    module_versions = Versionist.configuration.versioning_strategies.collect {|vs| vs.config[:module]}
    module_versions.uniq!
    Rails.logger.info "#{module_versions.size} configured versions"
    module_versions.each do |version|
      strategy = Versionist.configuration.versioning_strategies.find {|vs| vs.config[:module] == version}
      default = false
      default = true if strategy.config.has_key?(:default) && strategy.config[:default]
      version_str = "Version #{version}"
      version_str += " (DEFAULT)" if default
      Rails.logger.info version_str
      Rails.logger.info "  HEADER: #{strategy.config[:header][:name]}=#{strategy.config[:header][:value]}" if strategy.config.has_key?(:header)
      Rails.logger.info "  PATH: /#{strategy.config[:path][:value]}" if strategy.config.has_key?(:path)                                                           
      Rails.logger.info "  PARAMETER: #{strategy.config[:parameter][:name]}=#{strategy.config[:parameter][:value]}" if strategy.config.has_key?(:parameter)
    end

As an example, for these configured versions in config/routes.rb....

  api_version(:module => "V11", :header => {:name => "Accept", :value => "application/vnd.mycompany.com; version=11"}, :parameter => {:name => "version", :value => "11"}, :path => {:value => "v11"}, :defaults => {:format => "json"}) do
    resources :foos
  end

  api_version(:module => "V1", :header => {:name => "Accept", :value => "application/vnd.mycompany.com; version=1"}, :parameter => {:name => "version", :value => "1"}, :path => {:value => "v1"}, :defaults => {:format => "json"}, :default => true) do
    resources :foos
  end

....it produces this output:

2 configured versions
Version v11
  HEADER: Accept=application/vnd.mycompany.com; version=11
  PATH: /v11
  PARAMETER: version=11
Version v1 (DEFAULT)
  HEADER: Accept=application/vnd.mycompany.com; version=1
  PATH: /v1
  PARAMETER: version=1

Is that what you're looking for?