feedjira 2.2.0 → 3.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +635 -6
  3. data/.travis.yml +1 -1
  4. data/CHANGELOG.md +6 -12
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +5 -5
  7. data/README.md +37 -99
  8. data/Rakefile +5 -5
  9. data/feedjira.gemspec +27 -19
  10. data/lib/feedjira.rb +69 -41
  11. data/lib/feedjira/configuration.rb +3 -8
  12. data/lib/feedjira/core_ext.rb +3 -3
  13. data/lib/feedjira/core_ext/date.rb +1 -1
  14. data/lib/feedjira/core_ext/time.rb +2 -2
  15. data/lib/feedjira/date_time_utilities.rb +2 -2
  16. data/lib/feedjira/date_time_utilities/date_time_pattern_parser.rb +2 -2
  17. data/lib/feedjira/feed.rb +10 -80
  18. data/lib/feedjira/feed_entry_utilities.rb +4 -4
  19. data/lib/feedjira/parser.rb +4 -1
  20. data/lib/feedjira/parser/atom.rb +3 -3
  21. data/lib/feedjira/parser/atom_entry.rb +1 -1
  22. data/lib/feedjira/parser/atom_feed_burner.rb +4 -4
  23. data/lib/feedjira/parser/atom_feed_burner_entry.rb +1 -1
  24. data/lib/feedjira/parser/atom_youtube.rb +2 -2
  25. data/lib/feedjira/parser/atom_youtube_entry.rb +1 -1
  26. data/lib/feedjira/parser/google_docs_atom.rb +3 -3
  27. data/lib/feedjira/parser/google_docs_atom_entry.rb +1 -1
  28. data/lib/feedjira/parser/itunes_rss_item.rb +1 -1
  29. data/lib/feedjira/parser/json_feed.rb +39 -0
  30. data/lib/feedjira/parser/json_feed_item.rb +51 -0
  31. data/lib/feedjira/parser/podlove_chapter.rb +1 -1
  32. data/lib/feedjira/parser/rss.rb +1 -1
  33. data/lib/feedjira/parser/rss_entry.rb +5 -1
  34. data/lib/feedjira/parser/rss_feed_burner.rb +1 -1
  35. data/lib/feedjira/preprocessor.rb +1 -1
  36. data/lib/feedjira/version.rb +1 -1
  37. data/spec/feedjira/configuration_spec.rb +9 -16
  38. data/spec/feedjira/date_time_utilities_spec.rb +20 -20
  39. data/spec/feedjira/feed_entry_utilities_spec.rb +18 -18
  40. data/spec/feedjira/feed_spec.rb +15 -229
  41. data/spec/feedjira/feed_utilities_spec.rb +72 -72
  42. data/spec/feedjira/parser/atom_entry_spec.rb +34 -34
  43. data/spec/feedjira/parser/atom_feed_burner_entry_spec.rb +16 -16
  44. data/spec/feedjira/parser/atom_feed_burner_spec.rb +121 -119
  45. data/spec/feedjira/parser/atom_spec.rb +78 -76
  46. data/spec/feedjira/parser/atom_youtube_entry_spec.rb +38 -38
  47. data/spec/feedjira/parser/atom_youtube_spec.rb +15 -15
  48. data/spec/feedjira/parser/google_docs_atom_entry_spec.rb +8 -8
  49. data/spec/feedjira/parser/google_docs_atom_spec.rb +23 -21
  50. data/spec/feedjira/parser/itunes_rss_item_spec.rb +37 -37
  51. data/spec/feedjira/parser/itunes_rss_owner_spec.rb +5 -5
  52. data/spec/feedjira/parser/itunes_rss_spec.rb +118 -116
  53. data/spec/feedjira/parser/json_feed_item_spec.rb +79 -0
  54. data/spec/feedjira/parser/json_feed_spec.rb +53 -0
  55. data/spec/feedjira/parser/podlove_chapter_spec.rb +12 -12
  56. data/spec/feedjira/parser/rss_entry_spec.rb +30 -30
  57. data/spec/feedjira/parser/rss_feed_burner_entry_spec.rb +32 -32
  58. data/spec/feedjira/parser/rss_feed_burner_spec.rb +47 -45
  59. data/spec/feedjira/parser/rss_spec.rb +36 -36
  60. data/spec/feedjira/preprocessor_spec.rb +6 -6
  61. data/spec/feedjira_spec.rb +145 -0
  62. data/spec/sample_feeds.rb +27 -26
  63. data/spec/sample_feeds/HuffPostCanada.xml +279 -0
  64. data/spec/sample_feeds/json_feed.json +156 -0
  65. data/spec/spec_helper.rb +5 -5
  66. metadata +31 -49
  67. data/fixtures/vcr_cassettes/fetch_failure.yml +0 -62
  68. data/fixtures/vcr_cassettes/parse_error.yml +0 -222
  69. data/fixtures/vcr_cassettes/success.yml +0 -281
  70. data/spec/sample_feeds/InvalidDateFormat.xml +0 -20
@@ -3,9 +3,9 @@ language: ruby
3
3
  cache: bundler
4
4
 
5
5
  rvm:
6
- - 2.1
7
6
  - 2.2
8
7
  - 2.3
8
+ - 2.4
9
9
  - jruby-9
10
10
  - ruby-head
11
11
 
@@ -1,19 +1,13 @@
1
1
  # Feedjira Changelog
2
2
 
3
- ## 2.2.0
3
+ ## 3.0.0
4
4
 
5
+ * Breaking Changes
6
+ * `Feedjira::Feed.parse` has moved to `Feedjira.parse`
7
+ * `Feedjira::Feed.fetch_and_parse` has been removed. See README examples for
8
+ how to request XML and parse.
5
9
  * General
6
- * Backport support for parsing new iTunes podcasting tags
7
-
8
- ## 2.1.4
9
-
10
- * Bug fixes
11
- * Prevent errors when a feed has multiple dates and some are unparseable
12
-
13
- ## 2.1.3
14
-
15
- * Enhancements
16
- * No longer log date parsing errors as warnings, they are now debug messages
10
+ * Drop support for Ruby 2.1
17
11
 
18
12
  ## 2.1.1
19
13
 
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at mikeastock@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile CHANGED
@@ -1,11 +1,11 @@
1
- source 'https://rubygems.org/'
1
+ source "https://rubygems.org/"
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'pry'
5
+ gem "pry"
6
6
 
7
7
  group :test do
8
- gem 'oga'
9
- gem 'ox', platforms: [:mri, :rbx]
10
- gem 'rake'
8
+ gem "oga"
9
+ gem "ox", platforms: [:mri, :rbx]
10
+ gem "rake"
11
11
  end
data/README.md CHANGED
@@ -9,96 +9,34 @@
9
9
  [gitter-badge]: https://badges.gitter.im/feedjira/feedjira.svg
10
10
  [gitter]: https://gitter.im/feedjira/feedjira?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge
11
11
 
12
- Feedjira (formerly Feedzirra) is a Ruby library designed to fetch and parse
13
- feeds as quickly as possible.
12
+ Feedjira is a Ruby library designed to parse feeds.
14
13
 
15
- ## Getting Started
14
+ ## Installation
16
15
 
17
- Feedjira is tested with Ruby version 1.9.3 and 2.x so like any Ruby gem, the
18
- first step is to install the gem:
19
-
20
- ```
21
- $ gem install feedjira
22
- ```
23
-
24
- Or add it to your Gemfile:
16
+ Add this line to your application's Gemfile:
25
17
 
26
18
  ```ruby
27
19
  gem "feedjira"
28
20
  ```
29
21
 
30
- ## Fetching and Parsing
31
-
32
- For many users, the `fetch_and_parse` method is what they use Feedjira for. This
33
- method takes a url and returns a Parser object:
22
+ ## Parsing
34
23
 
35
- ```ruby
36
- url = "http://feedjira.com/blog/feed.xml"
37
- feed = Feedjira::Feed.fetch_and_parse(url)
38
- # => #<Feedjira::Parser::Atom...>
39
- ```
40
-
41
- These feed objects have both the meta data for a feed and an `entries`
42
- collection that contains all the entries that were found:
24
+ An example of parsing a feed with Feedjira:
43
25
 
44
26
  ```ruby
45
- feed.title
46
- # => "Feedjira Blog"
47
- feed.url
48
- # => "http://feedjira.com/blog"
49
- feed.entries # returns an array of Entry objects
50
- # => [<Feedjira::Feed::Entry ...>, <Feedjira::Feed::Entry ...>, ...]
51
- ```
52
-
53
- These entry objects contain the data parsed from the feed XML:
54
-
55
- ```ruby
56
- entry = feed.entries.first
57
- entry.title
58
- # => "Announcing verison 1.0"
59
- entry.url
60
- # => "http://feedjira.com/blog/2014-02-12-announcing-version-10.html"
61
- ```
62
-
63
- ## Just Parsing
64
-
65
- The parsing functionality of Feedjira has been exposed so that it can be used in
66
- isolation:
67
-
68
- ```ruby
69
- xml = Faraday.get(url).body
70
- feed = Feedjira::Feed.parse xml
27
+ xml = HTTParty.get(url).body
28
+ feed = Feedjira.parse(xml)
71
29
  feed.entries.first.title
72
- # => "Announcing verison 1.0"
73
- ```
74
-
75
- ## Adding a feed parsing class
76
-
77
- When determining which parser to use for a given XML document, the following
78
- list of parser classes is used:
79
-
80
- * `Feedjira::Parser::RSSFeedBurner`
81
- * `Feedjira::Parser::GoogleDocsAtom`
82
- * `Feedjira::Parser::AtomFeedBurner`
83
- * `Feedjira::Parser::Atom`
84
- * `Feedjira::Parser::ITunesRSS`
85
- * `Feedjira::Parser::RSS`
86
-
87
- You can insert your own parser at the front of this stack by calling
88
- `add_feed_class`, like this:
89
-
90
- ```ruby
91
- Feedjira::Feed.add_feed_class(MyAwesomeParser)
30
+ # => "Announcing verison 3.0"
92
31
  ```
93
32
 
94
- Now when you `fetch_and_parse`, `MyAwesomeParser` will be the first one to get a
95
- chance to parse the feed.
33
+ ## Specifying parser
96
34
 
97
35
  If you have the XML and just want to provide a parser class for one parse, you
98
- can specify that using `parse_with`:
36
+ can specify that using `parse` with the parser option:
99
37
 
100
38
  ```ruby
101
- Feedjira::Feed.parse_with(MyAwesomeParser, xml)
39
+ Feedjira.parse(xml, parser: MyAwesomeParser)
102
40
  ```
103
41
 
104
42
  ## Adding attributes to all feeds types / all entries types
@@ -106,7 +44,8 @@ Feedjira::Feed.parse_with(MyAwesomeParser, xml)
106
44
  ```ruby
107
45
  # Add the generator attribute to all feed types
108
46
  Feedjira::Feed.add_common_feed_element("generator")
109
- Feedjira::Feed.fetch_and_parse("http://www.pauldix.net/atom.xml").generator
47
+ xml = HTTParty.get("http://www.pauldix.net/atom.xml").body
48
+ Feedjira.parse(xml).generator
110
49
  # => "TypePad"
111
50
  ```
112
51
 
@@ -123,60 +62,59 @@ end
123
62
 
124
63
  # Fetch a feed containing GeoRss info and print them
125
64
  url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_week.atom"
126
- Feedjira::Feed.fetch_and_parse(url).entries.each do |entry|
65
+ xml = HTTParty.get(url).body
66
+ Feedjira.parse(xml).entries.each do |entry|
127
67
  puts "Elevation: #{entry.elevation}"
128
68
  end
129
69
  ```
130
70
 
131
71
  ## Configuration
132
72
 
133
- #### Stripping whitespace from XML
73
+ ### Parsers
134
74
 
135
- Feedjira can be configured to strip all whitespace but defaults to lstrip only:
75
+ #### Adding a custom parser
136
76
 
137
- ```ruby
138
- Feedjira.configure do |config|
139
- config.strip_whitespace = true
140
- end
141
- ```
142
-
143
- #### Follow redirect limit
144
-
145
- For fetching feeds, the follow redirect limit defaults to 3 but can be set:
77
+ You can insert your own parser at the front of the available parser list by:
146
78
 
147
79
  ```ruby
148
80
  Feedjira.configure do |config|
149
- config.follow_redirect_limit = 5
81
+ config.parsers.unshift(MyAwesomeParser)
150
82
  end
151
83
  ```
152
84
 
153
- #### Request timeout
85
+ Now when you call `Feedjira.parse`, `MyAwesomeParser` will be the first one to
86
+ get a chance to parse the feed.
87
+
88
+ #### Explicitly set all available parsers
154
89
 
155
- The request timeout defaults to 30 but can be set:
90
+ Feedjira can be configured to use a specific set of parsers and in a specific order:
156
91
 
157
92
  ```ruby
158
93
  Feedjira.configure do |config|
159
- config.request_timeout = 45
94
+ config.parsers = [
95
+ Feedjira::Parser::ITunesRSS,
96
+ MyAwesomeParser,
97
+ Feedjira::Parser::RSS
98
+ ]
160
99
  end
161
100
  ```
162
101
 
163
- #### User agent
102
+ #### Stripping whitespace from XML
164
103
 
165
- The default user agent is "Feedjira #{Version}" but can be set:
104
+ Feedjira can be configured to strip all whitespace but defaults to lstrip only:
166
105
 
167
106
  ```ruby
168
107
  Feedjira.configure do |config|
169
- config.user_agent = "Awesome Feed Reader"
108
+ config.strip_whitespace = true
170
109
  end
171
110
  ```
172
111
 
173
- ## Testing
174
-
175
- Feedjira uses [faraday][] to perform requests, so testing Feedjira is really
176
- about [stubbing out faraday requests][stub].
112
+ ## Contributing
177
113
 
178
- [faraday]: https://github.com/lostisland/faraday
179
- [stub]: https://github.com/lostisland/faraday#using-faraday-for-testing
114
+ Bug reports and pull requests are welcome on GitHub at
115
+ https://github.com/feedjira/feedjira. This project is intended to be a safe,
116
+ welcoming space for collaboration, and contributors are expected to adhere to
117
+ the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
180
118
 
181
119
  ## Projects that use Feedjira
182
120
 
data/Rakefile CHANGED
@@ -1,17 +1,17 @@
1
- require 'rspec/core/rake_task'
2
- require 'rubocop/rake_task'
3
- require 'yard'
1
+ require "rspec/core/rake_task"
2
+ require "rubocop/rake_task"
3
+ require "yard"
4
4
 
5
5
  RSpec::Core::RakeTask.new(:spec) do |t|
6
6
  t.verbose = false
7
7
  end
8
8
 
9
9
  RuboCop::RakeTask.new(:rubocop) do |t|
10
- t.options = ['--display-cop-names']
10
+ t.options = ["--display-cop-names"]
11
11
  end
12
12
 
13
13
  YARD::Rake::YardocTask.new do |t|
14
- t.files = ['lib/**/*.rb', '-', 'LICENSE']
14
+ t.files = ["lib/**/*.rb", "-", "LICENSE"]
15
15
  end
16
16
 
17
17
  task default: [:spec, :rubocop]
@@ -1,31 +1,39 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/feedjira/version', __FILE__)
3
2
 
3
+ require File.expand_path("../lib/feedjira/version", __FILE__)
4
+
5
+ # rubocop:disable Metrics/BlockLength
4
6
  Gem::Specification.new do |s|
5
- s.authors = ['Paul Dix', 'Julien Kirch', 'Ezekiel Templin', 'Jon Allured']
6
- s.email = 'feedjira@gmail.com'
7
- s.homepage = 'http://feedjira.com'
8
- s.license = 'MIT'
9
- s.name = 'feedjira'
7
+ s.authors = [
8
+ "Adam Hess",
9
+ "Ezekiel Templin",
10
+ "Jon Allured",
11
+ "Julien Kirch",
12
+ "Michael Stock",
13
+ "Paul Dix",
14
+ ]
15
+ s.email = "feedjira@gmail.com"
16
+ s.homepage = "http://feedjira.com"
17
+ s.license = "MIT"
18
+ s.name = "feedjira"
10
19
  s.platform = Gem::Platform::RUBY
11
- s.summary = 'A feed fetching and parsing library'
20
+ s.summary = "A feed parsing library"
12
21
  s.version = Feedjira::VERSION
13
22
 
14
23
  s.files = `git ls-files`.split("\n")
15
- s.require_paths = ['lib']
24
+ s.require_paths = ["lib"]
16
25
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
26
 
18
- s.required_ruby_version = '>=1.9.3'
27
+ s.required_ruby_version = ">=1.9.3"
19
28
 
20
- s.add_dependency 'faraday', '>= 0.9'
21
- s.add_dependency 'faraday_middleware', '>= 0.9'
22
- s.add_dependency 'loofah', '>= 2.0'
23
- s.add_dependency 'sax-machine', '>= 1.0'
29
+ s.add_dependency "loofah", ">= 2.0"
30
+ s.add_dependency "sax-machine", ">= 1.0"
24
31
 
25
- s.add_development_dependency 'danger'
26
- s.add_development_dependency 'danger-commit_lint'
27
- s.add_development_dependency 'rspec'
28
- s.add_development_dependency 'rubocop', '0.46'
29
- s.add_development_dependency 'vcr'
30
- s.add_development_dependency 'yard', '~> 0.9'
32
+ s.add_development_dependency "danger"
33
+ s.add_development_dependency "danger-commit_lint"
34
+ s.add_development_dependency "rspec"
35
+ s.add_development_dependency "rubocop"
36
+ s.add_development_dependency "vcr"
37
+ s.add_development_dependency "yard"
31
38
  end
39
+ # rubocop:enable Metrics/BlockLength
@@ -1,47 +1,75 @@
1
- require 'zlib'
2
- require 'faraday'
3
- require 'faraday_middleware'
4
- require 'sax-machine'
5
- require 'loofah'
6
- require 'logger'
7
-
8
- require 'feedjira/core_ext'
9
- require 'feedjira/configuration'
10
- require 'feedjira/date_time_utilities/date_time_epoch_parser'
11
- require 'feedjira/date_time_utilities/date_time_language_parser'
12
- require 'feedjira/date_time_utilities/date_time_pattern_parser'
13
- require 'feedjira/date_time_utilities'
14
- require 'feedjira/date_time_utilities'
15
- require 'feedjira/feed_entry_utilities'
16
- require 'feedjira/feed_utilities'
17
- require 'feedjira/feed'
18
- require 'feedjira/parser'
19
- require 'feedjira/parser/rss_entry'
20
- require 'feedjira/parser/rss_image'
21
- require 'feedjira/parser/rss'
22
- require 'feedjira/parser/atom_entry'
23
- require 'feedjira/parser/atom'
24
- require 'feedjira/preprocessor'
25
- require 'feedjira/version'
26
-
27
- require 'feedjira/parser/rss_feed_burner_entry'
28
- require 'feedjira/parser/rss_feed_burner'
29
- require 'feedjira/parser/podlove_chapter'
30
- require 'feedjira/parser/itunes_rss_owner'
31
- require 'feedjira/parser/itunes_rss_category'
32
- require 'feedjira/parser/itunes_rss_item'
33
- require 'feedjira/parser/itunes_rss'
34
- require 'feedjira/parser/atom_feed_burner_entry'
35
- require 'feedjira/parser/atom_feed_burner'
36
- require 'feedjira/parser/google_docs_atom_entry'
37
- require 'feedjira/parser/google_docs_atom'
38
- require 'feedjira/parser/atom_youtube_entry'
39
- require 'feedjira/parser/atom_youtube'
1
+ require "zlib"
2
+ require "sax-machine"
3
+ require "loofah"
4
+ require "logger"
5
+ require "json"
6
+
7
+ require "feedjira/core_ext"
8
+ require "feedjira/configuration"
9
+ require "feedjira/date_time_utilities/date_time_epoch_parser"
10
+ require "feedjira/date_time_utilities/date_time_language_parser"
11
+ require "feedjira/date_time_utilities/date_time_pattern_parser"
12
+ require "feedjira/date_time_utilities"
13
+ require "feedjira/date_time_utilities"
14
+ require "feedjira/feed_entry_utilities"
15
+ require "feedjira/feed_utilities"
16
+ require "feedjira/feed"
17
+ require "feedjira/parser"
18
+ require "feedjira/parser/rss_entry"
19
+ require "feedjira/parser/rss_image"
20
+ require "feedjira/parser/rss"
21
+ require "feedjira/parser/atom_entry"
22
+ require "feedjira/parser/atom"
23
+ require "feedjira/preprocessor"
24
+ require "feedjira/version"
25
+
26
+ require "feedjira/parser/rss_feed_burner_entry"
27
+ require "feedjira/parser/rss_feed_burner"
28
+ require "feedjira/parser/podlove_chapter"
29
+ require "feedjira/parser/itunes_rss_owner"
30
+ require "feedjira/parser/itunes_rss_category"
31
+ require "feedjira/parser/itunes_rss_item"
32
+ require "feedjira/parser/itunes_rss"
33
+ require "feedjira/parser/atom_feed_burner_entry"
34
+ require "feedjira/parser/atom_feed_burner"
35
+ require "feedjira/parser/google_docs_atom_entry"
36
+ require "feedjira/parser/google_docs_atom"
37
+ require "feedjira/parser/atom_youtube_entry"
38
+ require "feedjira/parser/atom_youtube"
39
+ require "feedjira/parser/json_feed"
40
+ require "feedjira/parser/json_feed_item"
40
41
 
41
42
  # Feedjira
42
43
  module Feedjira
43
- class NoParserAvailable < StandardError; end
44
- class FetchFailure < StandardError; end
44
+ NoParserAvailable = Class.new(StandardError)
45
45
 
46
46
  extend Configuration
47
+
48
+ # Parse XML with first compatible parser
49
+ #
50
+ # @example
51
+ # xml = HTTParty.get("http://example.com").body
52
+ # Feedjira.parse(xml)
53
+ def parse(xml, parser: nil, &block)
54
+ parser ||= parser_for_xml(xml)
55
+
56
+ if parser.nil?
57
+ raise NoParserAvailable, "No valid parser for XML."
58
+ end
59
+
60
+ parser.parse(xml, &block)
61
+ end
62
+ module_function :parse
63
+
64
+ # Find compatible parser for given XML
65
+ #
66
+ # @example
67
+ # xml = HTTParty.get("http://example.com").body
68
+ # parser = Feedjira.parser_for_xml(xml)
69
+ # parser.parse(xml)
70
+ def parser_for_xml(xml)
71
+ start_of_doc = xml.slice(0, 2000)
72
+ Feedjira.parsers.detect { |klass| klass.able_to_parse?(start_of_doc) }
73
+ end
74
+ module_function :parser_for_xml
47
75
  end