mtpbzz
mtpbzz

Reputation: 318

XML feed into Rails object

I'm doing some work with Adcourier. They send me an xml feed with some job data, i.e. job_title, job_description and so on.

I'd like to provide them with a url in my application, i.e. myapp:3000/job/inbox. When they send their feed to that URL, it takes the data and stores it in my database on a Job object that I already created.

  1. What's the best way to structure this? I'm quite new to MVC and i'm not sure where something like this would fit.

  2. How can I get an action to interpret the XML feed from an external source? I use Nokogiri to handle local XMl documents, but never ones from a feed.

I was thinking about using http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-raw_post to handle the post. Doest anyone any thoughts on this?

Upvotes: 0

Views: 652

Answers (1)

Vikko
Vikko

Reputation: 1406

In your job controller add a action inbox which gets the correct parameter(s) from the post request and saves them (or whatever you need to do with it).

def inbox
  data = Xml::ParseStuff(params[:data])
  title = data[:title]
  description = data[:description]
  if Job.create(:title => title, :description => description)
    render :string => "Thanks!"
  else
    render :string => "Data was not valid :("
  end
end 

Next set your routes.rb to send posts request for that URL to the correct location

resources :jobs do
  collection do
    post 'inbox'
  end
end

Note I did just made up the xml parse stuff here, just google a bit to find out what would be the best solution/gem for parsing your request.

Upvotes: 1

Related Questions