-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.rb
33 lines (26 loc) · 762 Bytes
/
post.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
require 'open-uri'
class Post
attr_accessor :title, :author, :description, :url, :created_at
RSS_URL = "http://rss.groups.yahoo.com/group/Houston-RoR/rss"
def self.all
posts = []
REXML::Document.new(messages_xml).elements.to_a("//item").each do |element|
posts << create_post_from_xml(element)
end
posts
end
def self.messages_xml
URI.parse(RSS_URL).read
end
def self.create_post_from_xml(xml)
post = Post.new
post.title = xml.elements['title'].text
post.description = xml.elements['description'].text rescue nil
post.created_at = Time.parse(xml.elements['pubDate'].text)
post.author = xml.elements['dc:creator'].text
post.url = xml.elements['link'].text
post
rescue:
post
end
end