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. How can I do that? Some have suggested using rxml or builder, but I am not sure how. Has anyone done this before? thank you
I''m not sure what you mean by order, but if you are working with a list of items you can order the array and the to_xml will reflect that. Otherwise if by order you mean having different xml blocks in a post request then use a hash. The order in which the items are contained within the xml won''t matter as long as they are all there and in the proper structure. For example, say we are using a person a payment and a product, and the payment is in the person block. h = Hash.new h[''person''] = Person.first.attributes h[''person''][''payment''] = "$100.00" h[''product''] = Product.first h.to_xml(:root => ''notification'') would result <notification> <person> <attr></attr> <payment>$100</payment> <maybe_another_attr></maybe_another_attr> </person> <product> <attr></attr> <attr2></attr2> </product> </notification> On Jul 31, 10:46 pm, slava <mikerin.sl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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. How can I do that? > Some have suggested using rxml or builder, but I am not sure how. Has > anyone done this before? > > thank you
SalesDocument < ActiveResource I need to make sure that when I call SalesDocument.save the order of elements in the xml is predetermined and consistent, which currently not the case. POST http://<myurl>/sales_documents.xml body: <?xml version="1.0" encoding="UTF-8"?> <sales-document> <items type="array"> <item> <quantity type="float">3.0</quantity> <product>AD_DASHERBOARD-200</product> </item> </items> <sales-group>110</sales-group> <division>10</division> <sales-office>110</sales-office> <ref-inquiry>Sales Lett</ref-inquiry> <distr-channel>10</distr-channel> <document-type>AG</document-type> <customer>0000100007</customer> <sales-org>1100</sales-org> </sales-document>
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://<myurl>/sales_documents.xml body: <?xml version="1.0" encoding="UTF-8"?> <sales-document> <items type="array"> <item> <quantity type="float">3.0</quantity> <product>AD_DASHERBOARD-200</product> </item> </items> <sales-group>110</sales-group> <division>10</division> <sales-office>110</sales-office> <ref-inquiry>Sales Lett</ref-inquiry> <distr-channel>10</distr-channel> <document-type>AG</document-type> <customer>0000100007</customer> <sales-org>1100</sales-org> </sales-document> -- Posted via http://www.ruby-forum.com/.
On Mon, Aug 3, 2009 at 10:56 PM, Slava Mikerin<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 :-) -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan
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 :) Hassan Schroeder wrote:> On Mon, Aug 3, 2009 at 10:56 PM, Slava > Mikerin<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 :-) > > -- > Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > twitter: @hassan-- Posted via http://www.ruby-forum.com/.
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> -- Posted via http://www.ruby-forum.com/.
On Mon, Aug 3, 2009 at 11:21 PM, Slava Mikerin<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 problemAre we talking about the same thing? How that even begins to relate to <http://api.rubyonrails.org/classes/Builder/XmlMarkup.html> escapes me... -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan
Slava Mikerin 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. -- Posted via http://www.ruby-forum.com/.
ok. I found solution :) 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 -- Posted via http://www.ruby-forum.com/.
Slava Mikerin wrote:> ok. I found solution :) > 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.