README.rdoc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. = Active Resource
  2. Active Resource (ARes) connects business objects and Representational State Transfer (REST)
  3. web services. It implements object-relational mapping for REST web services to provide transparent
  4. proxying capabilities between a client (ActiveResource) and a RESTful service (which is provided by Simply RESTful routing
  5. in ActionController::Resources).
  6. == Philosophy
  7. Active Resource attempts to provide a coherent wrapper object-relational mapping for REST
  8. web services. It follows the same philosophy as Active Record, in that one of its prime aims
  9. is to reduce the amount of code needed to map to these resources. This is made possible
  10. by relying on a number of code- and protocol-based conventions that make it easy for Active Resource
  11. to infer complex relations and structures. These conventions are outlined in detail in the documentation
  12. for ActiveResource::Base.
  13. == Overview
  14. Model classes are mapped to remote REST resources by Active Resource much the same way Active Record maps model classes to database
  15. tables. When a request is made to a remote resource, a REST XML request is generated, transmitted, and the result
  16. received and serialized into a usable Ruby object.
  17. == Download and installation
  18. The latest version of Active Resource can be installed with RubyGems:
  19. % [sudo] gem install activeresource
  20. Source code can be downloaded as part of the Rails project on GitHub
  21. * https://github.com/rails/rails/tree/3-2-stable/activeresource
  22. === Configuration and Usage
  23. Putting Active Resource to use is very similar to Active Record. It's as simple as creating a model class
  24. that inherits from ActiveResource::Base and providing a <tt>site</tt> class variable to it:
  25. class Person < ActiveResource::Base
  26. self.site = "http://api.people.com:3000"
  27. end
  28. Now the Person class is REST enabled and can invoke REST services very similarly to how Active Record invokes
  29. life cycle methods that operate against a persistent store.
  30. # Find a person with id = 1
  31. ryan = Person.find(1)
  32. Person.exists?(1) # => true
  33. As you can see, the methods are quite similar to Active Record's methods for dealing with database
  34. records. But rather than dealing directly with a database record, you're dealing with HTTP resources (which may or may not be database records).
  35. ==== Protocol
  36. Active Resource is built on a standard XML format for requesting and submitting resources over HTTP. It mirrors the RESTful routing
  37. built into Action Controller but will also work with any other REST service that properly implements the protocol.
  38. REST uses HTTP, but unlike "typical" web applications, it makes use of all the verbs available in the HTTP specification:
  39. * GET requests are used for finding and retrieving resources.
  40. * POST requests are used to create new resources.
  41. * PUT requests are used to update existing resources.
  42. * DELETE requests are used to delete resources.
  43. For more information on how this protocol works with Active Resource, see the ActiveResource::Base documentation;
  44. for more general information on REST web services, see the article here[http://en.wikipedia.org/wiki/Representational_State_Transfer].
  45. ==== Find
  46. Find requests use the GET method and expect the XML form of whatever resource/resources is/are being requested. So,
  47. for a request for a single element, the XML of that item is expected in response:
  48. # Expects a response of
  49. #
  50. # <person><id type="integer">1</id><attribute1>value1</attribute1><attribute2>..</attribute2></person>
  51. #
  52. # for GET http://api.people.com:3000/people/1.xml
  53. #
  54. ryan = Person.find(1)
  55. The XML document that is received is used to build a new object of type Person, with each
  56. XML element becoming an attribute on the object.
  57. ryan.is_a? Person # => true
  58. ryan.attribute1 # => 'value1'
  59. Any complex element (one that contains other elements) becomes its own object:
  60. # With this response:
  61. #
  62. # <person><id>1</id><attribute1>value1</attribute1><complex><attribute2>value2</attribute2></complex></person>
  63. #
  64. # for GET http://api.people.com:3000/people/1.xml
  65. #
  66. ryan = Person.find(1)
  67. ryan.complex # => <Person::Complex::xxxxx>
  68. ryan.complex.attribute2 # => 'value2'
  69. Collections can also be requested in a similar fashion
  70. # Expects a response of
  71. #
  72. # <people type="array">
  73. # <person><id type="integer">1</id><first>Ryan</first></person>
  74. # <person><id type="integer">2</id><first>Jim</first></person>
  75. # </people>
  76. #
  77. # for GET http://api.people.com:3000/people.xml
  78. #
  79. people = Person.all
  80. people.first # => <Person::xxx 'first' => 'Ryan' ...>
  81. people.last # => <Person::xxx 'first' => 'Jim' ...>
  82. ==== Create
  83. Creating a new resource submits the XML form of the resource as the body of the request and expects
  84. a 'Location' header in the response with the RESTful URL location of the newly created resource. The
  85. id of the newly created resource is parsed out of the Location response header and automatically set
  86. as the id of the ARes object.
  87. # <person><first>Ryan</first></person>
  88. #
  89. # is submitted as the body on
  90. #
  91. # POST http://api.people.com:3000/people.xml
  92. #
  93. # when save is called on a new Person object. An empty response is
  94. # is expected with a 'Location' header value:
  95. #
  96. # Response (201): Location: http://api.people.com:3000/people/2
  97. #
  98. ryan = Person.new(:first => 'Ryan')
  99. ryan.new? # => true
  100. ryan.save # => true
  101. ryan.new? # => false
  102. ryan.id # => 2
  103. ==== Update
  104. 'save' is also used to update an existing resource and follows the same protocol as creating a resource
  105. with the exception that no response headers are needed -- just an empty response when the update on the
  106. server side was successful.
  107. # <person><first>Ryan</first></person>
  108. #
  109. # is submitted as the body on
  110. #
  111. # PUT http://api.people.com:3000/people/1.xml
  112. #
  113. # when save is called on an existing Person object. An empty response is
  114. # is expected with code (204)
  115. #
  116. ryan = Person.find(1)
  117. ryan.first # => 'Ryan'
  118. ryan.first = 'Rizzle'
  119. ryan.save # => true
  120. ==== Delete
  121. Destruction of a resource can be invoked as a class and instance method of the resource.
  122. # A request is made to
  123. #
  124. # DELETE http://api.people.com:3000/people/1.xml
  125. #
  126. # for both of these forms. An empty response with
  127. # is expected with response code (200)
  128. #
  129. ryan = Person.find(1)
  130. ryan.destroy # => true
  131. ryan.exists? # => false
  132. Person.delete(2) # => true
  133. Person.exists?(2) # => false
  134. == License
  135. Active Resource is released under the MIT license.
  136. == Support
  137. API documentation is at
  138. * http://api.rubyonrails.org
  139. Bug reports and feature requests can be filed with the rest for the Ruby on Rails project here:
  140. * https://github.com/rails/rails/issues
  141. You can find more usage information in the ActiveResource::Base documentation.