Class: Omnom::Source::Base

Inherits:
Object
  • Object
show all
Includes:
ParserMethods
Defined in:
lib/omnom/source/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ParserMethods

#html_to_text

Constructor Details

#initialize(settings = {}, options = {}) ⇒ Base

Returns a new instance of Base.



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/omnom/source/base.rb', line 88

def initialize(settings={}, options={})
  settings ||= {}
  options ||= {}
  @settings = self.class.settings.dup.merge(settings)
  @options = options
  @url = @settings[:url]
  @key = self.class.key
  @source_id = Digest::SHA1.hexdigest("#{@key}-#{hash_to_consistent_string(@options)}")
  @config = self.class.config
  validate_config
  after_initialize
end

Class Attribute Details

.settingsObject



22
23
24
25
26
27
# File 'lib/omnom/source/base.rb', line 22

def settings
  @settings ||= {
    key: key,
    guid_namespace: key
  }
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



86
87
88
# File 'lib/omnom/source/base.rb', line 86

def config
  @config
end

#feed_keyObject

Returns the value of attribute feed_key.



85
86
87
# File 'lib/omnom/source/base.rb', line 85

def feed_key
  @feed_key
end

#keyObject (readonly)

Returns the value of attribute key.



86
87
88
# File 'lib/omnom/source/base.rb', line 86

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.



86
87
88
# File 'lib/omnom/source/base.rb', line 86

def options
  @options
end

#settingsObject (readonly)

Returns the value of attribute settings.



86
87
88
# File 'lib/omnom/source/base.rb', line 86

def settings
  @settings
end

#source_idObject (readonly)

Returns the value of attribute source_id.



86
87
88
# File 'lib/omnom/source/base.rb', line 86

def source_id
  @source_id
end

Class Method Details

.configObject



29
30
31
32
# File 'lib/omnom/source/base.rb', line 29

def config
  config_keys = full_key.split('__')
  config_keys.inject(Omnom.config) { |config, key| config = config[key] unless config.nil? }
end

.configureObject

Implement configuration code in child class if necessary (e.g. code that would be in an initializer)



19
20
# File 'lib/omnom/source/base.rb', line 19

def configure
end

.cron(cron) ⇒ Object



42
43
44
# File 'lib/omnom/source/base.rb', line 42

def cron(cron)
  settings[:cron] = cron
end

.every(every) ⇒ Object



46
47
48
# File 'lib/omnom/source/base.rb', line 46

def every(every)
  settings[:every] = every
end

.feed_url(url) ⇒ Object



50
51
52
53
# File 'lib/omnom/source/base.rb', line 50

def feed_url(url)
  settings[:feed_url] = url
  settings[:icon] = icon_from_url(url) unless settings[:icon].present?
end

.full_keyObject



34
35
36
# File 'lib/omnom/source/base.rb', line 34

def full_key
  name.sub('Omnom::Source::', '').underscore.gsub('/', '__')
end

.guid_namespace(guid_namespace) ⇒ Object



55
56
57
# File 'lib/omnom/source/base.rb', line 55

def guid_namespace(guid_namespace)
  settings[:guid_namespace] = guid_namespace
end

.icon(url) ⇒ Object



59
60
61
# File 'lib/omnom/source/base.rb', line 59

def icon(url)
  settings[:icon] = url
end

.icon_from_url(url) ⇒ Object



76
77
78
79
80
# File 'lib/omnom/source/base.rb', line 76

def icon_from_url(url)
  return if url.blank?
  uri = URI(url)
  "http://#{uri.host}/favicon.ico"
end

.inherited(child) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/omnom/source/base.rb', line 7

def inherited(child)
  Omnom::FeedSourcesDSL.create_source_method(child)
  Omnom.add_source(child)

  # Inherit settings of the superclass if it isn't a base source class (e.g. Base, Atom)
  superclass_is_a_base_source_class = child.superclass.name.split('::').length <= 3
  if !superclass_is_a_base_source_class && child.superclass.respond_to?(:settings)
    child.settings = child.superclass.settings.dup
  end
end

.keyObject



38
39
40
# File 'lib/omnom/source/base.rb', line 38

def key
  full_key.sub(/__default$/, '')
end

.required_config(*keys) ⇒ Object



63
64
65
# File 'lib/omnom/source/base.rb', line 63

def required_config(*keys)
  settings[:required_config] = keys
end

.required_options(*keys) ⇒ Object



67
68
69
# File 'lib/omnom/source/base.rb', line 67

def required_options(*keys)
  settings[:required_options] = keys
end

.url(url) ⇒ Object



71
72
73
74
# File 'lib/omnom/source/base.rb', line 71

def url(url)
  settings[:url] = url
  settings[:icon] = icon_from_url(url) unless settings[:icon].present?
end

Instance Method Details

#after_initializeObject



101
102
# File 'lib/omnom/source/base.rb', line 101

def after_initialize
end

#updateObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/omnom/source/base.rb', line 104

def update
  return unless Omnom.is_database_ready?
  set_agent
  authenticate
  set_page
  update_posts
  filter_past_posts
  @page = nil
  @agent = nil
end