Web Services on Rails note

By Kevin Marshall

The Basics
APIs(application programming interface)

three architectures:
Representational State Transfer (REST)
Simple Object Access Protocol (SOAP)
Extensible Markup Language Remote Procedural(XML-RPC)
(SOAP grew out of XML-RPC)

SOAP
Web service signatures are really just data types that the service either expects or returns.
WSDL(Web Service Description Language)
a WSDL file is an XML document that defines the interface to a SOAP service.

REST
URL, http
The world wide web itself can be considered a REST system.

Searching Yahoo using REST
need:
1.Connect( net lib)
2.store
3.Parse results ( REXML lib)

Yahoo! web service API (up to 5,000 requests/IP per day)

Google AJAX Search API

use the CGI library to escape our search term, ensuring that the search term is safe for use in our HTTP GET request:
query = CGI.escape("SEARCH TEXT")

https://www.google.com/accounts/NewAccount?continue=http://api.google.com/createkey&followup=http://api.google.com/createkey:

Searching Google Using SOAP or SOAP with WSDL Files
UTF-8 can contain special characters that Ruby can’t handle in a string, causing our driver to throw an XSD::ValueSpaceError error upon invocation. To avoid this problem, we manually set our encoding to UTF-8 using the XSD library.
XSD::Charset.encoding = ‘UTF8′

Displaying Photos from Flickr Using XML-RPC

Building Web Service Servers
Make sure you turn off layouts for the methods you’ll be using in your service
Associate RXML templates with the methods in your controllers, instead of RHTML templates
xml is an XML document object that’s available in any RXML template
xml.name("Kevin", :id => 1, "age" => "31")
# creates <name id="1" age="31">Kevin</name>

 xml.exampledata do
     xml.name("Kevin")
 end
 # creates <exampledata><name>Kevin</name></exampledata>

SOAP and XML-RPC Web Service Servers
ActionWebService (AWS)
 script/generate web_service YOURSERVICE YOURMETHOD1 YOURMETHOD2

 three dispatching options, :direct, :delegated, or :layered.
web_service_dispatching_mode:direct
:delegated or :layered is the best way to go for any large or complex web service
With :delegated, clients use a distinct URL for each method in the API
With :layered, clients use the same URL for all attached methods and rely on AWS to route the request based on information passed in its header

:expects and :returns symbols
Ruby types (:string, :int, :bool, :float, :time, :datetime, :date, and :base64), or the names of ActiveRecord::Base or ActionWebService::Struct classes (for example, Greeting or Account), or a single element array to represent arrays of objects (for example, [:string] to represent an array of Strings or [Account] to represent an array of ActionWebService::Struct Account objects).

 raise "Access denied!"

转载请注明: 转自船长日志, 本文链接地址: http://www.cslog.cn/Content/web-services-on-rails/

此条目发表在 Ruby on Rails 分类目录。将固定链接加入收藏夹。

发表评论