This file contains 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
# https://github.com/iamvery/rails-api-example/blob/0ec91c6c6cce5113b7f9e1d9484a3f2d94936ad5/config/routes.rb | |
# ... | |
- scope module: :v1 do | |
+ scope module: :v1, constraints: ApiConstraint.new(version: 1) do | |
# ... |
This file contains 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
# https://github.com/iamvery/rails-api-example/blob/5bd29d0bb92c10c3884b1f5aa8fac0886e1f0205/config/routes.rb | |
# ... | |
+ scope module: :v2, constraints: ApiConstraint.new(version: 2) do | |
+ resources :articles, only: :index | |
+ end | |
# ... |
This file contains 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
# https://github.com/iamvery/rails-api-example/blob/5bd29d0bb92c10c3884b1f5aa8fac0886e1f0205/app/controllers/v2/articles_controller.rb | |
module V2 | |
class ArticlesController < ApplicationController | |
def index | |
articles = [ | |
{ id: 123, name: 'The Things' }, | |
] | |
respond_to do |format| | |
format.json do |
This file contains 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
# https://github.com/iamvery/rails-api-example/blob/dbbf6ea77c433937da41e466b6bf2266a0d8cfd1/config/initializers/mime_types.rb | |
Mime::Type.register 'application/vnd.articles+json', :articles_json |
This file contains 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
# https://github.com/iamvery/rails-api-example/blob/17b850599f2546600475b3a525e71c3afff0abbe/app/controllers/v1/articles_controller.rb | |
# https://github.com/iamvery/rails-api-example/blob/17b850599f2546600475b3a525e71c3afff0abbe/app/controllers/v2/articles_controller.rb | |
# ... | |
- format.json do | |
+ format.articles_json do | |
# ... |
This file contains 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
# https://github.com/iamvery/rails-api-example/blob/323fe034d4135a9a2aaa5b801509eb7c6fc38b7b/app/controllers/v1/articles_controller.rb | |
# https://github.com/iamvery/rails-api-example/blob/323fe034d4135a9a2aaa5b801509eb7c6fc38b7b/app/controllers/v2/articles_controller.rb | |
# ... | |
- format.articles_json do | |
+ format.any(:articles_json, :json) do | |
# ... |
This file contains 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
source 'https://rubygems.org' | |
gem 'savon', '~> 2.0' | |
gem 'yard', '~> 0.8.7' | |
gem 'rspec' |
This file contains 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
module ErrorConversion | |
def to_error | |
Error.new(error: name, description: body) | |
end | |
end | |
class Error | |
attr_reader :error, :description | |
def initialize(attributes) |
This file contains 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
require 'forwardable' | |
module Api | |
class Errors | |
extend Forwardable | |
def_delegators :errors, :<<, :to_ary, :empty?, :count | |
def initialize(errors=[]) | |
@errors = Array(errors) |
This file contains 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
module Api | |
class Error | |
DEFAULT_CODE = 42200 | |
attr_reader :error, :description, :code | |
def initialize(attributes) | |
@error = attributes.fetch(:error, &missing_attr) | |
@description = attributes.fetch(:description, &missing_attr) | |
@code = attributes.fetch(:code, DEFAULT_CODE) |