Class: ChuckNorrisJokes::ChuckNorris
- Inherits:
-
Object
- Object
- ChuckNorrisJokes::ChuckNorris
- Defined in:
- lib/chuck_norris_jokes/chuck_norris.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
Returns the value of attribute api.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #give_me_lot_of_jokes_having(term) ⇒ Object
-
#initialize(name = "Chuck Norris") ⇒ ChuckNorris
constructor
A new instance of ChuckNorris.
- #show_me_joke_categories ⇒ Object
- #tell_me_a_joke_now ⇒ Object
- #tell_me_a_joke_now_from_category(cat) ⇒ Object
Constructor Details
#initialize(name = "Chuck Norris") ⇒ ChuckNorris
Returns a new instance of ChuckNorris.
8 9 10 11 |
# File 'lib/chuck_norris_jokes/chuck_norris.rb', line 8 def initialize(name="Chuck Norris") @name = name @api = Api.new end |
Instance Attribute Details
#api ⇒ Object
Returns the value of attribute api.
7 8 9 |
# File 'lib/chuck_norris_jokes/chuck_norris.rb', line 7 def api @api end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/chuck_norris_jokes/chuck_norris.rb', line 7 def name @name end |
Instance Method Details
#give_me_lot_of_jokes_having(term) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/chuck_norris_jokes/chuck_norris.rb', line 42 def give_me_lot_of_jokes_having(term) begin resp = api.search term parsed = JSON.parse(resp.body.gsub! "Chuck Norris", name) parsed["result"].collect{|res| res["value"]} rescue ChuckNorrisJokes::ChuckNorrisJokesError=> exception puts "Chuck Norris ain't happy because #{exception}" end end |
#show_me_joke_categories ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/chuck_norris_jokes/chuck_norris.rb', line 23 def show_me_joke_categories begin resp = api.get_categories parsed = JSON.parse resp.body rescue ChuckNorrisJokes::ChuckNorrisJokesError=> exception puts "Chuck Norris ain't happy because #{exception}" end end |
#tell_me_a_joke_now ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/chuck_norris_jokes/chuck_norris.rb', line 13 def tell_me_a_joke_now begin resp = api.get_random parsed = JSON.parse(resp.body.gsub! "Chuck Norris", name) parsed["value"] rescue ChuckNorrisJokes::ChuckNorrisJokesError=> exception puts "Chuck Norris ain't happy because #{exception}" end end |
#tell_me_a_joke_now_from_category(cat) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/chuck_norris_jokes/chuck_norris.rb', line 32 def tell_me_a_joke_now_from_category(cat) begin resp = api.get_random_in_category cat parsed = JSON.parse resp.body parsed["value"].gsub! "Chuck Norris", name rescue ChuckNorrisJokes::ChuckNorrisJokesError=> exception puts "Chuck Norris ain't happy because #{exception}" end end |