lib/urban_pass/generate.rb in urban_pass-0.0.4.1 vs lib/urban_pass/generate.rb in urban_pass-0.1.0
- old
+ new
@@ -1,8 +1,9 @@
require 'open-uri'
require 'nokogiri'
require 'clipboard'
+require 'thread'
module UrbanPass
class Generate
def generate_word
@@ -12,22 +13,27 @@
# Remove the extra spaces
remove_spaces(urban)
end
def generate_phrase
- arr = []
+ threads = []
4.times do
- word = random_word
- arr << remove_spaces(word)
+ threads << Thread.new { Thread.current["word"] = random_word }
end
- pass_phrase = arr.join
+ phrase = [] # two arrays > string concatenation
+ threads.each {|t| t.join; phrase << t["word"]}
+ phrase = phrase.join
+ pass_phrase = remove_spaces(phrase)
copy(pass_phrase)
return pass_phrase
end
def random_word
page = Nokogiri::HTML(open("http://urbandictionary.com/random.php"))
page.css('a.word')[0].text
+ rescue SocketError
+ puts "Your not connected to the internet, silly!"
+ exit
end
def remove_spaces(phrase)
phrase.gsub(" ", "")
end