Adding a Feed Class & CLI Interface to My Twitter Bot
After reading the blog post “What I Wish a Ruby Programmer Had Told Me One Year Ago” and reading the code for his ToDo app I realized I should create a Feed class that has CRUD operations so I can list, add, edit, and delete my feeds. It’s probably not the cleanest or most “ruby way” of doing it (if you have any suggestions please let me know) but it got the job done and I’ll refactor it to make it more rubish and/or turn it into a Sinatra app next.
#class that stores feedsclassFeedattr_accessor:filedefinitialize(file='feeds.txt')# sets file to save feeds to, sets @feed array, and calls the list method @file=file@feeds=[]enddefcreate# takes a feed from the user and adds it to the file puts"Add feed"puts'>>'new_feed=gets.chompFile.open@file,'a'do|f|f.putsnew_feedendputs"added the feed #{new_feed}"enddefget_feeds#gets the feeds from the file and puts them in the @feeds array so they can be used by the .edit and .delete methodsFile.open(@file).readlines.eachdo|f|@feeds<<f.stripendenddefwrite_feeds#writes the @feeds array to the file so .edit and .delete can write the @feeds array they've changed to the file File.open@file,'w'do|f|@feeds.each{|feed|f.puts(feed)}endenddefedit(name)#takes a file name from the user and then checks if that exists in the @feeds array. get_feeds#If it does it gets the id and asks the user what they want to change the name to and then changes that index to the new name.#From here it calls the write_feeds method and writes the new array to the file if@feeds.include?(name)index=@feeds.index(name)puts"What do you want to change #{name} to?"new_feed=gets.chomp@feeds[index]=new_feedwrite_feedselseputs"no such feed"endlistenddefdelete(name)#works almost the same as the .edit method but instead of changing the feed name it deletes itget_feedsif@feeds.include?(name)index=@feeds.index(name)puts"Are you sure you want to delete #{name}?"puts"Enter 'yes' or 'no':"answer=gets.chomp.downcaseifanswer=="yes"@feeds.delete_if{|feed|feed==name}write_feedsputs"Feed #{name} has been deleted."elseputs"Ok I won't delete it then."endelseputs"Sorry don't have that file."endenddeflist#if the feeds.txt file is empty it asks for feeds, if not it puts the feeds from the file read_file=File.read@fileifread_file.empty?puts"the file is empty please add some feeds"createelseputs"here is the list of feeds from file #{@file}"putsread_fileendendend
After finishing this I decided I wanted to add some sort of command line interface so I could list out, add, edit, and delete the feeds from my terminal. I also added the -tweet command which runs the FeedParser class and begins tweeting out the feeds. I realize there is an options parser library I could use for this but I wanted to design and build my own.
require_relative'feed'require_relative'feed_parser'classCommandLineInterface#allows user to interact with feed.rb to list, add, edit, and delete definitialize@feeds=Feed.newtake_commandsenddeftake_commandsputs"What do you want to do? To see list of commands enter -commands"untilfalseresponse=gets.chomp.downcaseparse_response(response)endenddefparse_response(response)ifresponse=="-commands"puts"-list - list your feeds"puts"-add - add a new feed"puts"-edit - edit an existing feed"puts"-delete - delete an existing feed"puts"-tweet - tweets out the feeds"puts"-quit - quit this program"elsecaseresponsewhen"-list"@feeds.listwhen"-add"@feeds.addwhen"-edit"puts"feed name:"name=gets.chomp@feeds.edit(name)when"-delete"puts"feed name:"name=gets.chomp@feeds.delete(name)when"-tweet"parser=FeedParser.new(@feeds.get_feeds)parser.runwhen"-quit"exitelsep'not a valid command'endendtake_commandsendendcli=CommandLineInterface.new