:: Topo :: doCoding :: Linguagens de Programação :: Ruby :: FORPC101 ::

Week-12

Aprendizagem

WebService

Os WebServices são, normalmente, baseados numa das seguintes arquitecturas:

  1. Representational State Transfer (REST);
  2. Simple Object Access Protocol (SOAP);
  3. Extensible Markup Language Remote Procedural Calls (XML-RPC).

Para codificar um serviço SOAP basta usar a soap4r:

require 'sopar4r'
A soap4r implementa o SOAP v1.1, e faz parte da Standard Library na v1.8 - não será incluída na próxima versão do Ruby.

Exercícios

Exercício 1

Server

=begin
Write a method that converts from Fahrenheit temperature to Celsius
and host this web service on your own localhost.
=end
 
require 'logger'
require 'soap/rpc/standaloneServer'
 
class Centigrade2FahrenheitWebService < SOAP::RPC::StandaloneServer
 
  def initialize(*args)
    super
    add_method(self, 'f2c', 'straider')
    add_method(self, 'c2f', 'straider')
    @log = Logger.new("soapserver.log", 5, 10 * 1024)
  end
 
  def f2c(temperature_in_fahrenheit = -40.0)
    @log.info("f2c() called with argument: #{temperature_in_fahrenheit}")
    temperature_in_centigrade = (temperature_in_fahrenheit - 32.0) * 5 / 9
    @log.info("f2c() result = #{temperature_in_centigrade}")
    temperature_in_centigrade
  end
 
  def c2f(temperature_in_centigrade = -40.0)
    @log.info("c2f() called with argument: #{temperature_in_centigrade}")
    temperature_in_fahrenheit = (temperature_in_centigrade * 9.0 / 5) + 32
    @log.info("c2f() result = #{temperature_in_fahrenheit}")
    temperature_in_fahrenheit
  end
 
end
 
soap_server = Centigrade2FahrenheitWebService.new('Centigrade2FahrenheitWebService', 'urn:C2F-SOAP', 'localhost', 10080)
trap('INT') { soap_server.shutdown }
soap_server.start

Client

=begin
Write a method that converts from Fahrenheit temperature to Celsius
and host this web service on your own localhost.
=end
 
require 'soap/rpc/driver'
 
driver = SOAP::RPC::Driver.new('http://localhost:10080/', 'urn:C2F-SOAP')
driver.add_method('f2c', 'straider')
driver.add_method('c2f', 'straider')
puts driver.f2c(-459.67)
puts driver.c2f(-273.15)

Questionário

Referências

Considerações

Pontos Altos

Esta secção regista o que considero serem os pontos altos da linguagem Ruby quando comparada com outras linguagens que conheço.

  1. Novamente, como já é hábito, o Ruby torna a codificação de um WebService muito simples. Extremamente simples.

Pontos Baixos

Esta secção regista o que considero serem os pontos baixos da linguagem Ruby quando comparada com outras linguagens que conheço.

 
docoding/languages/ruby/forpc101/week-12.txt · Modificado em: 2008/09/25 10:07 por straider     Voltar ao topo