class HaarJoke::Joke
This class can be used to create joke instances. The .text method returns the joke text.
Attributes
text[R]
Private Instance Methods
accept_joke?(joke)
click to toggle source
# File lib/haar_joke/joke.rb, line 33 def accept_joke?(joke) filters = HaarJoke.config['filters'].join('|') # rejecting jokes that don't match "Chuck Norris" to avoid complicated # and hard to precict substitions on variations of Chuck Norris, like # an element called Chuckium or something. joke.match(/#{filters}/i).nil? && !joke.match(/Chuck Norris/).nil? end
generate_joke()
click to toggle source
# File lib/haar_joke/joke.rb, line 13 def generate_joke joke = joke_from_api !joke.nil? ? substitute_terms(joke) : 'Zzzzzzzzzzzzzzz. Haar is sleeping.' end
joke_from_api()
click to toggle source
# File lib/haar_joke/joke.rb, line 18 def joke_from_api api_url = 'http://api.icndb.com/jokes/random' joke = '' open(api_url) do |stream| joke = JSON.parse(stream.read) end joke = joke['value']['joke'] accept_joke?(joke) ? joke : joke_from_api rescue StandardError nil end
substitute_terms(joke)
click to toggle source
# File lib/haar_joke/joke.rb, line 41 def substitute_terms(joke) substitutions = HaarJoke.config['substitutions'] substitutions.each do |key, value| joke.gsub!(/#{key}/i, value) end joke.gsub(""", "\"") end