ActiveResource xml element ordering

Hello,
I am using ActiveResource to integrate with a 3rd party service.
when calling MyModel.save a POST with xml is sent.
I need to make sure that xml is created with defined order of
elements (the service requires it) How can I do that?
Some have suggested using rxml or builder, but I am not sure how. Has
anyone done this before?

SalesDocument < ActiveResource

I need to make sure that when I call

@sales_document.save

the order of elements in the xml is predetermined and consistent,
which currently not the case.

POST http:///sales_documents.xml
body: <?xml version="1.0" encoding="UTF-8"?>



3.0
AD_DASHERBOARD-200


110
10
110
Sales Lett
10
AG
0000100007
1100

On Mon, Aug 3, 2009 at 10:56 PM, Slava
Mikerin[email protected] wrote:

I am using ActiveResource to integrate with a 3rd party service.
when calling MyModel.save a POST with xml is sent.
I need to make sure that xml is created with defined order of
elements (the service requires it) How can I do that?
Some have suggested using rxml or builder, but I am not sure how. Has
anyone done this before?

I haven’t, but e.g. the Builder API looks pretty straightforward:

http://api.rubyonrails.org/classes/Builder/XmlMarkup.html

:: and includes examples; maybe you should just try that and come
back if you have a specific problem :slight_smile:


Hassan S. ------------------------ [email protected]
twitter: @hassan

I am stuck… here is what i tried
class SalesDocument < ActiveResource::Base
def encode
SalesDocumentsController.new.render_to_string :template, ‘rxml-file’
end
end

But that does not work. I get:

NoMethodError: protected method `render_to_string’ called for
#SalesDocumentsController:0xb6fd111c

thanks, I am not sure how to make my model convert to xml for POST
according to builder rules…
currently it uses a hash to represent all fields and calls hash.to_xml
but hash is unordered so no predictability. this is my specific problem
:slight_smile:

Hassan S. wrote:

On Mon, Aug 3, 2009 at 10:56 PM, Slava
Mikerin[email protected] wrote:

I am using ActiveResource to integrate with a 3rd party service.
when calling MyModel.save a POST with xml is sent.
I need to make sure that xml is created with defined order of
elements (the service requires it) �How can I do that?
Some have suggested using rxml or builder, but I am not sure how. Has
anyone done this before?

I haven’t, but e.g. the Builder API looks pretty straightforward:

http://api.rubyonrails.org/classes/Builder/XmlMarkup.html

:: and includes examples; maybe you should just try that and come
back if you have a specific problem :slight_smile:


Hassan S. ------------------------ [email protected]
twitter: @hassan

Slava M. wrote:

I am stuck… here is what i tried
class SalesDocument < ActiveResource::Base
def encode
SalesDocumentsController.new.render_to_string :template, ‘rxml-file’
end
end

But that does not work. I get:

NoMethodError: protected method `render_to_string’ called for
#SalesDocumentsController:0xb6fd111c

Well, you’re trying to render the controller… I don’t think that’s
what you want.

On Mon, Aug 3, 2009 at 11:21 PM, Slava
Mikerin[email protected] wrote:

thanks, I am not sure how to make my model convert to xml for POST
according to builder rules…
currently it uses a hash to represent all fields and calls hash.to_xml
but hash is unordered so no predictability. this is my specific problem

Are we talking about the same thing? How that even begins to relate to
http://api.rubyonrails.org/classes/Builder/XmlMarkup.html
escapes me…


Hassan S. ------------------------ [email protected]
twitter: @hassan

ok. I found solution :slight_smile:
Here is what I did

class SalesDocument < ActiveResource::Base
def encode
builder = Builder::XmlMarkup.new(:indent=>2)
builder.instruct!
builder.tag!(‘sales-document’){
builder.items(‘type’ => ‘array’){
self.items.each do |item|
builder.item{
builder.quantity(item.quantity, “type” => “float”)
builder.product(item.product)
builder.id(item.id) if item.id
}
end
}
builder.tag!(‘sales-group’, self.sales_group)
builder.tag!(‘sales-office’, self.sales_office)
builder.tag!(‘ref-inquiry’, self.ref_inquiry)
builder.tag!(‘distr-channel’, self.distr_channel)
builder.division(self.division)
builder.tag!(‘document-type’, self.document_type)
builder.customer(self.customer)
builder.tag!(‘sales-org’, self.sales_org)
builder.id(self.id) if self.id
}
builder.target!
end
end

Slava M. wrote:

ok. I found solution :slight_smile:
Here is what I did

class SalesDocument < ActiveResource::Base
def encode
builder = Builder::XmlMarkup.new(:indent=>2)

Cool. BTW, it’s more idiomatic Ruby to use do/end for multiline blocks
and save { } for one-liners.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs