Class: Omnom::Feed

Inherits:
Object
  • Object
show all
Defined in:
lib/omnom/feed.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFeed

Returns a new instance of Feed.



32
33
34
# File 'lib/omnom/feed.rb', line 32

def initialize
  @options = self.class.options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



30
31
32
# File 'lib/omnom/feed.rb', line 30

def options
  @options
end

Class Method Details

.filter(&block) ⇒ Object



12
13
14
# File 'lib/omnom/feed.rb', line 12

def filter(&block)
  options[:filter] = block
end

.inherited(child) ⇒ Object



4
5
6
# File 'lib/omnom/feed.rb', line 4

def inherited(child)
  Omnom.add_feed(child)
end

.keyObject



25
26
27
# File 'lib/omnom/feed.rb', line 25

def key
  name.underscore.gsub('/', '__')
end

.optionsObject



8
9
10
# File 'lib/omnom/feed.rb', line 8

def options
  @options ||= {key: self.key}
end

.sources(&block) ⇒ Object



16
17
18
19
# File 'lib/omnom/feed.rb', line 16

def sources(&block)
  config = FeedSourcesDSL.new(&block)
  options[:sources] = config.sources
end

.template(template) ⇒ Object



21
22
23
# File 'lib/omnom/feed.rb', line 21

def template(template)
  options[:template] = template
end

Instance Method Details

#keyObject



36
37
38
# File 'lib/omnom/feed.rb', line 36

def key
  self.class.key
end

#postsObject



48
49
50
51
52
53
54
# File 'lib/omnom/feed.rb', line 48

def posts
  source_ids = sources.collect(&:source_id)
  Post.includes(:posts_origins).where(
    'omnom_posts_origins.feed_key' => key,
    'omnom_posts_origins.source_id' => source_ids
  )
end

#source_by_source_id(source_id) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/omnom/feed.rb', line 86

def source_by_source_id(source_id)
  source_id = source_id.to_s
  sources.each do |source|
    return source if source.source_id == source_id
  end
  nil
end

#source_by_source_key(source_key) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/omnom/feed.rb', line 94

def source_by_source_key(source_key)
  source_key = source_key.to_s
  sources.each do |source|
    return source if source.key == source_key
  end
  nil
end

#sourcesObject



77
78
79
80
81
82
83
84
# File 'lib/omnom/feed.rb', line 77

def sources
  if @options[:sources]
    @options[:sources].each do |source|
      source.feed_key = key
    end
  end
  @options[:sources]
end

#templateObject



44
45
46
# File 'lib/omnom/feed.rb', line 44

def template
  @options[:template] || :default
end

#titleObject



40
41
42
# File 'lib/omnom/feed.rb', line 40

def title
  key.gsub(/_feed$/, '').humanize
end

#updateObject



56
57
58
59
60
# File 'lib/omnom/feed.rb', line 56

def update
  sources.each do |source|
    source.update
  end
end

#update_by_source_key(source_key = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/omnom/feed.rb', line 62

def update_by_source_key(source_key=nil)
  if source_key
    source = source_by_source_key(source_key)
    raise "Source with source key '#{source_key}' not found in feed #{self.class}" and return if source.nil?
    source.update
  else
    return if sources.nil?
    sources.each(&:update)
  end
end

#update_source(source) ⇒ Object



73
74
75
# File 'lib/omnom/feed.rb', line 73

def update_source(source)
  source.update
end