Ruby on Rails and XML - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Ruby on Rails and XML

Description:

Ruby on Rails Sweet Spots. RESTful CRUD 80% Rule [makes easy easier and ... is the invocation of an atomic, CRUD-based operation on web accessible resources ... – PowerPoint PPT presentation

Number of Views:80
Avg rating:3.0/5.0
Slides: 16
Provided by: danp152
Category:
Tags: xml | crud | rails | ruby

less

Transcript and Presenter's Notes

Title: Ruby on Rails and XML


1
Ruby on Rails and XML
  • A 2008 Campaign Sound bite on the fifth
    anniversary of
  • MISSION ACCOMPLISHED

2
Ruby on Rails Sweet Spots
  • RESTful CRUD 80 Rule makes easy easier and
    hard harder
  • REST http//AppURL.com/controller/action
  • RoR is an ORM that is a Wrapping Framework as
    opposed to a Mapping Framework (Hibernate)

Poor Browser Support
3
What is REST
  • Individual HTTP methods represent the type of
    action to take (The Verb) and the URI is the
    location of the resource to act upon (The Noun).
  • REST is the invocation of an atomic, CRUD-based
    operation on web accessible resources by the
    appropriate HTTP method instead of the URL of the
    request.
  • GET /person/1 same URIs but different
    methods
  • DELETE /person/1

4
RESTful Architecture
No WSDL needed Convention over
Configuration User Developer vs IT focus
http//AppURL.com/controller/action
http//YourStore.com/product/who_bought
demo/
code for who_bought action
app/
Class Product ltApplicationController def
who_bought _at_productProduct.find(paramsid)
_at_orders_at_product.orders end end
controllers/
Product_controller.rb
models/
Xml.order_list(for_productgt_at_product.title) do
for o in _at_order xml.order do
xml.name(o.name) xml.email(o.email)
end end end
views/
who_bought.rxml
5
Architectural Decisions Made for You
  • Simple Convention for URL Routing
  • Names of a controller (a class name)
  • Action (a method name)
  • Primary Key (ID)
  • Mappings and Routings by default
  • Map URIs to Rail Controllers
  • Map Rails controllers to resources
  • Map Resources to ActiveRecord objects
  • Ruby uses a Hash (key value pairs) as the
    internal representation and maps to either XML or
    Form-encoded

6
Basic XML Processing Facilities
  • Ruby Built Ins
  • Sean Russells Ruby Electric XML (REXML)
  • Rails Built Ins
  • XML Builder Library
  • Alternatives I HAVENT CHECKED WHAT IS IN
    RAILS 2.0 !!!
  • Libxml-Ruby - a Ruby binding to GNOMEs
    libxml2 library
  • XmlSimple Ruby translation of Perl
    XMLSimple and requires REXML used in
    ActiveResource
  • SOAP4r seeing more usage by the Java croud as a
    simple way to perform system admin tasks e.g.
    SOAP authentication into SalesForce.com for SSO.
  • Porting activity from Python and PERL to Ruby for
    specialized XML parsers

7
REXML
Ruby uses a Hash (key value pairs) as the
internal representation and maps to either XML or
Form-encoded PHP prices array(Tiresgt100,
Oilgt10, Spark Plugsgt 4) Ruby prices
tire gt 100, oilgt 10, SparkPlugs gt 4
in Rails, hashes typically use symbols as keys.
Can use strings like PHP. Hash.create_from_xml
ltltEOX ltusergt ltid typeintegergt1lt/idgt
ltuser-namegtryanlt/user-namegt lt/usergt EOX Result?
user gt id gt 1, user_name gt ryan
8
XML Builder Library/demo/app/views/who_bought.rxm
l
  • Variable xml represents the XML object being
    constructed. When a method is invoked on this
    object, the builder emits the corresponding tag
  • .rxml template gives control over order of
    elements returned

xml.order_list(for_productgt_at_product.title) do
for o in _at_order xml.order do
xml.name(o.name) xml.email(o.email)
end end end
9
Autogenerating XML from a Model
  • Let Rails generate the XML for a model object by
    calling to_xml method
  • _at_product.to_xml
  • When to use
  • If order of elements returned is not important
  • Dont use if you need to meet a particular schema
    or DTD, use .rxml template
  • By default, to_xml dumps everything out and
    excluding certain attributes gets messy quickly
  • Can pass to render to set the response content
    type to application/xml

10
Three ways of viewing Inventory DataActionView
Module renders Templates
  • rhtml Templates ? JSP hardcoding style using lt
    ruby code gtMixture of html and embedded Ruby
    Code
  • rxml Templates ? Ruby Code in rxml uses the XML
    Builder Library to construct XML Responses
    Converts names of methods to XML tags xml.price
    ? ltpricegt and pulls attributes from the
    HASH. (if conflicts force xml.tag)
  • rjs Templates ? creates Javascript for AJAXified
    web pages

11
Evolution in Ease of Use Abstractions
ActiveRecord abstracted away the database
layer and you stay in the RoR Domain ActiveReso
urce abstracted away the RESTful architecture
details of controllers and xml etc.
ActiveRDF - use the same RoR techniques for
bridging from RDF triples to objects as did the
ActiveRecord people bridging from relational
tables to objects.
12
ActiveResource OO REST Services
Configuration is as simple as inheriting from
ActiveResourceBase and providing a site class
variable. class Person lt ActiveResourceBase
self.site http//api.yoursite.com3000/
end Person is now REST enabled and can invoke
REST services similar to how ActiveRecord invokes
lifecycle methods over a persistent
store. Person.find(1).name gt Ryan If the
Person model is an ActiveResource, the find call
is actually sending an HTTP GET request across
the wire to a RESTful service which returns
Ryan. ActiveResource provides a way to utilize
model objects as REST-based client proxies to
remote services.
13
The 3rd Newest AbstractionActiveRDF (new
immature)
Today OO programming is the current mainstream
programming paradigm but existing RDF APIs are
mostly triple oriented. An RDF triple is a
subject, predicate, object. The Dream - use
the same RoR techniques for bridging from RDF
triples to objects as did the ActiveRecord people
bridging from relational tables to objects.
14
Mapping RDF Resources
Mapping Resources
The Dream to query a SPARQL endpoint such as
dbpedia
references Query.new.distinct(reference).
where(RDFSResource.new("http//dbpedia.org/resou
rce/resource_name"), RDFSResource.new('http/
/dbpedia.org/property/reference'), reference).
execute It translates to this SPARQL query
over the wire SELECT ?re WHERE
lthttp//dbpedia.org/resource/The_Beatlesgt
lthttp//dbpedia.org/property/referencegt ?object .
15
RoR Secret Sauce
  • Reusable Mental Models Rails is good at gently
    imposing its design preferences on the
    programmer ? Doing the same kind of thing in the
    same way.
  • Higher quality and Lower Costs through Reduced
    Code Size (1/2 to 1/5 the code of Java
    equivalent) and it takes effort to defeat TDD
    (Test Driven Development). No Drive By
    Shootings of XML configuration data. Easy to
    find the code you need to change.
  • Use XML as a wire format for interoperability and
    keep it simple
  • As of mid 2008, RoR is not a large enterprise
    phenomenon but the rise of User Developer is
    making a dent in large organizations for managing
    large contracts through the use of an Executable
    Proposal.
  • This will drive the maturation of Enterprise
    Quality XML tools. (e.g. 4 gb xml file from the
    days retail sales) ? IBM DB2 Express C and
    pureXML (Xquery) and Ruby bindings
Write a Comment
User Comments (0)
About PowerShow.com