[RUBY] [Rails] Read the RSS of the site and return the contents to the front

What is RSS?

Abbreviation for Rich Site Summary, which means a rich site summary.

The main feature is

--Use RDF to convert content information into metadata. --Write the information in XML language.

Website elements Markup language
Web page HTML
RSS XML
Metadata Columns in the database
RDF A standard for unifying metadata. A model in a database?
Markup language A language that uses elements represented by <> to indicate the structure of data

Add gem

Gemfile


gem 'feedjira'
gem 'httparty'
$ bundle install

Flow of processing the target RSS with Rails and sending it to the client

  1. Send RSS to rails
  2. Parse the received RSS in a manageable form
  3. Return the parsed and object data to the client in JSON format

This time, I will write the server side processing, so I will look at the second and third coding.

Convert the received RSS to XML format data

python



rss = Feed.new(url: params[:url])
xml = HTTParty.get(rss).body

Parse the converted XML data into object format data.

python



obj = Feedjira.parse(xml)
What is parsing This is the process of analyzing the data to determine whether it can be handled (rss format data), and if it can be handled, even converting it.

Decompose the object data so that the article content can be obtained

python


list = []

obj.entries.each do |item|
  list += [
    :title => item.title,
    :url => item.url,
    :title => item.summary,
    :published => item.published.to_time.strftime("%Y-%m-%d %H:%M:%S")
  ]
end

You can get the content of the article by making the data an element of the array. ʻObj.entries` is a block of article content of an object.

Create the data to return to the client

python


data = {
  feed: {
    id: rss.id,
    url: rss.url,
    title: obj.title,
    item: list    
  }
}

render :json => data

Return to the client in JSON format.

Recommended Posts