API-based Client/server Applications With TDD and ActiveResource
API-based Client/server Applications With TDD and ActiveResource
Wolfram Arnold
www.rubyfocus.biz
In collaboration with:
Kurt Snyder
papercheck.com
marakana.com
Database-backed
Application
ActiveRecord
SQL
DB
API-backed
Client
Application
ActiveResource
JSON
API
Server SQL
Application
DB
Tradeoffs?
DB-backed
Fast
ActiveRecord well developed
Complex relationships possible
Limited scalability—tight coupling
API-backed
Extra network & server latency
Complex relationships more difficult
Scales well to different front-ends—loose coupling
Frontend Scalability
Client Client
Client
Application Application
Application
(Flash or JS) (Mobile)
ActiveResource
JSON
API
Server SQL
Application
DB
Rails ActiveResource
http://github.com/taryneast/hyperactiveresource
Adds Associations
Supports nested routes
/people/5/addresses/3
But: Cannot mix nested and non-nested routes for the
same resource
Adds Validations
Let's do some coding
Demo
RESTful Person Server API with JSON
HyperactiveResource-based client
TDD anyone?
Application
Test Database
DB
with transaction support
TDD ActiveRecord Style
Client
Application
ActiveResource
RSpec model test
JSON
FakeWeb.register_uri(:get,
"http://server.example.com/people",
:body => "Hello World!")
Remote Specs
Rely on remote server running (in test environment)
Mechanism to set up data
Mechanism to inspect data
Mechanism to clean up data
Distributed Ruby “DRb”
Client
Application
Remote
Method
foo(args) Invocation
DRb Client
DRb
DRb Server
foo(args)
Server
Application
Demo DRb
Gotcha's
ActiveResource::Base.include_root_in_json
after_initialize hook for starting DRb server
:id problem for Objects, via DRb
Need to run server in test mode
Ideas/Questions:
Front Object in DRb?
rake task to start/stop server—reliably find process?
DRb port inflation—better way?
Summary