On Mon, Jan 9, 2012 at 1:10 PM, Jonny Turk wrote:

I am new to rails and looking someone to point me in the right
direction in how to accomplish the following:-

I need to communicate with an external api by either passing an XML
document directly
into the cgi (https://api.domain.com/v1/method.cgi) and set the
content-type to "text/xml", or pass it as a parameter and set the
content-type to "text/plain"

I supposedly get an XML response back instead of the HTML response, so
no need to download the HTML response, store it, then render a local
copy for the user; nor will i need to stick the XML document within a
parameter of a locally generated HTML form to submit it via the
browser to avoid downloading the HTML.

Each API method has example xml code for (Sending, The Response, DTD,

What is the best tools/techniques to accomplish this !??

One of their simpler methods is as follows :-


<?xml version="1.0" encoding="utf-8" ?>


<?xml version="1.0" encoding="utf-8" ?>
<FullName>Joe Bloggs</FullName>
<Telephone>+44 207 123 456 789</Telephone>

This API method needs no interaction from the user or view, should the
coding be done from the controller or should I create a model for all
the api methods ?

How do I perform a post to the cgi url with the specified XML above
and process the response XML and display in a view ?

What are the best practices for accomplishing this ?
A few hints ...

1) Add a XML file to override the JSON default template wit scaffolds.

Something like this:


(this is just the default JSON format, with all json find/replace with xml).

2) Try out

$ rails generate resource User first_name:string last_name:string

to see an example in a scaffold.

Once that is there, you can start playing with localhost:3000/users.xml
to see the effect. By targeting either:
* the .xml extension in a GUI browser
* the Accept and Content-Type 'application/xml' in curl (see below)

Check the /log/development.log to verify that XML format is used (and not

3) You may override to_xml to e.g. have whitelists on what you expose over
the API
(don't forget to call super in your overridden method). to_xml will be
default be
used and will render a default XML template.

You can override that with a custom xml builder, like:

app/views/users/show.xml.builder # here you would create your special
builder for
# <SoftwareAPIResponse> if

But with clever includes and whitelists of associated resources, you may be
able to use the default XML template.

4) When you have associated models (e.g. User has_one :address), you will
2 difficulties:

* address.singularize is wrong and this causes a bug in the to_xml
(<user><addres>...</addres></user> with 1 's'). I will try to commit a patch
for that (in the meanwhile, use custom inflections where needed).

* for create/update accepts_nested_attributes_for will not work
(since it only adds e.g. user.address_attributes= ; but for XML you would
user.address= to accept a hash and not a <Address ...> object. I will try to
publish a gem for that.

* for the XML format (instead of HTML), the client needs to set BOTH the
Accepts and the Content-Type headers. You could use curl to test. Like this:
request << "--header 'Accept:application/xml'"
request << "--header 'Content-Type:application/xml; charset=utf-8'"


Call me if you have more questions on this.


Peter Vandenabeele

You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

Discussion Posts


Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 2 | next ›
Discussion Overview
grouprubyonrails-talk @
postedJan 9, '12 at 2:35p
activeJan 9, '12 at 5:22p

2 users in discussion

Peter Vandenabeele: 1 post Jonny Turk: 1 post



site design / logo © 2021 Grokbase