rails 与第 3 方互动 API?
rails interact with 3rd party API?
我的应用程序经常需要与第 3 方 API 合作并使用大量来自响应的数据,redmine 就是其中之一。(也许会使用 3~4第3API)
我尝试使用 Net::HTTP,例如:
我的控制器:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def get_request (request)
uri = URI.parse(request)
res = Net::HTTP.get_response(uri)
end
end
require 'net/http'
class LogsController < ApplicationController
def new
redmine_api_key = 'key=' + 'my key'
redmine_api_url = 'http://redmine/users/1.json?'
request_user = redmine_api_url + redmine_api_key
@user_get = get_request(request_user)
@user_data = JSON.parse(@user_get.body)['user']
end
end
我的看法:(只是测试一下我得到了什么)
<div class="container-fluid">
<h1>Account data</h1>
<%= @user_data %><br>
<%= @user_get.code %><br>
<%= @user_get.message %><br>
<%= @user_get.class.name %><br>
<div class="table-responsive">
<table class="table">
<thead>
<th>ID</th>
<th>login</th>
<th>firstname</th>
<th>lastname</th>
<th>mail</th>
<th>created_on</th>
<th>last_login_on</th>
<th>api_key</th>
</thead>
<tbody>
<tr>
<td><%= @user_data['id'] %></td>
<td><%= @user_data['login'] %></td>
<td><%= @user_data['firstname'] %></td>
<td><%= @user_data['custom_fields'][0]['id'] %></td>
</tr>
</tbody>
</table>
</div>
</div>
我可以得到我想要的数据,但我不知道我的方法是正确的还是愚蠢的(我的意思是像JSON.parse(@user_get.body)[ 'user'])。
我做了一些研究,在一些文章中,他们说:如果应用程序使用多个 API,写入 lib 文件夹 是更好的方法。
一些人建议:从第 3 个 API 获取所有数据并 创建自己的数据库 来管理数据。
但是我找不到关于如何使用第 3 方的完整教程 API...
因为您可能需要经常 API 调用第 3 方。您可以在 lib
文件夹中编写该代码。
在Api.rb
module Api
def self.do_get_request(url, params={})
request = request + '?' + params.to_query
uri = URI.parse(request)
response = Net::HTTP.get_response(uri)
JSON.parse(response) if response
end
现在在你的控制器中你可以调用这个函数:
require 'net/http'
class LogsController < ApplicationController
def new
params = {key: 'my key'}
redmine_api_url = 'http://redmine/users/1.json'
response = Api.do_get_request(redmine_api_url, params)
@user_data = response['user'] if response.present?
end
end
do_get_request
可以是一般函数。您还可以在 lib 的 API 模块中创建第三方特定功能,这样您就不必在每个请求的末尾添加密钥。
无论响应是什么,您总是会使用 JSON.parse 解析它,因此可以将该代码推送到 Api 模块。
如果您经常使用这些数据,您可以将其存储在您的数据库中。为此,您必须创建一个模型(阅读 rails 指南:http://guides.rubyonrails.org/getting_started.html)。
我的应用程序经常需要与第 3 方 API 合作并使用大量来自响应的数据,redmine 就是其中之一。(也许会使用 3~4第3API) 我尝试使用 Net::HTTP,例如:
我的控制器:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def get_request (request)
uri = URI.parse(request)
res = Net::HTTP.get_response(uri)
end
end
require 'net/http'
class LogsController < ApplicationController
def new
redmine_api_key = 'key=' + 'my key'
redmine_api_url = 'http://redmine/users/1.json?'
request_user = redmine_api_url + redmine_api_key
@user_get = get_request(request_user)
@user_data = JSON.parse(@user_get.body)['user']
end
end
我的看法:(只是测试一下我得到了什么)
<div class="container-fluid">
<h1>Account data</h1>
<%= @user_data %><br>
<%= @user_get.code %><br>
<%= @user_get.message %><br>
<%= @user_get.class.name %><br>
<div class="table-responsive">
<table class="table">
<thead>
<th>ID</th>
<th>login</th>
<th>firstname</th>
<th>lastname</th>
<th>mail</th>
<th>created_on</th>
<th>last_login_on</th>
<th>api_key</th>
</thead>
<tbody>
<tr>
<td><%= @user_data['id'] %></td>
<td><%= @user_data['login'] %></td>
<td><%= @user_data['firstname'] %></td>
<td><%= @user_data['custom_fields'][0]['id'] %></td>
</tr>
</tbody>
</table>
</div>
</div>
我可以得到我想要的数据,但我不知道我的方法是正确的还是愚蠢的(我的意思是像JSON.parse(@user_get.body)[ 'user'])。 我做了一些研究,在一些文章中,他们说:如果应用程序使用多个 API,写入 lib 文件夹 是更好的方法。 一些人建议:从第 3 个 API 获取所有数据并 创建自己的数据库 来管理数据。 但是我找不到关于如何使用第 3 方的完整教程 API...
因为您可能需要经常 API 调用第 3 方。您可以在 lib
文件夹中编写该代码。
在Api.rb
module Api
def self.do_get_request(url, params={})
request = request + '?' + params.to_query
uri = URI.parse(request)
response = Net::HTTP.get_response(uri)
JSON.parse(response) if response
end
现在在你的控制器中你可以调用这个函数:
require 'net/http'
class LogsController < ApplicationController
def new
params = {key: 'my key'}
redmine_api_url = 'http://redmine/users/1.json'
response = Api.do_get_request(redmine_api_url, params)
@user_data = response['user'] if response.present?
end
end
do_get_request
可以是一般函数。您还可以在 lib 的 API 模块中创建第三方特定功能,这样您就不必在每个请求的末尾添加密钥。
无论响应是什么,您总是会使用 JSON.parse 解析它,因此可以将该代码推送到 Api 模块。
如果您经常使用这些数据,您可以将其存储在您的数据库中。为此,您必须创建一个模型(阅读 rails 指南:http://guides.rubyonrails.org/getting_started.html)。