Using gsub to replace text with link from database

I’m really close to solving this that I can smell it. In a nutshell I’m
trying to create this:

I have an app that has a database of baseball players.
I’m trying to create a feature where you can copy and paste your blog
text, the feature will scan the text and return the text with the
proper html links of my app that point to the player. For example, I’m
a blog writer, I create a entry with one sentence “I am a big fan of
Derek Jeter and Jorge Posada of the Yankees” and I click submit. I want
the output to return the same sentence, but with the link of Derek Jeter
and Jorge Posada.

This is where I’m at:
#This code is taking the sentence and scanning it for two words that
are next to each other that have the first letter capitalized
@new_link_array = #this is the link data that is returned from my query
search via ferret. It will provide the proper link to the players.
@full_text = params[:q]
@full_text_array = @full_text.scan(/([A-Z]+[a-zA-Z]* [A-Z]+[a-zA-Z]*)/)

@full_text_array.each_with_index do |original_name, i|
@full_text.gsub!(original_name.to_s, @new_link_array[i])
end

results = @full_text.scan(/([A-Z]+[a-zA-Z]* [A-Z]+[a-zA-Z]*)/)

names_links = {}

results.each do |name|
unless names_links[name]
names_links[name] = method_to_construct_link_for_name(name)
end
end

names_links.each_pair do |name, link|
@full_text.gsub!(name, link)
end

The results are not consistent. I was wondering if somebody could help
me out.

-A

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs