简体   繁体   English

使用Rails创建Web API

[英]Creating web APIs with Rails

Trying to implement API calls. 尝试实现API调用。 What goes in the controller? 控制器里面有什么? I think I should create API an API view. 我想我应该创建一个API API视图。 Should I use a one of the API gems? 我应该使用其中一个API宝石吗?

I'm trying to use my first app to write to the database. 我正在尝试使用我的第一个应用程序写入数据库。 My 2nd App will use a web API to GET/POST from first app's uri for fields that would share same data. 我的第二个应用程序将使用Web API从第一个应用程序的uri获取GET / POST,以获取可共享相同数据的字段。

Take a look at rabl gem it will assist you to generate JSON views using your current controllers, From this you can get the idea of how JSON APIs work and proceed to namespacing your controllers and routes for your API. 看一下rabl gem ,它将帮助您使用当前的控制器生成JSON视图。通过这种方式,您可以了解JSON API的工作原理,并继续命名控制API的控制器和路由。 For http requests between your two apps, you can use either httparty or typhoeus gems 对于两个应用之间的http请求,您可以使用httpartytyphoeus gems

I have done several Api's in RoR, and these are the steps I usually take: 我在RoR中做了几个Api,这些是我通常采取的步骤:

  1. Create a restfull Api: 创建一个restfull Api:

I usually create a controller for the api, lets call it ApiController, all the requests i do on my app go through that controller 我通常为api创建一个控制器,让我们称之为ApiController,我在我的应用程序上执行的所有请求都通过该控制器

  1. Authenticate the api controller: 验证api控制器:

for the sake of simplicity here is a non secure way to authenticate the users who use your api: 为了简单起见,这是一种非安全的方式来验证使用您的API的用户:

class ApiController < ApplicationController
  before_filter :apiauth

  private 

  def apiauth
    if request.headers["ApiAuth"]!='IguD7ITC203'
      raise "error"
    end
  end
end
  1. create some mothods and responses for your api: 为你的api创建一些方法和响应:

here is a simple method you can implement on the controller: 这是一个可以在控制器上实现的简单方法:

def successOrder
  @order=Order.find_by_id(params['id'])
  if @order
    @order.status=params['status']
    if @order.save
      render :json => {:status => "true", :order => {:id => @order.id, :status => @order.status}}
    else
      render :json => {:status =>"false", :errors => @order.errors.full_messages }            
    end 
  else
      render :json => {:status => "false", :errors => ["Invalid Order Id"]}
  end
end
  1. update your routes: 更新您的路线:

sometimes I like to wildcard my routes for my api, like this: 有时候我喜欢用我的api路由通配符,如下所示:

match '/:action', :to => 'api#%{action}', :constraints =>  lambda {|r| r.subdomain.present?  && r.subdomain == 'api'} , :via => [:post]

Finnaly take a look at gems like Jbuilder that can build you awsome responses for your jsons. Finnaly看看像Jbuilder这样的宝石,可以为你的jsons建立你很棒的回应。 hope it helps 希望能帮助到你

I recently created an API which served mobile and web application. 我最近创建了一个服务于移动和Web应用程序的API。 I used RABL gem templating system and i was able to customize my API views as i wanted. 我使用了RABL gem模板系统,我可以根据需要自定义我的API视图。 You can use RABL to generate JSON and XML based APIs from any ruby object. 您可以使用RABL从任何ruby对象生成基于JSON和XML的API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM