Grape-demo1

Grape,为API设计而生DSL(Domain Specified Language),官网地址: http://intridea.github.io/grape

http://intridea.github.io/grape/docs/index.html

(1)描述性的API构建方式,代码可读性非常强

(2)提供了构建Restful API的一套工具,如参数约束,路径约束,版本管理等

(3)提供了JSON/XML/TXT格式的渲染工具

 

demo的地址是https://github.com/shiralwz/grape_api

module HelloApi
  require 'grape'

  class OneAPI < Grape::API
    resource :apis do
      format :json

      get 'hello' do
        {message: "hello #{params[:name]} via GET"}
      end 

      post 'hello' do
        {message: "hello #{params[:name]} via POST"}
      end 
    end 

  end 

end

 

Rails.application.routes.draw do

  mount HelloApi::OneAPI => '/' 
end

rails s

localhost:3000/apis/hello.json?name=mike

如果不需要apis这个命名空间可以直接删除 app/model/hello_api.rb里的第五行 resource :apis do 和第十五行 end

 

posted @ 2015-10-08 16:20  冰凌花花~  阅读(170)  评论(0编辑  收藏  举报