While a lot depends on several other aspects of your project, this specific
problem depends on a principle question:
Do you have any control over the feed object?
If you have any control, then you could make a polymorphic superclass called
Feed and go on from there where each separate feed has its own related
methods to deal with its idiosyncrasies and polymorphic methods that deal
with common tasks. For ex:
class Feed
def initialize
end
def parse_content
end
end
class BbcFeed < Feed
def parse_content
# do something relevant here
# probably return a string/hash/or content object
end
# define other methods specific to BbcFeed etc
end
Once this is done, you just have to create the right object at runtime,
perhaps have a factory object detailing this.
If you don't have control, there isn't a way to run away from doing checks
on the object. Depending on how you determine the regex to use, you could
have specific solutions. Perhaps each feed has a different URL that you
could use to determine the parsing, or perhaps something else in the
xml...either way, you have to know what object you are dealing with, or you
have to write a parser that deals with it.
From your pseudo code, it would seem that you are still not thinking in
terms of object oriented code. For ex:
clean_up(feed)
parse(feed)
If you have to do it using OO, you probably want to assign those behaviors
to specific objects. But don't listen to me, I am not very good at this.
Hope this helps.
Jayanth
···
On Thu, Mar 26, 2009 at 11:51 AM, Adam Akhtar <adamtemporary@gmail.com>wrote:
although im making a rails app this is more specific to ruby and general
coding.
I want to access several preknown rss feeds, parse and extract the
relevant data and store it into an array. The feeds all have similiar
content but the structure differs. That means my extracting regexps etc
will differ for each feed.
I want to do something like this
items =
feeds.each do |feed|
clean_up(feed) #squeeze and strip stuff etc
item = parse(feed)
items << item
end
return items
where the code in parse() will vary depedning on the feed.
i know of the template design patter and can implement it but how will i
select the correct parse method for a given feed?
will i have to have a case control structure in the block which will
determine which feed is currently being processed and then apply the
correct parse method? If i had 50 feeds that could be one huge case
structure.. is there a more elegant way?
any tips would be greatly appreciated.
--
Posted via http://www.ruby-forum.com/\.