13aal 13aal - 6 months ago 20
Ruby Question

Generating random URL with real words

I've written a little script that will generate a random URL, it works, however I would like the generated URL to be somewhat believable, meaning I want it to generate real words. As of now it generates 7 random characters and digits.

def generate_url(length=7)
protocall = %w(https:// http://)
body = rand(36**length).to_s(36)
num = rand(1..999).to_s
url = "#{protocall.sample.to_s}#{body}.com/php?id=#{num}"
puts url
end

#<= http://s857yi5.com/php?id=168
#<= https://6rm0oq3.com/php?id=106
#<= http://skhvk1n.com/php?id=306


So what I'm looking for is an easier way to do this with real words in place of the random 7 character string (please keep it between 7 and 10 characters) without using an external gem

My OS is Windows 7

Answer

Try faker is a cool gem to generate words, emails, urls, or whatever you need

https://github.com/stympy/faker

I've used on many proyect.

hb@hora ~ » irb
2.2.3 :001 > require 'faker'
 => true 
2.2.3 :002 > Faker::Lorem.sentence(3)
 => "Ea esse ex." 
2.2.3 :003 > Faker::Lorem.sentence(3)
 => "Fugiat odio harum." 
2.2.3 :004 > Faker::Lorem.words
 => ["consequuntur", "labore", "optio"] 
2.2.3 :005 > Faker::Lorem.word
 => "error" 
2.2.3 :006 > 

But if you are not able to add external gem you can create your own array/dictionary

2.2.3 :013 > dict
 => ["Editors", "and", "critics", "of", "the", "plays", "disdaining", "the", "showiness", "and", "melodrama", "of", "Shakespearean", "stage", "representation", "began", "to", "focus", "on", "Shakespeare", "as", "a", "dramatic", "poet", "to", "be", "studied", "on", "the", "printed", "page", "rather", "than", "in", "the", "theatre", "The", "rift", "between", "Shakespeare", "on", "the", "stage", "and", "Shakespeare", "on", "the", "page", "was", "at", "its", "widest", "in", "the", "early", "19th", "century", "at", "a", "time", "when", "both", "forms", "of", "Shakespeare", "were", "hitting", "peaks", "of", "fame", "and", "popularity", "theatrical"] 
2.2.3 :014 > dict.sample
 => "the" 
2.2.3 :015 > dict.sample
 => "a" 
2.2.3 :016 > dict.sample
 => "disdaining" 
2.2.3 :017 > dict.sample
 => "century" 
2.2.3 :018 > 

that dictionary was created doing a copy paste from wikipedia's text to my own irb and then scanning all /w+/

2.2.3 :023 > dict='n his own time, William Shakespeare (1564–1616) was rated as merely one among many talented playwrights and poets, but since the late 17th century he has been considered the supreme playwright and poet of the English language.'
 => "n his own time, William Shakespeare (1564–1616) was rated as merely one among many talented playwrights and poets, but since the late 17th century he has been considered the supreme playwright and poet of the English language." 
2.2.3 :024 > dict.scan(/\w+/)
 => ["n", "his", "own", "time", "William", "Shakespeare", "1564", "1616", "was", "rated", "as", "merely", "one", "among", "many", "talented", "playwrights", "and", "poets", "but", "since", "the", "late", "17th", "century", "he", "has", "been", "considered", "the", "supreme", "playwright", "and", "poet", "of", "the", "English", "language"]