Class: Feedjira::Feed

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

Class Method Summary collapse

Class Method Details

.add_common_feed_element(element_tag, options = {}) ⇒ Object



33
34
35
36
37
# File 'lib/feedjira/feed.rb', line 33

def add_common_feed_element(element_tag, options = {})
  feed_classes.each do |k|
    k.element element_tag, options
  end
end

.add_common_feed_elements(element_tag, options = {}) ⇒ Object



39
40
41
42
43
# File 'lib/feedjira/feed.rb', line 39

def add_common_feed_elements(element_tag, options = {})
  feed_classes.each do |k|
    k.elements element_tag, options
  end
end

.add_common_feed_entry_element(element_tag, options = {}) ⇒ Object



45
46
47
# File 'lib/feedjira/feed.rb', line 45

def add_common_feed_entry_element(element_tag, options = {})
  call_on_each_feed_entry :element, element_tag, options
end

.add_common_feed_entry_elements(element_tag, options = {}) ⇒ Object



49
50
51
# File 'lib/feedjira/feed.rb', line 49

def add_common_feed_entry_elements(element_tag, options = {})
  call_on_each_feed_entry :elements, element_tag, options
end

.add_feed_class(klass) ⇒ Object



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

def add_feed_class(klass)
  feed_classes.unshift klass
end

.call_on_each_feed_entry(method, *parameters) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/feedjira/feed.rb', line 53

def call_on_each_feed_entry(method, *parameters)
  feed_classes.each do |klass|
    klass.sax_config.collection_elements.each_value do |value|
      collection_configs = value.select do |v|
        v.accessor == 'entries' && v.data_class.class == Class
      end

      collection_configs.each do |config|
        config.data_class.send(method, *parameters)
      end
    end
  end
end

.connection(url) ⇒ Object

rubocop:disable LineLength



81
82
83
84
85
86
# File 'lib/feedjira/feed.rb', line 81

def connection(url)
  Faraday.new(url: url, headers: headers, request: request_options) do |conn|
    conn.use FaradayMiddleware::FollowRedirects, limit: Feedjira.follow_redirect_limit
    conn.adapter Faraday.default_adapter
  end
end

.determine_feed_parser_for_xml(xml) ⇒ Object



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

def determine_feed_parser_for_xml(xml)
  start_of_doc = xml.slice(0, 2000)
  feed_classes.detect { |klass| klass.able_to_parse?(start_of_doc) }
end

.feed_classesObject



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

def feed_classes
  @feed_classes ||= Feedjira.parsers
end

.fetch_and_parse(url) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/feedjira/feed.rb', line 67

def fetch_and_parse(url)
  response = connection(url).get
  unless response.success?
    raise FetchFailure, "Fetch failed - #{response.status}"
  end
  feed = parse response.body
  feed.feed_url = url
  feed.etag = response.headers['etag'].to_s.delete '"'

  feed.last_modified = parse_last_modified(response)
  feed
end

.parse(xml, &block) ⇒ Object

Raises:



10
11
12
13
14
# File 'lib/feedjira/feed.rb', line 10

def parse(xml, &block)
  parser = determine_feed_parser_for_xml(xml)
  raise NoParserAvailable, 'No valid parser for XML.' unless parser
  parse_with parser, xml, &block
end

.parse_with(parser, xml, &block) ⇒ Object



6
7
8
# File 'lib/feedjira/feed.rb', line 6

def parse_with(parser, xml, &block)
  parser.parse xml, &block
end

.reset_parsers!Object



29
30
31
# File 'lib/feedjira/feed.rb', line 29

def reset_parsers!
  @feed_classes = nil
end