1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* NEWS: add an entry for rss.

* lib/rss/, test/rss/: merge from trunk.
- 0.2.4 -> 0.2.5.
- RSS::Maker.make raise an exception not returns nil for invalid
feed making.
- RSS::Maker.make requires block.
- don't use instance_variable to initialize variables. (speed up)


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@17677 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
kou 2008-06-29 09:12:56 +00:00
parent 8272251f6d
commit 51bd9cada8
16 changed files with 120 additions and 60 deletions

View file

@ -1,3 +1,14 @@
Sun Jun 29 18:09:00 2008 Kouhei Sutou <kou@cozmixng.org>
* NEWS: add an entry for rss.
* lib/rss/, test/rss/: merge from trunk.
- 0.2.4 -> 0.2.5.
- RSS::Maker.make raise an exception not returns nil for invalid
feed making.
- RSS::Maker.make requires block.
- don't use instance_variable to initialize variables. (speed up)
Sun Jun 29 10:59:12 2008 Tanaka Akira <akr@fsij.org> Sun Jun 29 10:59:12 2008 Tanaka Akira <akr@fsij.org>
* math.c (domain_check): fix preprocess condition. * math.c (domain_check): fix preprocess condition.

8
NEWS
View file

@ -22,6 +22,14 @@ with all sufficient information, see the ChangeLog file.
Return an enumerator if no block is given. Return an enumerator if no block is given.
* rss
* 0.2.4 -> 0.2.5
* RSS::Maker.make
* raise an exception not returns nil for invalid feed making.
* requires block.
== Changes since the 1.8.6 release == Changes since the 1.8.6 release
=== Configuration changes === Configuration changes

View file

@ -1,4 +1,3 @@
require 'base64'
require 'rss/parser' require 'rss/parser'
module RSS module RSS

View file

@ -31,7 +31,9 @@ module RSS
self::OTHER_ELEMENTS << variable_name self::OTHER_ELEMENTS << variable_name
end end
def add_need_initialize_variable(variable_name, init_value="nil") def add_need_initialize_variable(variable_name, init_value=nil,
&init_block)
init_value ||= init_block
self::NEED_INITIALIZE_VARIABLES << [variable_name, init_value] self::NEED_INITIALIZE_VARIABLES << [variable_name, init_value]
end end
@ -45,7 +47,7 @@ module RSS
def_delegators("@#{plural}", :push, :pop, :shift, :unshift) def_delegators("@#{plural}", :push, :pop, :shift, :unshift)
def_delegators("@#{plural}", :each, :size, :empty?, :clear) def_delegators("@#{plural}", :each, :size, :empty?, :clear)
add_need_initialize_variable(plural, "[]") add_need_initialize_variable(plural) {[]}
module_eval(<<-EOC, __FILE__, __LINE__ + 1) module_eval(<<-EOC, __FILE__, __LINE__ + 1)
def new_#{name} def new_#{name}
@ -74,7 +76,9 @@ module RSS
def def_classed_element_without_accessor(name, class_name=nil) def def_classed_element_without_accessor(name, class_name=nil)
class_name ||= Utils.to_class_name(name) class_name ||= Utils.to_class_name(name)
add_other_element(name) add_other_element(name)
add_need_initialize_variable(name, "make_#{name}") add_need_initialize_variable(name) do |object|
object.send("make_#{name}")
end
module_eval(<<-EOC, __FILE__, __LINE__ + 1) module_eval(<<-EOC, __FILE__, __LINE__ + 1)
private private
def setup_#{name}(feed, current) def setup_#{name}(feed, current)
@ -185,7 +189,19 @@ module RSS
private private
def initialize_variables def initialize_variables
self.class.need_initialize_variables.each do |variable_name, init_value| self.class.need_initialize_variables.each do |variable_name, init_value|
instance_eval("@#{variable_name} = #{init_value}", __FILE__, __LINE__) if init_value.nil?
value = nil
else
if init_value.respond_to?(:call)
value = init_value.call(self)
elsif init_value.is_a?(String)
# just for backward compatibility
value = instance_eval(init_value, __FILE__, __LINE__)
else
value = init_value
end
end
instance_variable_set("@#{variable_name}", value)
end end
end end
@ -238,7 +254,8 @@ module RSS
def variables def variables
self.class.need_initialize_variables.find_all do |name, init| self.class.need_initialize_variables.find_all do |name, init|
"nil" == init # init == "nil" is just for backward compatibility
init.nil? or init == "nil"
end.collect do |name, init| end.collect do |name, init|
name name
end end
@ -364,7 +381,9 @@ module RSS
%w(xml_stylesheets channel image items textinput).each do |element| %w(xml_stylesheets channel image items textinput).each do |element|
attr_reader element attr_reader element
add_need_initialize_variable(element, "make_#{element}") add_need_initialize_variable(element) do |object|
object.send("make_#{element}")
end
module_eval(<<-EOC, __FILE__, __LINE__) module_eval(<<-EOC, __FILE__, __LINE__)
private private
def setup_#{element}(feed) def setup_#{element}(feed)
@ -392,12 +411,8 @@ module RSS
end end
def make def make
if block_given?
yield(self) yield(self)
to_feed to_feed
else
nil
end
end end
def to_feed def to_feed
@ -405,11 +420,8 @@ module RSS
setup_xml_stylesheets(feed) setup_xml_stylesheets(feed)
setup_elements(feed) setup_elements(feed)
setup_other_elements(feed) setup_other_elements(feed)
if feed.valid? feed.validate
feed feed
else
nil
end
end end
private private

View file

@ -176,7 +176,7 @@ module RSS
%w(hour minute second).each do |name| %w(hour minute second).each do |name|
attr_reader(name) attr_reader(name)
add_need_initialize_variable(name, '0') add_need_initialize_variable(name, 0)
end end
def content=(content) def content=(content)

View file

@ -220,9 +220,7 @@ module RSS
name = (@@class_names[uri] || {})[tag_name] name = (@@class_names[uri] || {})[tag_name]
return name if name return name if name
tag_name = tag_name.gsub(/[_\-]([a-z]?)/) do tag_name = tag_name.gsub(/[_\-]([a-z]?)/) {$1.upcase}
$1.upcase
end
tag_name[0, 1].upcase + tag_name[1..-1] tag_name[0, 1].upcase + tag_name[1..-1]
end end
@ -389,9 +387,7 @@ module RSS
def start_else_element(local, prefix, attrs, ns) def start_else_element(local, prefix, attrs, ns)
class_name = self.class.class_name(_ns(ns, prefix), local) class_name = self.class.class_name(_ns(ns, prefix), local)
current_class = @last_element.class current_class = @last_element.class
if class_name and if known_class?(current_class, class_name)
(current_class.const_defined?(class_name) or
current_class.constants.include?(class_name))
next_class = current_class.const_get(class_name) next_class = current_class.const_get(class_name)
start_have_something_element(local, prefix, attrs, ns, next_class) start_have_something_element(local, prefix, attrs, ns, next_class)
else else
@ -407,6 +403,20 @@ module RSS
end end
end end
if Module.method(:const_defined?).arity == -1
def known_class?(target_class, class_name)
class_name and
(target_class.const_defined?(class_name, false) or
target_class.constants.include?(class_name.to_sym))
end
else
def known_class?(target_class, class_name)
class_name and
(target_class.const_defined?(class_name) or
target_class.constants.include?(class_name))
end
end
NAMESPLIT = /^(?:([\w:][-\w\d.]*):)?([\w:][-\w\d.]*)/ NAMESPLIT = /^(?:([\w:][-\w\d.]*):)?([\w:][-\w\d.]*)/
def split_name(name) def split_name(name)
name =~ NAMESPLIT name =~ NAMESPLIT
@ -504,7 +514,7 @@ module RSS
else else
if klass.have_content? if klass.have_content?
if @last_element.need_base64_encode? if @last_element.need_base64_encode?
text = Base64.decode64(text.lstrip) text = text.lstrip.unpack("m").first
end end
@last_element.content = text @last_element.content = text
end end

View file

@ -45,6 +45,7 @@ class Time
end end
end end
require "English" require "English"
require "rss/utils" require "rss/utils"
require "rss/converter" require "rss/converter"
@ -52,7 +53,7 @@ require "rss/xml-stylesheet"
module RSS module RSS
VERSION = "0.2.4" VERSION = "0.2.5"
URI = "http://purl.org/rss/1.0/" URI = "http://purl.org/rss/1.0/"
@ -1200,7 +1201,7 @@ EOC
__send__(self.class.xml_getter).to_s __send__(self.class.xml_getter).to_s
else else
_content = content _content = content
_content = Base64.encode64(_content) if need_base64_encode? _content = [_content].pack("m").delete("\n") if need_base64_encode?
h(_content) h(_content)
end end
end end

View file

@ -570,7 +570,7 @@ EOA
text << char text << char
char.succ! char.succ!
end end
base64_content = Base64.encode64(Zlib::Deflate.deflate(text)) base64_content = [Zlib::Deflate.deflate(text)].pack("m").delete("\n")
[false, true].each do |with_space| [false, true].each do |with_space|
xml_content = base64_content xml_content = base64_content
@ -1272,7 +1272,9 @@ EOA
invalid_feed_checker=nil) invalid_feed_checker=nil)
_wrap_assertion do _wrap_assertion do
elements = [] elements = []
invalid_feed = false invalid_feed_exception = nil
feed = nil
begin
feed = RSS::Maker.make("atom:#{feed_type}") do |maker| feed = RSS::Maker.make("atom:#{feed_type}") do |maker|
yield maker yield maker
targets = chain_reader(maker, maker_readers) targets = chain_reader(maker, maker_readers)
@ -1281,13 +1283,21 @@ EOA
elements << element if element elements << element if element
end end
if invalid_feed_checker if invalid_feed_checker
invalid_feed = invalid_feed_checker.call(targets) invalid_feed_exception = invalid_feed_checker.call(targets)
end
end
rescue RSS::Error
if invalid_feed_exception.is_a?(RSS::TooMuchTagError)
assert_too_much_tag(invalid_feed_exception.tag,
invalid_feed_exception.parent) do
raise
end
else
raise
end end
end end
if invalid_feed if invalid_feed_exception.nil?
assert_nil(feed)
else
actual_elements = chain_reader(feed, feed_readers) || [] actual_elements = chain_reader(feed, feed_readers) || []
actual_elements = actual_elements.collect do |target| actual_elements = actual_elements.collect do |target|
feed_extractor.call(target) feed_extractor.call(target)
@ -1542,18 +1552,24 @@ EOA
:length => target.length, :length => target.length,
} }
end end
if feed_readers.first == "entries"
parent = "entry"
else
parent = feed_type
end
invalid_feed_checker = Proc.new do |targets| invalid_feed_checker = Proc.new do |targets|
infos = {} infos = {}
invalid = false invalid_exception = nil
targets.each do |target| targets.each do |target|
key = [target.hreflang, target.type] key = [target.hreflang, target.type]
if infos.has_key?(key) if infos.has_key?(key)
invalid = true invalid_exception = RSS::TooMuchTagError.new("link", parent)
break break
end end
infos[key] = true if target.rel.nil? or target.rel == "alternate" infos[key] = true if target.rel.nil? or target.rel == "alternate"
end end
invalid invalid_exception
end end
invalid_feed_checker = nil if allow_duplication invalid_feed_checker = nil if allow_duplication
_assert_maker_atom_elements(feed_type, maker_readers, feed_readers, _assert_maker_atom_elements(feed_type, maker_readers, feed_readers,

View file

@ -658,7 +658,7 @@ module RSS
content.content = original_content content.content = original_content
xml = REXML::Document.new(content.to_s).root xml = REXML::Document.new(content.to_s).root
assert_rexml_element([], {"type" => type}, assert_rexml_element([], {"type" => type},
Base64.encode64(original_content), xml) [original_content].pack("m").delete("\n"), xml)
end end
end end

View file

@ -6,8 +6,9 @@ module RSS
class TestMaker09 < TestCase class TestMaker09 < TestCase
def test_rss def test_rss
rss = RSS::Maker.make("0.91") assert_raise(LocalJumpError) do
assert_nil(rss) RSS::Maker.make("0.91")
end
rss = RSS::Maker.make("0.9") do |maker| rss = RSS::Maker.make("0.9") do |maker|
setup_dummy_channel(maker) setup_dummy_channel(maker)

View file

@ -6,6 +6,10 @@ module RSS
class TestMaker10 < TestCase class TestMaker10 < TestCase
def test_rdf def test_rdf
assert_raise(LocalJumpError) do
RSS::Maker.make("1.0")
end
rss = RSS::Maker.make("1.0") do |maker| rss = RSS::Maker.make("1.0") do |maker|
setup_dummy_channel(maker) setup_dummy_channel(maker)
setup_dummy_item(maker) setup_dummy_item(maker)
@ -48,9 +52,6 @@ module RSS
link = "http://hoge.com" link = "http://hoge.com"
description = "fugafugafugafuga" description = "fugafugafugafuga"
rss = RSS::Maker.make("1.0")
assert_nil(rss)
rss = RSS::Maker.make("1.0") do |maker| rss = RSS::Maker.make("1.0") do |maker|
maker.channel.about = about maker.channel.about = about
maker.channel.title = title maker.channel.title = title

View file

@ -6,8 +6,9 @@ module RSS
class TestMaker20 < TestCase class TestMaker20 < TestCase
def test_rss def test_rss
rss = RSS::Maker.make("2.0") assert_raise(LocalJumpError) do
assert_nil(rss) RSS::Maker.make("2.0")
end
rss = RSS::Maker.make("2.0") do |maker| rss = RSS::Maker.make("2.0") do |maker|
setup_dummy_channel(maker) setup_dummy_channel(maker)

View file

@ -86,11 +86,10 @@ module RSS
elems.each do |name, values, plural| elems.each do |name, values, plural|
dc_elems = item.__send__("dc_#{plural}") dc_elems = item.__send__("dc_#{plural}")
values.each do |value| values.each do |value|
dc_elems.__send__("new_#{name}") do |elem| elem = dc_elems.__send__("new_#{name}")
elem.value = value elem.value = value
end end
end end
end
setup_dummy_textinput(maker) setup_dummy_textinput(maker)
set_multiple_elements(maker.textinput, elems) set_multiple_elements(maker.textinput, elems)

View file

@ -462,7 +462,7 @@ module RSS
"all of your answers here.", "all of your answers here.",
maker_readers, feed_readers) maker_readers, feed_readers)
_assert_maker_itunes_summary("This week we talk about surviving in a " + _assert_maker_itunes_summary("This week we talk about surviving in a " +
"Red state if youre a Blue person. Or " + "Red state if you're a Blue person. Or " +
"vice versa.", "vice versa.",
maker_readers, feed_readers) maker_readers, feed_readers)
end end

View file

@ -509,3 +509,4 @@ EOR
end end
end end
end end

View file

@ -3,7 +3,7 @@ require "rss-testcase"
module RSS module RSS
class TestVersion < TestCase class TestVersion < TestCase
def test_version def test_version
assert_equal("0.2.4", ::RSS::VERSION) assert_equal("0.2.5", ::RSS::VERSION)
end end
end end
end end