File tree Expand file tree Collapse file tree 4 files changed +39
-2
lines changed
Expand file tree Collapse file tree 4 files changed +39
-2
lines changed Original file line number Diff line number Diff line change 22require 'httparty'
33require 'nokogiri'
44
5- require_relative './slack_crawler '
5+ require_relative './../crawler/joke_text '
66
77module Joker
88 module Commands
99 # Slack bot commands
1010 class Jokes < SlackRubyBot ::Commands ::Base
11+ text_joke = Text . new
1112 command 'jokes_image' do |client , data , _match |
1213 client . say ( channel : data . channel , text : Image . image_random_joke )
1314 end
1415
1516 command 'jokes_text' do |client , data , _match |
16- client . say ( channel : data . channel , text : Text . text_random_joke )
17+ client . say ( channel : data . channel , text : text_joke . text_random_joke )
1718 end
1819
1920 command 'say_hello' do |client , data , _match |
Original file line number Diff line number Diff line change 1+ require 'httparty'
2+ require 'nokogiri'
3+
4+ # Text based jokes implementation
5+ class Text
6+ def initialize
7+ @text_url = 'https://upjoke.com/programmer-jokes'
8+ unparsed_page_text = HTTParty . get ( @text_url )
9+ @parsed_page_text = Nokogiri ::HTML ( unparsed_page_text . body )
10+ end
11+
12+ def text_jokes
13+ my_array = [ ]
14+ my_text_jokes = @parsed_page_text . css ( '.joke-wrapper' ) . map ( &:text )
15+ if my_text_jokes . is_a? Array
16+ my_text_jokes
17+ else
18+ my_array
19+ end
20+ end
21+
22+ def text_random_joke
23+ if text_jokes . any?
24+ text_jokes . sample
25+ else
26+ 'Whoops, something is not quite right!, try again'
27+ end
28+ end
29+
30+ private :text_jokes
31+ end
32+
33+ text = Text . new
34+
35+ # puts text_jokes.is_a? Array
36+ puts text . text_random_joke
You can’t perform that action at this time.
0 commit comments