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

require 'soap/wsdlDriver'

begin
driver = SOAP::WSDLDriverFactory.
new("http://api.google.com/GoogleSearch.wsdl").create_rpc_driver
rescue
puts "Error creating rpc driver: " + $!
return
end

for i in (0...iterations) do
begin
result = driver.doGoogleSearch(
user.google_api_key, anchor, 0 + i, 10 + i, true, "", false, "", "", "")
rescue SOAP::FaultError
puts "Got Fault: " + $!
return
end

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
rank.save
return value
end
end

# shorter than 10 means no next result
return if result.resultElements.size < 10
end
end

0 Comments:

Prześlij komentarz

Links to this post:

Utwórz link

<< Home