Unable to get XML data from rails controller -
i'm having issue grabbing xml info controller post request xml. i'm able parse xml info fine in irb nokogiri 1 time have xml stored in string variable. there doesn't seem parameters xml info beingness supplied in post request. help set me in right direction how grab xml info great!
started post "/vz_api" 127.0.0.1 @ 2014-06-24 10:59:07 -0400 processing locationscontroller#vz_api */* controller code
def vz_api require 'nokogiri' puts params # {"controller"=>"locations", "action"=>"vz_api"} xml_doc = nokogiri::xml(params["_data"]) puts xml_doc # <?xml version="1.0"?> vin = xml_doc.xpath("//vin").inner_text @truck = truck.find_by(vin: vin) if vin.present? lat = xml_doc.xpath("//latitude").inner_text lng = xml_doc.xpath("//longitude").inner_text @location = @truck.location respond_to |format| if @location.update_attributes(longitude: lng, latitude: lat) format.xml { render xml: @location } else format.xml { render xml: @location.errors, status: :unprocessable_entity } end end end irb xml parsing
f = '<?xml version="1.0" encoding="utf-8" standalone="no"?><networkfleetmessage type="gps"><vin>1m2axo4c5cm014389</vin><fleetid>47790</fleetid><msgid>25195315419</msgid><messagetime>2014-06-24 10:09:46 gmt</messagetime><messagetimeutf>1403604586</messagetimeutf><deliverystatus>current</deliverystatus><gpsfixes numfixes="1"><gpsfix><fixtime>2014-06-24 10:08:59 gmt</fixtime><fixtimeutf>1403604539</fixtimeutf><latitude>40.83705</latitude><longitude>-73.87563</longitude><ignition>on</ignition><speed type="avg" units="mph">0</speed><speed type="inst" units="mph">0</speed><speed type="max" units="mph">0</speed><odometer>51525.976</odometer><ageinmiles>0.000</ageinmiles></gpsfix></gpsfixes></networkfleetmessage>' 2.0.0-p353 :003 > require 'nokogiri' => false 2.0.0-p353 :004 > doc = nokogiri::xml(f) => #<nokogiri::xml::document:0x8270cf4c name="document" children=[#<nokogiri::xml::element:0x8270cbdc name="networkfleetmessage" attributes=[#<nokogiri::xml::attr:0x8270cb78 name="type" value="gps">] children=[#<nokogiri::xml::element:0x8270c5c4 name="vin" children=[#<nokogiri::xml::text:0x8270c31c "1m2axo4c5cm014389">]>, #<nokogiri::xml::element:0x8270c1a0 name="fleetid" children=[#<nokogiri::xml::text:0x82710638 "47790">]>, #<nokogiri::xml::element:0x82711f24 name="msgid" children=[#<nokogiri::xml::text:0x82711c04 "25195315419">]>, #<nokogiri::xml::element:0x827119c0 name="messagetime" children=[#<nokogiri::xml::text:0x82711768 "2014-06-24 10:09:46 gmt">]>, #<nokogiri::xml::element:0x827115ec name="messagetimeutf" children=[#<nokogiri::xml::text:0x827113a8 "1403604586">]>, #<nokogiri::xml::element:0x82711204 name="deliverystatus" children=[#<nokogiri::xml::text:0x82710ffc "current">]>, #<nokogiri::xml::element:0x82710e80 name="gpsfixes" attributes=[#<nokogiri::xml::attr:0x82710e1c name="numfixes" value="1">] children=[#<nokogiri::xml::element:0x8271096c name="gpsfix" children=[#<nokogiri::xml::element:0x82710688 name="fixtime" children=[#<nokogiri::xml::text:0x82710444 "2014-06-24 10:08:59 gmt">]>, #<nokogiri::xml::element:0x8271023c name="fixtimeutf" children=[#<nokogiri::xml::text:0x82714620 "1403604539">]>, #<nokogiri::xml::element:0x82715f70 name="latitude" children=[#<nokogiri::xml::text:0x82715c00 "40.83705">]>, #<nokogiri::xml::element:0x82715a84 name="longitude" children=[#<nokogiri::xml::text:0x827158a4 "-73.87563">]>, #<nokogiri::xml::element:0x827156c4 name="ignition" children=[#<nokogiri::xml::text:0x827154bc "on">]>, #<nokogiri::xml::element:0x82715318 name="speed" attributes=[#<nokogiri::xml::attr:0x827152b4 name="type" value="avg">, #<nokogiri::xml::attr:0x827152a0 name="units" value="mph">] children=[#<nokogiri::xml::text:0x82714bc0 "0">]>, #<nokogiri::xml::element:0x8271497c name="speed" attributes=[#<nokogiri::xml::attr:0x827148b4 name="type" value="inst">, #<nokogiri::xml::attr:0x827148a0 name="units" value="mph">] children=[#<nokogiri::xml::text:0x827183c4 "0">]>, #<nokogiri::xml::element:0x82719fbc name="speed" attributes=[#<nokogiri::xml::attr:0x82719ecc name="type" value="max">, #<nokogiri::xml::attr:0x82719eb8 name="units" value="mph">] children=[#<nokogiri::xml::text:0x82719814 "0">]>, #<nokogiri::xml::element:0x82719670 name="odometer" children=[#<nokogiri::xml::text:0x82719490 "51525.976">]>, #<nokogiri::xml::element:0x82719314 name="ageinmiles" children=[#<nokogiri::xml::text:0x8271910c "0.000">]>]>]>]>]> 2.0.0-p353 :005 > doc.xpath("//vin").inner_text => "1m2axo4c5cm014389"
try using request object , read body of request , parse nokogiri
require 'nokogiri' xml_doc = nokogiri::xml(request.body.read) xml ruby-on-rails-4
No comments:
Post a Comment