Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Parsing XML File in Rails


  • Please log in to reply
5 replies to this topic

#1 ruby4life

ruby4life

    Passenger

  • Members
  • 3 posts

Posted 06 July 2015 - 05:27 PM

Screen Shot 2015-07-06 at 10.23.55 AM.png Hi,

 

I'm a super newbie with ruby on rails and ruby in general.  Basically, I'm trying to display a pre-generated xml file within my rails app.  Super simple, but I can't get it to work.  I created a new controller called "nmap" and it has this code:

class NmapController < ApplicationController

require 'nmap/xml'
def create
@nmapfile = Nmapfile.new(nmapfile_params)
@nmapfile.nmapfilerecords = 0
@nmapfile.save
sleep 8
Nmap::XML.new('/opt/ruby_projects/nmap/nmap_parse/app/views/nmap/test.nmap.xml') do |xml|
    xml.each_host do |host|
      if "#{host.status}" == "down"            
      else
        @nmapfile.nmapfilerecords += 1
      end
    end
end

respond_to do |format|
  if @nmapfile.save
    format.html { redirect_to @nmapfile, notice: 'nmap file was successfully created.' }
    format.json { render action: 'show', status: :created, location: @nmapfile }
  else
    format.html { render action: 'new' }
    format.json { render json: @nmapfile.errors, status: :unprocessable_entity }
  end
end
end

I've placed my xml file in "/opt/ruby_projects/nmap/nmap_parse/app/views/nmap/test.nmap.xml" as you can see above.  However, when I load the webapp, I receive the following errors within the application trace (see attached screenshot):

 

/opt/ruby_projects/nmap/nmap_parse/app/controllers/nmap_controller.rb:30: syntax error, unexpected $end, expecting keyword_end
 

 

 

This could be extremely simple to do, so I'm looking for someone to help point me in the right direction.

 

tx.



#2 rahoulb

rahoulb

    Controller

  • Members
  • 111 posts
  • LocationHeadingley, Leeds, England

Posted 06 July 2015 - 07:27 PM

Take care to indent your code properly - each time you hit a block or method or class then the Ruby convention is to include two spaces.  

 

If you do that you will see that you're actually missing an "end" statement so your file does not contain valid Ruby - hence the error.  

class NmapController < ApplicationController

  require 'nmap/xml'
  def create
    @nmapfile = Nmapfile.new(nmapfile_params)
    @nmapfile.nmapfilerecords = 0
    @nmapfile.save
    sleep 8
    Nmap::XML.new('/opt/ruby_projects/nmap/nmap_parse/app/views/nmap/test.nmap.xml') do |xml|
      xml.each_host do |host|
        if "#{host.status}" == "down"            
        else
          @nmapfile.nmapfilerecords += 1
        end
      end
    end

    respond_to do |format|
      if @nmapfile.save
        format.html { redirect_to @nmapfile, notice: 'nmap file was successfully created.' }
        format.json { render action: 'show', status: :created, location: @nmapfile }
      else
        format.html { render action: 'new' }
        format.json { render json: @nmapfile.errors, status: :unprocessable_entity }
      end
    end
  end

As an aside, I would also move the require statement out of the controller - either in your Gemfile (if it's a gem it should be mentioned in there and it will be required automatically) or at worst in your config/application.rb.  The reason for this is a bit esoteric but to do with thread-safety and load orders.  

 

 

 


Write better Ruby and Rails code: http://theartandscienceofruby.com/


#3 ruby4life

ruby4life

    Passenger

  • Members
  • 3 posts

Posted 15 July 2015 - 01:48 AM

Thanks!  I added another "end" to the .rb file and now I'm running into another error.    It errors out and highlights the "require 'nmap/xml'".  Is this where I put the path to the nmap xml file?

Attached Thumbnails

  • Screen Shot 2015-07-14 at 6.46.42 PM.png


#4 rahoulb

rahoulb

    Controller

  • Members
  • 111 posts
  • LocationHeadingley, Leeds, England

Posted 16 July 2015 - 03:48 PM

You could move the require to the top of the file, so it's loaded before the controller.  

 

However, have you loaded it as a gem in your Gemfile?

 

I'm not familiar with the nmap gem (is it https://github.com/sophsec/ruby-nmap?) - but when loaded via bundler and your Gemfile, it will automatically require the main file of the gem - in this case https://github.com/sophsec/ruby-nmap/blob/master/lib/nmap.rb, which in turn requires the 'nmap/xml' for you.  


Write better Ruby and Rails code: http://theartandscienceofruby.com/


#5 ruby4life

ruby4life

    Passenger

  • Members
  • 3 posts

Posted 20 July 2015 - 04:18 PM

I moved the require to the top of the file and I'm receiving the same issues, but yes, I'm using ruby-nmap and it's within my gem list.  For the latter part of your statement, is there something I'm missing then?  I'm not sure what I need to do with nmap.rb.



#6 rahoulb

rahoulb

    Controller

  • Members
  • 111 posts
  • LocationHeadingley, Leeds, England

Posted 22 July 2015 - 04:12 PM

As I say, I'm not familiar with the gem in question - but by default, anything in your Gemfile gets loaded up, and the main nmap.rb file then loads nmap/xml for you - so in theory it should already be loaded ready for you to use.  


Write better Ruby and Rails code: http://theartandscienceofruby.com/





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users