This blog is no longer updated. We have moved to trix.pl/blog. Please update your bookmarks.
Track keywords in Ruby on Rails using Google API
Below is essential part of code which tracks ranks of keywords in Google. As almost always in Ruby world, this source code is self-explanatory. It was partially inspired by this Bernard Peh's article. If you are PHP developer and willing to become programming language polyglot, compare length of relevant code and its readablity. If you think this snippet sucks in any way, comment on! Some variables in the code are defined outside of its scope, but it's not hard to find which.
Bonus tip: Ruby SOAP translates Google API field URL from resultElement structure to method uRL!
def update_link_rank(iterations = 10)
# sanity chceck
iterations = 10 if iterations < 1 || iterations > 100
driver = SOAP::WSDLDriverFactory.
puts "Error creating rpc driver: " + $!
for i in (0...iterations) do
result = driver.doGoogleSearch(
user.google_api_key, anchor, 0 + i, 10 + i, true, "", false, "", "", "")
puts "Got Fault: " + $!
partial_rank = 0
for resultElement in result.resultElements do
partial_rank += 1
if resultElement.uRL.include? href then
value = i * 10 + partial_rank
rank = LinkRank.new
rank.link = self
rank.value = value
# shorter than 10 means no next result
return if result.resultElements.size < 10