2

I am trying to conect to REST service like this

class Node < ActiveResource::Base
  self.site = "http://www.openstreetmap.org/api/0.6/"
  self.element_name = "node"  
  self.collection_name = "node"
  self.format = ActiveResource::Formats::XmlFormat 
end

however the object returned is incorrect. Apparently it reads the whole response inluding root element (example of xml here). How do I tell ActiveResource to ignore root element?

The object returned is

#<Node:0x97952a8 @attributes={"version"=>"0.6", "generator"=>"OpenStreetMap server", "copyright"=>"OpenStreetMap and contributors", "attribution"=>"http://www.openstreetmap.org/copyright", "license"=>"http://opendatacommons.org/licenses/odbl/1-0/", "node"=>#<Node:0x9794740 @attributes={"id"=>"367861148", "changeset"=>"872060", "timestamp"=>"2009-03-31T12:00:25Z", "version"=>"1", "visible"=>"true", "user"=>"pavel", "uid"=>"1066", "lat"=>"50.0077", "lon"=>"14.717027", "tag"=>[#<Node::Tag:0x9925104 @attributes={"k"=>"amenity", "v"=>"restaurant"}, @prefix_options={}, @persisted=true>, #<Node::Tag:0x99249fc @attributes={"k"=>"created_by", "v"=>"andnav.org"}, @prefix_options={}, @persisted=true>, #<Node::Tag:0x9924150 @attributes={"k"=>"name", "v"=>"restaurace"}, @prefix_options={}, @persisted=true>]}, @prefix_options={}, @persisted=true>}, @prefix_options={}, @persisted=true> 

but it should be just the value of the "node key".

gorn
  • 5,042
  • 7
  • 31
  • 46

2 Answers2

1

The returned object is not incorrect. It is exactly what the API should return. What do you mean with value of the node key? Obviously you already know the node ID. Do you want the location? The tags? You need to parse the returned XML structure for all keys you are interested in, this should be fairly straightforward.

scai
  • 20,297
  • 4
  • 56
  • 72
  • I do not thing that I should parse XML structure, AFAIK ActiveResource is meant as an drop in replacement for ActiveRecord with the difference, that it does not communicate to database, but with REST web service. Therefor I expected that it should return the object (here Node) and although the response is of the Node class, it does not contains Node data (it contains them but deeper in the structure). – gorn Jun 13 '14 at 13:39
  • But how is ActiveResource supposed to know which part of the XML structure is relevant for your and which isn't? – scai Jun 13 '14 at 13:51
  • It should not, there are lots of configuration options where I can tell it but I did not found one for this purpose. I would be even happy to hack some method of ActiveResource, but after brief look at th sourcecode I was not able to tell where the parsing is done. – gorn Jun 13 '14 at 14:38
1

try use custom formatter

class MyXMLFormatter
  include ActiveResource::Formats::XmlFormat

  def decode(xml)
    ActiveResource::Formats::XmlFormat.decode(xml)['node']
  end
end

class Node < ActiveResource::Base
  self.format = MyXMLFormatter.new
end  
Fivell
  • 11,829
  • 3
  • 61
  • 99
  • This looks good I will try it ... BTW is there any reason why include the formater and not just inherit it? i.e. `class MyXMLFormatter << ActiveResource::Formats::XmlFormat` – gorn Jun 29 '14 at 21:43
  • @gorm, there is many options, but ActiveResource::Formats::XmlFormat is a module not class – Fivell Jun 29 '14 at 21:55