Class: Igotjokes::Client

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

Class Method Summary collapse

Class Method Details

.get_jokesObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/igotjokes/client.rb', line 6

def self.get_jokes
  url = 'https://api.jokes.one'
  type = ['jod', 'animal', 'blonde', 'knock-knock'].sample
  conn = Faraday.new(url: url) do |build|
    build.request :url_encoded
    build.use Igotjokes::Logs
    build.adapter  Faraday.default_adapter
  end

  response = conn.get('jod') do |req|
    req.params['category'] = type
  end

  data = JSON.parse(response.body)
  jokes = data['contents']['jokes']

  jokes
end