Permalink
Browse files

Merge branch 'parser-dsl'

  • Loading branch information...
nakajima committed Jan 31, 2009
2 parents a757b41 + 22133c9 commit 6fb4c37e77f5b28d15126d7804be1b1702feb45c
View
@@ -0,0 +1,5 @@
+class Object
+ def metaclass
+ class << self; self end
+ end
+end
View
@@ -2,17 +2,25 @@
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
+require 'sax-machine'
+require 'activesupport'
+require 'curb'
+
+require 'core_ext/object'
+
require 'feedzirra/feed_utilities'
require 'feedzirra/feed_entry_utilities'
require 'feedzirra/feed'
-require 'feedzirra/atom'
require 'feedzirra/atom_entry'
-require 'feedzirra/atom_feed_burner'
require 'feedzirra/atom_feed_burner_entry'
-require 'feedzirra/rdf'
require 'feedzirra/rdf_entry'
-require 'feedzirra/rss'
require 'feedzirra/rss_entry'
+require 'feedzirra/parser'
+
+parsers = Dir[File.join(File.dirname(__FILE__), *%w[feedzirra parsers *])]
+parsers.each { |lib| require lib }
+
+# Feed parsers
module Feedzirra
VERSION = "0.0.1"
View
@@ -1,17 +0,0 @@
-require 'sax-machine'
-require 'feedzirra/atom_entry'
-
-module Feedzirra
- class Atom
- include SAXMachine
- include FeedUtilities
- element :title
- element :link, :as => :url, :value => :href, :with => {:type => "text/html"}
- element :link, :as => :feed_url, :value => :href, :with => {:type => "application/atom+xml"}
- elements :entry, :as => :entries, :class => AtomEntry
-
- def self.able_to_parse?(xml)
- xml =~ /Atom/
- end
- end
-end
@@ -1,6 +1,3 @@
-require 'sax-machine'
-require 'feedzirra/feed_utilities'
-
module Feedzirra
class AtomEntry
include SAXMachine
@@ -1,17 +0,0 @@
-require 'sax-machine'
-require 'feedzirra/atom_feed_burner_entry'
-
-module Feedzirra
- class AtomFeedBurner
- include SAXMachine
- include FeedUtilities
- element :title
- element :link, :as => :url, :value => :href, :with => {:type => "text/html"}
- element :link, :as => :feed_url, :value => :href, :with => {:type => "application/atom+xml"}
- elements :entry, :as => :entries, :class => AtomFeedBurnerEntry
-
- def self.able_to_parse?(xml)
- (xml =~ /Atom/ && xml =~ /feedburner/) || false
- end
- end
-end
View
@@ -1,8 +1,3 @@
-require 'feedzirra/atom'
-require 'feedzirra/atom_feed_burner'
-require 'curb'
-require 'activesupport'
-
module Feedzirra
class NoParserAvailable < StandardError; end
@@ -27,7 +22,7 @@ def self.add_feed_class(klass)
end
def self.feed_classes
- @feed_classes ||= [RSS, RDF, AtomFeedBurner, Atom]
+ @feed_classes ||= []
end
# can take a single url or an array of urls
View
@@ -0,0 +1,21 @@
+module Feedzirra
+ def self.parses_feed(name, *patterns, &block)
+ parser = Class.new do
+ attr_accessor :feed_url
+
+ include SAXMachine
+ include FeedUtilities
+
+ class_eval(&block)
+ end
+
+ parser.metaclass.instance_eval do
+ define_method("able_to_parse?") do |content|
+ patterns.all? { |pattern| content =~ pattern }
+ end
+ end
+
+ Feed.add_feed_class(parser)
+ const_set(name, parser)
+ end
+end
@@ -0,0 +1,6 @@
+Feedzirra.parses_feed('Atom', /Atom/) do
+ element :title
+ element :link, :as => :url, :value => :href, :with => {:type => "text/html"}
+ element :link, :as => :feed_url, :value => :href, :with => {:type => "application/atom+xml"}
+ elements :entry, :as => :entries, :class => Feedzirra::AtomEntry
+end
@@ -0,0 +1,6 @@
+Feedzirra.parses_feed('AtomFeedBurner', /Atom/, /feedburner/) do
+ element :title
+ element :link, :as => :url, :value => :href, :with => {:type => "text/html"}
+ element :link, :as => :feed_url, :value => :href, :with => {:type => "application/atom+xml"}
+ elements :entry, :as => :entries, :class => Feedzirra::AtomFeedBurnerEntry
+end
@@ -0,0 +1,5 @@
+Feedzirra.parses_feed('RDF', /rdf\:RDF/) do
+ element :title
+ element :link, :as => :url
+ elements :item, :as => :entries, :class => Feedzirra::RDFEntry
+end
@@ -0,0 +1,5 @@
+Feedzirra.parses_feed('RSS', /rss version\=\"2\.0\"/) do
+ element :title
+ element :link, :as => :url
+ elements :item, :as => :entries, :class => Feedzirra::RSSEntry
+end
View
@@ -1,18 +0,0 @@
-require 'sax-machine'
-require 'feedzirra/rdf_entry'
-
-module Feedzirra
- class RDF
- include SAXMachine
- include FeedUtilities
- element :title
- element :link, :as => :url
- elements :item, :as => :entries, :class => RDFEntry
-
- attr_accessor :feed_url
-
- def self.able_to_parse?(xml)
- xml =~ /rdf\:RDF/ || false
- end
- end
-end
View
@@ -1,18 +0,0 @@
-require 'sax-machine'
-require 'feedzirra/rss_entry'
-
-module Feedzirra
- class RSS
- include SAXMachine
- include FeedUtilities
- element :title
- element :link, :as => :url
- elements :item, :as => :entries, :class => RSSEntry
-
- attr_accessor :feed_url
-
- def self.able_to_parse?(xml)
- xml =~ /rss version\=\"2\.0\"/
- end
- end
-end

0 comments on commit 6fb4c37

Please sign in to comment.