I found a SERP checker written in PHP and I decided, in order to better learn to program, I would re-write it in Ruby.
All I have written so far takes a list of keywords that a user inputs and cleans the list and turns each keyword into a url for Google search.
Here is the code:
require 'sinatra'
require 'rspec'
get '/serp_checker' do
"<form action='/ranked' method='post'>
<label for='keyword'>Keyword</label>
<textarea name='keyword' id='keyword' type='text' /></textarea>
<label for='url'>URL</label>
<input name='url' id='url' type='text' />
<input type='submit' value='Go!' />
</form>"
end
def clean_up_keywords(str)
str.gsub("\n", ",").delete("\r").split(',')
end
def clean_up_list(arr)
arr.reject(&:empty?).each(&:lstrip!)
end
def make_strings_url_friendly(arr)
arr.each do |e|
e.gsub!(" ", "+")
end
end
def make_urls(arr)
arr.map {|e| "http://www.google.com/search?num=100&q=" + e}
end
post '/ranked' do
dirty_list = clean_up_keywords(params[:keyword])
clean_list = clean_up_list(dirty_list)
url_ready_list = make_strings_url_friendly(clean_list)
url_list = make_urls(url_ready_list)
end
Here is the spec:
require_relative '../lib/rankypanky.rb'
describe "#clean_up_keywords" do
it "should push items separated by a newline into array as separate items" do
clean_up_keywords("apples\noranges").should == ["apples", "oranges"]
end
it "should delete all \r chars" do
clean_up_keywords("Chat\r").should == ["Chat"]
end
it "should push items separated by commas into an array" do
clean_up_keywords("Chat, Meta, About").should == ["Chat", " Meta", " About"]
end
it "should push items separated ONLY by a \s char into array as one item" do
clean_up_keywords("New York, apples").should == ["New York", " apples"]
end
end
describe "#clean_up_list" do
it "should not include empty strings/items in array" do
clean_up_list(["apples", "", "oranges"]).should == ["apples", "oranges"]
end
it "should remove any leading white space from items in array" do
clean_up_list([" oranges", "apples"]).should == ["oranges", "apples"]
end
end
describe "#make_strings_url_friendly" do
it "should replace /s with a +" do
make_strings_url_friendly(["oranges apples"]).should == ["oranges+apples"]
end
end
describe "#make_urls" do
it "should append array items to http://www.google.com/search?num=100&q=" do
make_urls(["pie"]).should == ["http://www.google.com/search?num=100&q=pie"]
end
end
Basically I'm looking for any better ways to do this as well as whether my code meets hmmm standards... just some general critique. Thank you so much!