我对rails很陌生,我希望有人能为我指明正确的方向,如何完成以下工作:
实现这一目标的最佳工具/技术是什么??
其中一种较简单的方法如下:
**SEND**
<?xml version="1.0" encoding="utf-8" ?>
<SoftwareAPI>
<Method>ListUsers</Method>
<APIKey>123</APIKey>
<Account>
<UserName>admin</UserName>
<Password>Password</Password>
</Account>
</SoftwareAPI>
**RESPONSE**
<?xml version="1.0" encoding="utf-8" ?>
<SoftwareAPIResponse>
<TimeNow>2012-01-23T16:44:00Z</TimeNow>
<ResponseId>01-23-1232729040-23456</ResponseId>
<ListUsersResponse>
<User>
<Team>team</Team>
<Office>office</Office>
<UserName>Joe.Bloggs</UserName>
<Password>Password123</Password>
<FullName>Joe Bloggs</FullName>
<Language>Auto-Detect</Language>
<Telephone>+44 207 123 456 789</Telephone>
<ResponseEmail>joebloggs@domain.co.uk</ResponseEmail>
</User>
</ListUsersResponse>
</SoftwareAPIResponse>
事先非常感谢
琼尼
发布于 2012-01-09 12:19:10
您猜到了: API客户端的最佳位置是一个模型。使用像HTTParty或RestClient这样的库,这个任务相当容易。控制器只应该请求视图所需的数据。
下面是一些使用HTTParty的示例代码。因为我没有细节,所以你得修改一下。这将是一种模式:
class JonnyService
include HTTParty
base_uri 'http://localhost:3000'
end
然后你就可以这样用它了。请注意,为了更方便起见,最好将其中一些逻辑(例如,为每个服务方法创建post参数)作为类方法移到模型中。
options = {
:body => {
:SoftwareAPI => {
:Method => 'ListUsers',
:APIKey => '123',
:Account => {
:UserName => 'admin',
:Password => 'password'
}
}
}
}
response = JonnyService.post('/service.xml', options)
puts response.inspect
#response can be treated as a data structure:
puts response['ResponseId']
https://stackoverflow.com/questions/8787605
复制相似问题