Class: GReader::Entry

Inherits:
Object
  • Object
show all
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.author          #=> "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

Class Method Summary collapse

Instance Method Summary collapse

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, options={})
  @client  = client

  @feed      = client.feed(options[:feed])
  @author    = options[:author]
  @content   = GReader.process_html(options[:content])
  @title     = options[:title]
  @published = options[:published]
  @updated   = options[:updated]
  @url       = options[:url]
  @id        = options[:id]
  @read      = options[:read]
  @starred   = options[:starred]
  @options = options
end

Instance Attribute Details

#authorObject (readonly)

Returns the value of attribute author.



44
45
46
# File 'lib/greader/entry.rb', line 44

def author
  @author
end

#clientObject (readonly)

Returns the value of attribute client.



52
53
54
# File 'lib/greader/entry.rb', line 52

def client
  @client
end

#contentObject (readonly)

Returns the value of attribute content.



43
44
45
# File 'lib/greader/entry.rb', line 43

def content
  @content
end

#feedObject (readonly)

Returns the value of attribute feed.



51
52
53
# File 'lib/greader/entry.rb', line 51

def feed
  @feed
end

#idObject (readonly)

Returns the value of attribute id.



49
50
51
# File 'lib/greader/entry.rb', line 49

def id
  @id
end

#publishedObject (readonly)

Returns the value of attribute published.



46
47
48
# File 'lib/greader/entry.rb', line 46

def published
  @published
end

#titleObject (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

#updatedObject (readonly)

Returns the value of attribute updated.



47
48
49
# File 'lib/greader/entry.rb', line 47

def updated
  @updated
end

#urlObject (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 => DateTime.new(doc['published']),
    :updated   => DateTime.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_contentObject

Returns the #content without HTML tags



103
104
105
# File 'lib/greader/entry.rb', line 103

def bare_content
  strip_tags(content)
end

#docObject

Returns a Nokogiri document.



83
84
85
# File 'lib/greader/entry.rb', line 83

def doc
  @doc ||= Nokogiri.HTML(content)
end

#image?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/greader/entry.rb', line 121

def image?
  ! image_url.nil?
end

#image_urlstring?

Returns the URL of the first image

Returns:

  • (string)
  • (nil)


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

#inspectObject



74
75
76
# File 'lib/greader/entry.rb', line 74

def inspect
  "#<#{self.class.name} \"#{title}\" (#{url})>"
end

#raw_image_urlObject



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

Returns:

  • (Boolean)


125
126
127
# File 'lib/greader/entry.rb', line 125

def read?
  @read
end

#starred?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/greader/entry.rb', line 129

def starred?
  @starred
end

#summarystring?

Returns a short summary

Returns:

  • (string)
  • (nil)


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_paramObject



78
79
80
# File 'lib/greader/entry.rb', line 78

def to_param
  slug @id
end