Putting It All Together: an Expanded Server Example

Now that we've covered the various dispatching modes and discussed how to define a web service API with appropriate signatures, we're ready to dive into a more realistic example. We'll develop the basics of a football statistics web service, starting with two methods: Listgames and Getgamestats. The first method requires an integer parameter that represents a year; it returns a list of games in that year for which statistics are available. The second method requires two parameters, both strings: a username and gamename. If the username is valid, this method returns the actual statistics for a specific game.

We'll start by defining the web service API in app/apis/stats_api.rb:

class StatsApi < ActionWebService::API::Base
  api_method :listgames,
     :expects => [{:year => :int}],
     :returns => [[:string]]

  api_method :getgamestats,
     :expects => [{:username => :string}, {:gamename => :string}],
     :returns => [[Footballstats]]
end

The StatsApi class reflects the service's design requirements. It defines two methods. The first, listgames, requires an integer argument; it returns an array of strings to our clients. The second requires two string arguments and returns an array of Footballstats, which we will define as an ActionWebService::Struct type.

The next step is to set up a controller in the file app/controllers/stats_controller.rb:

class StatsController < ApplicationController wsdl_service_name 'Stats' wsdl_namespace 'urn:sportsxml' web_service_dispatching_mode ...

Get Web Services on Rails now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.