开发者

XML parsing from API with Nokogiri and Rails 3

Hi Im trying to parse XML from a websites API with Nokogiri. Im just curious to see if Im on the right track. I have a controller wich handles the parsing and then I would like the model to initialize the necessary parameters and then display it as a simple list in the view. I was thinking something like this in the Controller:

def index
doc = Nokogiri::XML(open("http://www.mysomething.com/partner/api/1_0/somerandomkeynumber4b0/channel/11number/material/list/").read) 

@news = [] 

doc.css("news").each do |n|
  header = n.css("header").text
  source_name = n.css("source_name").text  
  summary = n.css("summary").text 
  url = i.css("url").text
  created_at = i.css("created_at").text
  type_of_media = i.css("type_of_media").text

  @news << News.new(
                    :header => header,)
end

and then the Model:

class News
include ActiveModel::Validations

validates_presence_of :url, :type_of_media

attr_accessor :header, :source_name, :summary, :url, :created_at, :type_of_media  

def initialize(attributes = {})
  @header = attributes[:header]
  @source_name = attributes[:source_name]
  @summary = attributes[:summary]
  @url = attributes[:url]
  @created_at = attributes[:created_at]
  @type_of_media = attributes[:type_of_media]
end  

Is this how you would do this?! Not sure Im thinking correct on this. Maybe you have any tips on a great way of incorporating Nokogiri with some other thing for the view like Google maps or something. Right now Im getting an error saying

Missing template news/index with {:formats=>[:html], :handlers=>[:builder, :rjs, :erb, :rhtml, :rxml], :locale=>[:en, :en]} in view paths

Thanks in advance!

@noodle: So this:

  @news = doc.css('query').map do |n|
  h = {}
  %w(header source_name summary url created_at type_of_media).each do |key|
   h[key.to_sym] = n.css(key).text
  end
 News.new(h)
end 

Is equal to:

@news = [] 

doc.css("news").each do |n|
  header = n.css("header").text
  source_name = n.css("source_name").text  
  summary = n.css("summary").text 
  url = i.css("url").text
  created_at = i.css("created_at").text
  type_开发者_如何学运维of_media = i.css("type_of_media").text

  @news << News.new(
                :header => header,)
end

Did I understand you correctly?! Regarding the template I have located the the problem. It was a minor misspelling. Cheers!


You're really asking two questions here..

Is my xml -> parse -> populate pipeline ok?

Yes, pretty much. As there's no conditional logic in your .each block it would be cleaner to do it like this:

@news = doc.css('query').map do |n|
  #...
  News.new(:blah => blah, ...)
end

.. but that's a minor point.

EDIT

You could save some typing by initializing a hash from the parsed xml and then passing that to Model.new, like:

@news = doc.css('query').map do |n|
  h = {}
  h[:header] = n.css('header').text
  # ...
  News.new(h)
end

EDIT 2

Or even shorter..

@news = doc.css('query').map do |n|
  h = {}
  %w(header source_name summary url created_at type_of_media).each do |key|
    h[key.to_sym] = n.css(key).text
  end
  News.new(h)
end

In fact #inject could make that shorter still but I think that'd be a little obfuscated.

Why can't rails find my view template?

Dunno, is there one? You've not given enough details to answer that part.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜