Class: GReader::Entry
- Inherits:
-
Object
- Object
- GReader::Entry
- Includes:
- Utilities
- Defined in:
- lib/greader/entry.rb
Overview
A feed entry.
Common usage
Getting entries:
feed.entries.each { |entry| }
tag.entries.each { |entry| }
feed.entries['ENTRY_ID'] # See Entry#id below
Common metadata:
entry.title #=> "On pride and prejudice" (or #to_s)
entry.content #=> "<p>There was a time where..."
entry.summary #=> "There was a time where..." (no HTML)
entry.image_url # URL of the first image
More metadata:
entry. #=> "Rico Sta. Cruz"
entry.updated #=> #<Date>
entry.published #=> #<Date>
entry.url #=> "http://ricostacruz.com/on-pride-and-prejudice.html"
entry.id #=> "reader_item_128cb290d31352d9"
Relationships:
entry.feed #=> #<Feed ...>
States and actions:
# Not implemented yet!
entry.read? # Read or not?
entry.starred?
entry.read! # Mark as read
entry.unread! # Mark as unread
Instance Attribute Summary collapse
-
#author ⇒ Object
readonly
Returns the value of attribute author.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#feed ⇒ Object
readonly
Returns the value of attribute feed.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#published ⇒ Object
readonly
Returns the value of attribute published.
-
#title ⇒ Object
(also: #to_s)
readonly
Returns the value of attribute title.
-
#updated ⇒ Object
readonly
Returns the value of attribute updated.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
-
.parse_json(doc) ⇒ Object
Converts a Noko XML node into a simpler Hash.
Instance Method Summary collapse
-
#bare_content ⇒ Object
Returns the #content without HTML tags.
-
#doc ⇒ Object
Returns a Nokogiri document.
- #image? ⇒ Boolean
-
#image_url ⇒ string?
Returns the URL of the first image.
-
#initialize(client = Client.new, options) ⇒ Entry
constructor
Constructor.
- #inspect ⇒ Object
- #raw_image_url ⇒ Object
- #read? ⇒ Boolean
- #starred? ⇒ Boolean
-
#summary ⇒ string?
Returns a short summary.
- #to_param ⇒ Object
Methods included from Utilities
#escape, #kv_map, #slug, #strip_tags
Constructor Details
#initialize(client = Client.new, options) ⇒ Entry
Constructor. Can be called with an options hash or a Nokogiri XML node.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/greader/entry.rb', line 58 def initialize(client=Client.new, ) @client = client @feed = client.feed([:feed]) @author = [:author] @content = GReader.process_html([:content]) @title = [:title] @published = [:published] @updated = [:updated] @url = [:url] @id = [:id] @read = [:read] @starred = [:starred] @options = end |
Instance Attribute Details
#author ⇒ Object (readonly)
Returns the value of attribute author.
44 45 46 |
# File 'lib/greader/entry.rb', line 44 def @author end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
52 53 54 |
# File 'lib/greader/entry.rb', line 52 def client @client end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
43 44 45 |
# File 'lib/greader/entry.rb', line 43 def content @content end |
#feed ⇒ Object (readonly)
Returns the value of attribute feed.
51 52 53 |
# File 'lib/greader/entry.rb', line 51 def feed @feed end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
49 50 51 |
# File 'lib/greader/entry.rb', line 49 def id @id end |
#published ⇒ Object (readonly)
Returns the value of attribute published.
46 47 48 |
# File 'lib/greader/entry.rb', line 46 def published @published end |
#title ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute title.
45 46 47 |
# File 'lib/greader/entry.rb', line 45 def title @title end |
#updated ⇒ Object (readonly)
Returns the value of attribute updated.
47 48 49 |
# File 'lib/greader/entry.rb', line 47 def updated @updated end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
48 49 50 |
# File 'lib/greader/entry.rb', line 48 def url @url end |
Class Method Details
.parse_json(doc) ⇒ Object
Converts a Noko XML node into a simpler Hash.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/greader/entry.rb', line 134 def self.parse_json(doc) summary = (doc['content'] || doc['summary']) summary = summary['content'] if summary.is_a?(Hash) { :url => doc['alternate'].first['href'], :author => doc['author'], :content => summary, :title => doc['title'], :published => Time.new(doc['published']), :updated => Time.new(doc['updated']), :feed => doc['origin']['streamId'], :id => doc['id'], :read => doc['categories'].any? { |s| s =~ /com\.google\/fresh$/ }, :starred => doc['categories'].any? { |s| s =~ /com\.google\/starred$/ } # Also available: comments [], annotations [], enclosure # [{href,type,length}] } rescue NoMethodError raise ParseError.new("Malformed entries", doc) end |
Instance Method Details
#bare_content ⇒ Object
Returns the #content without HTML tags
103 104 105 |
# File 'lib/greader/entry.rb', line 103 def (content) end |
#doc ⇒ Object
Returns a Nokogiri document.
83 84 85 |
# File 'lib/greader/entry.rb', line 83 def doc @doc ||= Nokogiri.HTML(content) end |
#image? ⇒ Boolean
121 122 123 |
# File 'lib/greader/entry.rb', line 121 def image? ! image_url.nil? end |
#image_url ⇒ string?
Returns the URL of the first image
110 111 112 113 114 115 |
# File 'lib/greader/entry.rb', line 110 def image_url url = raw_image_url and begin return nil if url.include?('feedburner.com') # "Share on Facebook" url end end |
#inspect ⇒ Object
74 75 76 |
# File 'lib/greader/entry.rb', line 74 def inspect "#<#{self.class.name} \"#{title}\" (#{url})>" end |
#raw_image_url ⇒ Object
117 118 119 |
# File 'lib/greader/entry.rb', line 117 def raw_image_url img = doc.xpath('*//img').first and img['src'] end |
#read? ⇒ Boolean
125 126 127 |
# File 'lib/greader/entry.rb', line 125 def read? @read end |
#starred? ⇒ Boolean
129 130 131 |
# File 'lib/greader/entry.rb', line 129 def starred? @starred end |
#summary ⇒ string?
Returns a short summary
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/greader/entry.rb', line 90 def summary doc = self.doc.dup # Remove images and empty paragraphs doc.xpath('*//img').each { |tag| tag.remove } doc.xpath('*//*[normalize-space(.)=""]').each { |tag| tag.remove } # The first block el = doc.xpath('//body/p | //body/div').first || doc.xpath('//body').first el and el.text end |
#to_param ⇒ Object
78 79 80 |
# File 'lib/greader/entry.rb', line 78 def to_param slug @id end |