I've written this class that returns feed updates, but am thinking it can be further improved. It's not glitchy or anything, but as a new ruby developer, I reckon it's always good to improve :-)
class FeedManager attr_accessor :feed_object, :update, :new_entries require 'feedtosis' def initialize(feed_url) @feed_object = Feedtosis::Client.new(feed_url) fetch end def fetch @feed_object.fetch end def update @updates = fetch end def updated? @updates.new_entries.count > 0 ? true : false end def new_entries @updates.new_entries end end
As you can see, it's quite simple, but the things I'm seeing that aren't quite right are:
- Whenever I call fetch via terminal, it prints a list with the updates, when it's really supposed return an object.
So as an example, in the terminal if I do something like:
client = Feedtosis::Client.new('http://stackoverflow.com/feeds') result = client.fetch
I then get:
Which is exactly what I'd expect. However, when doing the same thing with "inniting" class with:
I'm getting the object returning as an array with all the items on the feed.
Sure I'm doing something wrong, so any help refactoring this class will he greatly appreciated.
Also, I'd like to see comments about my implementation, as well as any sort of comment to make it better would be welcome.
Thanks in advance
self.feed_object = Feedtosis::Client.new(feed_url)
def updates (reload = true)
@updates = reload ? fetch : @updates
updates(false).new_entries.count > 0
Now you can get the updates as follows:
result = FeedManager.new("http://stackoverflow.com/feeds").updates
PS: I have removed the attr_accessor for :update, and :new_entries.
I have added code to enable conditional cache reload.
feed = FeedManager.new("http://stackoverflow.com/feeds")
updates = feed.updates # reloads the updates
# do something
updates = feed.updates(false) # get the updates from cache.
count > 0 ? true : falsecan be just
count > 0
It appears that you expect the initialize method to return the result of calling update. Initialize is basically a constructor in Ruby, so it will return the new FeedManager object.
It's also very "unusual" to place a require statement in the middle of a class definition.