Skip to content

Commit d911ea4

Browse files
refactor(slack-bot): decouple text class to use private method
1 parent 39a93a8 commit d911ea4

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

lib/command/slack_command.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
require 'httparty'
33
require 'nokogiri'
44

5-
require_relative './slack_crawler'
5+
require_relative './../crawler/joke_text'
66

77
module 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|

lib/crawler/hello.rb

Whitespace-only changes.

lib/crawler/joke_image.rb

Whitespace-only changes.

lib/crawler/joke_text.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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

0 commit comments

Comments
 (0)