edmc / api-versions Goto Github PK
View Code? Open in Web Editor NEWA simple gem to manage your Rails API routing endpoints.
License: MIT License
A simple gem to manage your Rails API routing endpoints.
License: MIT License
Forgive me if I'm doing something wrong, but after setting up api-versions, if I use respond_with @foo inside API::V1::FooController, it squawks at me that "api_v1_foos_url" doesn't exist. Did I miss something?
Thanks,
Chad
Rake routes shows that it just returns the url as /api/replies/:id
example
api vendor_string: 'vendor', default_version: 1, path: '' do
version 1 do
cache as: 'v1' do
resources :posts, shallow: true do
resources :replies
end
end
end
end
Hello,
I am trying api-versions
on a small project and encountered an issue with some of my already existing routing specs :
# encoding: UTF-8
require 'spec_helper'
describe 'Registrations' do
it 'routes to #create' do
expect(post('/users')).to route_to("api/v1/registrations#create")
end
end
RSpec returns :
1) Registrations routes to #create
Failure/Error: expect(post('/users')).to route_to("api/v1/registrations#create")
NoMethodError:
undefined method `split' for nil:NilClass
# ./spec/routing/registrations_routing_spec.rb:7:in `block (2 levels) in <top (required)>'
Indeed, actually the gem tried to do a split on a non existing header :
def matches?(request)
accepts = request.headers['Accept'].split(',')
accepts.any? do |accept|
accept.strip!
accepts_proper_format?(accept) && (matches_version?(accept) || unversioned?(accept))
end
Cf. that file.
On routing specs it seems that there is no way to specify an Accept
header. More, since the last changes on default version, I guess that at that point of the code we should already have a default version in Accept
header so that the split doesn't fail.
PS : yes I'm on the current master branch.
I'm using Rails 4 and Ruby 2.0. The change to setting default headers breaks all my none JSON tests. My Rails app has some HTML pages as well. The error I get is:
ActionController::UnknownFormat (ActionController::UnknownFormat)
./features/step_definitions/user_steps.rb:42:in `sign_in'
./features/step_definitions/user_steps.rb:74:in `/^I sign in with valid credentials$/'
features/users/sign_in.feature:22:in `When I sign in with valid credentials'
I used Rails composer to create the basic framework so the tests are their defaults.
If I revert to the latest published gem (1.2.0) everything is ok again.
Hey,
First of all, love the work you've done with this! :)
It has been documented, and verified, that controller specs no longer work when you use this library and request specs work fine.
That said, I would love to still be able to use controller specs, simply because they don't touch routing and are faster to execute. Is there a way in which I can still use controller specs for some of my controllers?
I would like to specify the format.
Could you let me know if you have any better ideas than using the defaults
block?
api vendor_string: 'foo', default_version: 1 do
defaults format: :json do
version 1 do
cache as: 'v1' do
:
end
end
end
end
Hello,
Quick question, actually not an issue here. :)
I would like to know if there's a 'clean' way of removing default api
namespace which is added by the gem.
Actually, I would like to directly have something like that : app/controllers/v1
and not app/controllers/api/v1
.
currently you are only supporting the following format application/vnd.myvendor+json;version=1
for the Accept
header...
Is it possible to also support the following format application/vnd.myvendor.v1+json
? similar to github accept header which IMHO think cleaner format
My routes.rb is as follows:
api vendor_string: "test", default_version: 1, defaults: { format: 'json' } do
version 1 do
cache as: 'v1' do
resources :jobs
end
end
end
And my rspec test follows the example test:
require 'spec_helper'
describe Api::V1::JobsController do
describe "GET 'index'" do
it "should be successful" do
get '/api/jobs', {}, 'HTTP_ACCEPT' => 'application/vnd.test+json; version=1'
response.should be_success
end
end
end
But I get the following my tests:
1) Api::V1::JobsController GET 'index' should be successful
Failure/Error: get '/api/jobs', {}, 'HTTP_ACCEPT' => 'application/vnd.test+json; version=1'
ActionController::UrlGenerationError:
No route matches {:controller=>"api/v1/jobs", :action=>"/api/jobs"}
# ./spec/controllers/api/v1/jobs_controller_spec.rb:6:in `block (3 levels) in <top (required)>'
I've tried the most recent published gem and also the latest version direct from GitHub and the result is the same. If I view from the browser or curl at the command line, it's successful:
[{"job":{}}]
54eaadb fixes a common bug where:
curl -i -XGET http://localhost:3000/api/bar/new \
-H 'Accept: '
Would respond with a 500 due to:
ERROR NoMethodError: undefined method `split' for nil:NilClass
/home/zorbash/dev/gems/api-versions/lib/api-versions/version_check.rb:12:in `matches?'
A new gem version has to be released to contain the fix.
According to a middleware spec, ApiVersions::Middleware
should add a default vendor accept to a nil Accept header.
But, in my opinion, it adds default vendor only if request path contains prefix path like /api
. Otherwise, It adds default vendor when request path is /
and Accept header is */*
.
What do you think of this?
Thank you for sharing this project. ๐
rails destroy api_versions:bump
does not destroy generated version, if you have v2 it tries do destroy v3, the next one.
Monitoring services such as pingdom do not set accept headers.
api-versions-1.1.0/lib/api-versions/middleware.rb:8โ call
warden-1.2.1/lib/warden/manager.rb:35โ block in call
Will do a pull request shortly.
I don't know whether it is handled or not but I didn't find any documentation regarding inheriting only few methods from previous version.
eg.
api vendor_string: "myvendor", default_version: 1 do
version 1 do
cache as: 'v1' do
resources :authorizations
end
end
version 2 do
inherit from: 'v1'
# Now I want only `index` action to inherit from authorizations and want to modify other actions of authorization controller. How to do this?
end
end
Thanks.
I have an application with a controller at app/controllers/api/v1/brewery_db/webhooks_controller.rb
and the following routes:
Goodbrews::Application.routes.draw do
api vendor_string: 'goodbrews', default_version: 1, path: '' do
version 1 do
cache as: 'v1' do
namespace :brewery_db do
resources :webhooks, only: [] do
collection do
get 'beer'
post 'brewery'
post 'location'
post 'guild'
post 'event'
end
end
end
end
end
end
end
Running rake routes
shows the expected:
Prefix Verb URI Pattern Controller#Action
beer_api_brewery_db_webhooks GET /brewery_db/webhooks/beer(.:format) api/v1/brewery_db/webhooks#beer {:vendor_string=>"goodbrews", :default_version=>1}
brewery_api_brewery_db_webhooks POST /brewery_db/webhooks/brewery(.:format) api/v1/brewery_db/webhooks#brewery {:vendor_string=>"goodbrews", :default_version=>1}
location_api_brewery_db_webhooks POST /brewery_db/webhooks/location(.:format) api/v1/brewery_db/webhooks#location {:vendor_string=>"goodbrews", :default_version=>1}
guild_api_brewery_db_webhooks POST /brewery_db/webhooks/guild(.:format) api/v1/brewery_db/webhooks#guild {:vendor_string=>"goodbrews", :default_version=>1}
event_api_brewery_db_webhooks POST /brewery_db/webhooks/event(.:format) api/v1/brewery_db/webhooks#event {:vendor_string=>"goodbrews", :default_version=>1}
When I try to visit /brewery_db/webhooks/beer
(which I changed to a GET
for testing), I get No route matches [GET] "/brewery_db/webhooks/beer"
.
Changing my routes from
Goodbrews::Application.routes.draw do
api vendor_string: 'goodbrews', default_version: 1, path: '' do
version 1 do
cache as: 'v1' do
# ...
to
namespace :api, path: ''
namespace :v1, path: ''
# ...
Makes my routes work as expected. Something in api-versions
seems to be messing with namespacing.
Hey, I tried using this gem but for some reason none of the routes work. I'm using Rails 4.0.0.rc and Ruby 2.0.0. I created a sample app which shows my setup. Am I missing something? https://github.com/cristian-rivera/api-versions_example
If I'm reading the source code right, then the file that sets the default header doesn't actually use the default_version
config option.
https://github.com/erichmenge/api-versions/blob/master/lib/api-versions/middleware.rb
So, I think currently if you don't manually set the header then the middleware sets the version to 1, regardless of the value of default_version
.
I would expect it to set the header to default_version
and if that's not set then 1.
In fact searching the repo it looks like default_version
isn't really used at all (though search might be dodgy) https://github.com/erichmenge/api-versions/search?q=default_version&source=c
Otherwise great gem, love being able to read all the code in one sitting, thanks.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.