Example of using Vendor MIME Types to determine how to route a request coming into a Rails application.
Last active
December 18, 2015 09:39
-
-
Save weavenet/5762658 to your computer and use it in GitHub Desktop.
Rails Vendor Mime Types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApiVersion | |
def initialize(version=1) | |
@version = version | |
end | |
def matches?(request) | |
versioned_accept_header?(request) | |
end | |
private | |
def versioned_accept_header?(request) | |
accept = request.headers['Accept'] | |
accept && accept[/application\/vnd\.my_app-v#{@version}\+json/] | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Api::V1::AppsController < ApplicationController | |
load_and_authorize_resource :apps | |
respond_to :my_app_v1_json | |
protect_from_forgery :except => :index | |
def index | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Browser | |
def matches?(request) | |
accept = request.headers['Accept'] | |
accept.blank? || accept[/text\/html/] || accept[/application\/xhtml\+xml/] || accept[/application\/xml/] | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Be sure to restart your server when you modify this file. | |
# Add new mime types for use in respond_to blocks: | |
# Mime::Type.register "text/richtext", :rtf | |
# Mime::Type.register_alias "text/html", :iphone | |
Mime::Type.register 'application/vnd.my_app-v1+json', :my_app_v1_json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MyApp::Application.routes.draw do | |
# Resource Routes | |
current_routes = lambda do | |
resources :apps | |
end | |
# Browser Resource Routes | |
constraints Browser.new do | |
scope ¤t_routes | |
end | |
# API Resource Routes | |
constraints ApiVersion.new(1) do | |
scope :module => "Api::V1" do | |
scope ¤t_routes | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment