program design - template design pattern

Discussion in 'Ruby' started by Adam Akhtar, Mar 26, 2009.

  1. Adam Akhtar

    Adam Akhtar Guest

    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/.
     
    Adam Akhtar, Mar 26, 2009
    #1
    1. Advertising

  2. [Note: parts of this message were removed to make it a legal post.]

    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 <>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/.
    >
    >
     
    Srijayanth Sridhar, Mar 26, 2009
    #2
    1. Advertising

  3. 2009/3/26 Srijayanth Sridhar <>:
    > While a lot depends on several other aspects of your project, this specif=

    ic
    > problem depends on a principle question:
    >
    > Do you have any control over the feed object?


    I second that. Also, what type is "feed"? That's not clear to me.

    > If you have any control, then you could make a polymorphic superclass cal=

    led
    > 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
    > =A0 def initialize
    > =A0 end
    > =A0 def parse_content
    > =A0 end


    One could also add

    # download the feed
    def fetch(uri)
    ....
    end

    > end
    >
    > class BbcFeed < Feed
    > =A0 def parse_content
    > =A0 =A0 =A0# do something relevant here
    > =A0 =A0 =A0# probably return a string/hash/or content object
    > =A0 end
    > =A0 # 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.


    Or stuff the relationship between feed URI's and class that deals with
    it in a Hash:

    FEEDS =3D {
    'http://foo.bar/feed.rdf' =3D> BbcFeed,
    'http://baz.boz.buz/path.rdf' =3D> BbcFeed,
    'http://what.ever.you/like' =3D> OtherFeed,
    }.freeze

    Then you can do

    FEEDS.each |uri, cl|
    feed_handler =3D cl.new
    feed_handler.fetch
    feed_handler.parse_content
    end

    > If you don't have control, there isn't a way to run away from doing check=

    s
    > 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 y=

    ou
    > have to write a parser that deals with it.


    Feed type checks could be done on the URI or the content. Depending
    on that a different approach needs to be followed but the basic
    pattern stays the same.

    Kind regards

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
     
    Robert Klemme, Mar 26, 2009
    #3
  4. [Note: parts of this message were removed to make it a legal post.]

    On Thu, Mar 26, 2009 at 4:23 PM, Robert Klemme
    <>wrote:

    > 2009/3/26 Srijayanth Sridhar <>:
    > > 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?

    >
    > I second that. Also, what type is "feed"? That's not clear to me.
    >
    > > 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

    >
    > One could also add
    >
    > # download the feed
    > def fetch(uri)
    > ....
    > 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.

    >
    > Or stuff the relationship between feed URI's and class that deals with
    > it in a Hash:
    >
    > FEEDS = {
    > 'http://foo.bar/feed.rdf' => BbcFeed,
    > 'http://baz.boz.buz/path.rdf' => BbcFeed,
    > 'http://what.ever.you/like' => OtherFeed,
    > }.freeze
    >
    > Then you can do
    >
    > FEEDS.each |uri, cl|
    > feed_handler = cl.new
    > feed_handler.fetch
    > feed_handler.parse_content
    > end
    >
    > > 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.

    >
    > Feed type checks could be done on the URI or the content. Depending
    > on that a different approach needs to be followed but the basic
    > pattern stays the same.



    Yeah I thought of that too, but God knows what his criterion for choosing
    the parser are. It might be more complex than just a simple uri mapping.


    Jayanth


    >
    > Kind regards
    >
    > robert
    >
    > --
    > remember.guy do |as, often| as.you_can - without end
    >
    >
     
    Srijayanth Sridhar, Mar 26, 2009
    #4
  5. Adam Akhtar

    Adam Akhtar Guest

    Hi all,

    thanks for you comments. Yes Im referring to rss feeds and i am in full
    controll of how they are represented. Im choosing the feed addresses,
    fetching them and parsing them.

    I actually got confused between my patterns, it was the strategy pattern
    not the template pattern. But at the end of the day it all boiled down
    to how I could dynamically initialize each feed_parser and thats where i
    got stuck mainly due to lack of programming experience.

    I didnt know I could store class names in hashes but after reading up on
    Ruby Desing Patterns i found out they are objects themselves.

    So Ill think ill use the hash idea!

    Thank you all so much for your suggestions.

    --
    Posted via http://www.ruby-forum.com/.
     
    Adam Akhtar, Mar 27, 2009
    #5
  6. 2009/3/27 Adam Akhtar <>:

    > I actually got confused between my patterns, it was the strategy pattern
    > not the template pattern. But at the end of the day it all boiled down
    > to how I could dynamically initialize each feed_parser and thats where i
    > got stuck mainly due to lack of programming experience.
    >
    > I didnt know I could store class names in hashes but after reading up on


    Actually those were not class names but class _objects_ themselves in
    my example!

    > Ruby Desing Patterns i found out they are objects themselves.
    >
    > So Ill think ill use the hash idea!


    So I conclude from that that you know which feed needs which parsing algorithm.

    > Thank you all so much for your suggestions.


    You're welcome!

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
     
    Robert Klemme, Mar 27, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shawn
    Replies:
    5
    Views:
    350
  2. sunny
    Replies:
    1
    Views:
    474
    Salt_Peter
    Dec 7, 2006
  3. Pallav singh
    Replies:
    0
    Views:
    391
    Pallav singh
    Jan 22, 2012
  4. Pallav singh
    Replies:
    0
    Views:
    422
    Pallav singh
    Jan 22, 2012
  5. Pallav singh
    Replies:
    1
    Views:
    465
    Peter Remmers
    Jan 22, 2012
Loading...

Share This Page