Class: NewsFetcher::Item

Inherits:
Object
  • Object
show all
Includes:
SetParams, Simple::Printer::Printable
Defined in:
lib/newsfetcher/item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry) ⇒ Item



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/newsfetcher/item.rb', line 15

def initialize(entry)
  if entry.url
    begin
      uri = Addressable::URI.parse(entry.url.strip)
    rescue Addressable::URI::InvalidURIError => e
      raise Error, "Can't parse URL for entry: #{entry.url.inspect}"
    end
  else
    uri = nil
  end
  id = entry.entry_id || uri or raise Error, "Can't determine ID or URL for entry"
  super(
    id: id.to_s,
    uri: uri,
    date: entry.published || Time.now,
    title: entry.title,
    author: entry.respond_to?(:author) ? entry.author&.sub(/^by\s+/i, '') : nil,
    content: (entry.content || entry.summary)&.to_s,
  )
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



9
10
11
# File 'lib/newsfetcher/item.rb', line 9

def author
  @author
end

#contentObject

Returns the value of attribute content.



10
11
12
# File 'lib/newsfetcher/item.rb', line 10

def content
  @content
end

#dateObject

Returns the value of attribute date.



6
7
8
# File 'lib/newsfetcher/item.rb', line 6

def date
  @date
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/newsfetcher/item.rb', line 5

def id
  @id
end

#titleObject

Returns the value of attribute title.



7
8
9
# File 'lib/newsfetcher/item.rb', line 7

def title
  @title
end

#uriObject

Returns the value of attribute uri.



8
9
10
# File 'lib/newsfetcher/item.rb', line 8

def uri
  @uri
end

Instance Method Details

#==(other) ⇒ Object



50
51
52
# File 'lib/newsfetcher/item.rb', line 50

def ==(other)
  @id == other&.id
end

#ageObject



54
55
56
# File 'lib/newsfetcher/item.rb', line 54

def age
  Time.now - @date
end

#eql?(other) ⇒ Boolean



46
47
48
# File 'lib/newsfetcher/item.rb', line 46

def eql?(other)
  @id.eql?(other.id)
end

#printableObject



36
37
38
39
40
41
42
43
44
# File 'lib/newsfetcher/item.rb', line 36

def printable
  [
    [:id, 'ID'],
    :date,
    :title,
    :uri,
    :author,
  ]
end