Class: SourceServices::Synchronize

Inherits:
Object
  • Object
show all
Defined in:
app/service_objects/source_services/synchronize.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Synchronize


6
7
8
# File 'app/service_objects/source_services/synchronize.rb', line 6

def initialize(source)
  @source = source
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source


4
5
6
# File 'app/service_objects/source_services/synchronize.rb', line 4

def source
  @source
end

Class Method Details

.callObject


33
34
35
# File 'app/service_objects/source_services/synchronize.rb', line 33

def call
  Source.all.map { |s| new(s).delay.call }
end

Instance Method Details

#callObject


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/service_objects/source_services/synchronize.rb', line 10

def call
  latest_entry      = source.articles.last

  filtered_entries  = Feedjira::Feed.fetch_and_parse(source.uri).entries.take_while do |e|
    latest_entry.nil? || e.entry_id == latest_entry.entry_id
  end

  created_entries   = filtered_entries.map do |e|
    source.articles.create(
      entry_id:     e.entry_id,
      uri:          e.url,
      description:  e.summary,
      title:        e.title,
      categories:   e.categories,
      author:       e.author,
      published_at: e.published
    )
  end

  created_entries.count {|o| o.valid? }
end