concept httpservice in category rails

This is an excerpt from Manning's book Flexible Rails: Flex 3 on Rails 2.
The HTTPService
makes a call to the sayhello method of the HelloController, due to the way Rails does routing. Simplistically, in the default routing, URLs are mapped /controller_name/action_name. The controller name is capitalized, and the Controller part is assumed, so /hello maps to HelloController, and sayhello maps to the sayhello action (which is a method); url="/hello/sayhello"
maps to the sayhello method of the HelloController class. We aren’t passing any parameters right now; you’ll do this soon when you try to log in.
The id of the HTTPService is helloSvc
. In MXML, the id property of a component becomes its variable name (the MXML file is a class, and the id is the name of a global variable inside that class). If we don’t provide an id for a component, Flex provides one for us—but then we don’t know what it is, so we can’t refer to the component in your code. Sometimes this is fine: We don’t need to refer to the TextInput, so we don’t bother giving it an id. We need to give the helloSvc HTTPService an id, though, so we can call its send method when the button’s click event is broadcast
and so we can refer to the last result from this HTTPService in the binding to the text property of the TextInput
. Note that we have to specify an HTTP method of POST
because the default value of the method property of HTTPService is GET. We’ll talk a lot more about HTTP methods in iteration 8.
In this iteration, we’ll refactor the Flex code to use Cairngorm with HTTPService. We’ll use Cairngorm in a slightly nonstandard way, to be slightly less verbose and more aligned with the design preferences of Rails. (This book won’t attempt to explain “How to Use Cairngorm in the Standard Way.” (Of course, there’s not really a “standard way” any more, now that Cairngorm has been split into Cairngorm and Cairngorm Enterprise.) To learn the standard way of using Cairngorm, see the Cairngorm documentation.)
Next, we create a nonstandard file called ServiceUtils, which contains one public static method called send. This method takes a bunch of parameters, all of which (except the URL) have sensible default values. (This is in line with the “convention over configuration” principle of Rails.) It then creates a disposable HTTPService, for use by this one call, properly configured with the URL and the various defaults.
makes a call to the sayhello method of the HelloController, due to the way Rails does routing. Simplistically, in the default routing, URLs are mapped /controller_name/action_name. The controller name is capitalized, and the Controller part is assumed, so /hello maps to HelloController, and sayhello maps to the sayhello action (which is a method); url="/hello/sayhello"
maps to the sayhello method of the HelloController class. We aren’t passing any parameters right now; you’ll do this soon when you try to log in.
. In MXML, the id property of a component becomes its variable name (the MXML file is a class, and the id is the name of a global variable inside that class). If we don’t provide an id for a component, Flex provides one for us—but then we don’t know what it is, so we can’t refer to
and so we can refer to the last result from this HTTPService in the binding to the text property of the TextInput
. Note that we have to specify an HTTP method of POST
because the default value of the method property of HTTPService is GET. We’ll talk a lot more about HTTP methods in iteration 8.