Class: Dadjoke::Joke

Inherits:
Object
  • Object
show all
Defined in:
lib/dadjoke.rb

Overview

Fetch dad jokes from icanhazdadjoke.com

Class Method Summary collapse

Class Method Details

.randomString

Fetch a random joke

Returns:

  • a dad joke



21
22
23
# File 'lib/dadjoke.rb', line 21

def self.random
  HTTParty.get(@url, headers: @headers)["joke"]
end

.search(term, limit = 3) ⇒ Array<String>

Search for jokes based on a term

Parameters:

  • the search term

  • (defaults to: 3)

    the number of jokes to return (default: 3, max: 30)

Returns:

  • an array of jokes



30
31
32
33
34
35
36
37
38
# File 'lib/dadjoke.rb', line 30

def self.search(term, limit = 3)
  query = { term:, limit: }
  response = HTTParty.get(
    "#{@url}/search",
    headers: @headers,
    query:
  )
  response["results"].map { |result| result["joke"] }
end