title: Nokogiri XML import feed organisation?
body: <p>I have built a site that relies on an XML feed that I currently parse with Nokogiri. Everything works fine and dandy although I have all the code currently within my Admin controller so I can actually invoke the import via a URL i.e. <code>/admin/import/</code>.</p>
  
  <p>I can't help but think that this doesn't belong in the controller. Is there a better way to do this, i.e. move the code into a stand alone <code>import.rb</code> file so it is only accessible from the console? If so where would I need to put this file, in the <code>/lib/</code> directory?</p>
  
  <p>Here is a code snippet:</p>
  
  <pre><code>class AdminController &lt; ApplicationController
  
      def import
        f = File.open("#{Rails.root}/public/feed.xml")
        @doc = Nokogiri::XML(f)
        f.close
  
        ignore_list = [] # ignore list
  
        @doc.xpath("/*/product[not(name = following-sibling::product/name)]").each do |node|
          if !ignore_list.include? node.xpath("./programName").inner_text.strip
            Product.create(:name =&gt; clean_field(node.xpath("./name").inner_text).downcase, 
            :description =&gt; clean_field(node.xpath("./description").inner_text),
            :brand =&gt; Brand.find_or_create_by_name(clean_field_key(node.xpath("./brand").inner_text).downcase),         
            :merchant =&gt; Merchant.find_or_create_by_name(clean_field_key(node.xpath("./programName").inner_text).downcase),     
            :image =&gt; node.xpath("./imageUrl").inner_text.strip,
            :link =&gt; node.xpath("./productUrl").inner_text.strip,
            :category =&gt; Category.find_or_create_by_name(clean_field_key(node.xpath("./CategoryName").inner_text).downcase),
            :price =&gt; "£" + node.xpath("./price").inner_text.strip)
            print clean_field(node.xpath("./name").inner_text).downcase + "\n"       
          end
        end
      end
  end
  </code></pre>