Skip to content

Commit 97b1e48

Browse files
feat(slack-bot): add singleton methods
1 parent a8ea07b commit 97b1e48

File tree

5 files changed

+18
-39
lines changed

5 files changed

+18
-39
lines changed

.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Style/FrozenStringLiteralComment:
2+
Enable: false

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ GEM
8080
diff-lcs (>= 1.2.0, < 2.0)
8181
rspec-support (~> 3.10.0)
8282
rspec-support (3.10.2)
83-
rubocop (1.9.0)
83+
rubocop (1.9.1)
8484
parallel (~> 1.10)
8585
parser (>= 3.0.0.0)
8686
rainbow (>= 2.2.2, < 4.0)

Procfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
dev: puma
2-
web: puma
1+
dev: bundle exec rackup
2+
web: bundle exec rackup -p 9292

bin/bot.rb

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,3 @@
33
require 'slack-ruby-bot'
44
require_relative './../lib/command/slack_command'
55
require_relative './../lib/slack-bot'
6-
7-
require 'httparty'
8-
require 'nokogiri'
9-
class Web
10-
def initialize
11-
@base_url = 'https://www.hongkiat.com/blog/programming-jokes/'
12-
unparsed_page = HTTParty.get(@base_url)
13-
@parsed_page = Nokogiri::HTML(unparsed_page.body)
14-
15-
@text_url = 'https://upjoke.com/programmer-jokes'
16-
unparsed_page_text = HTTParty.get(@text_url)
17-
@parsed_page_text = Nokogiri::HTML(unparsed_page_text.body)
18-
end
19-
20-
def images
21-
imgs = @parsed_page.css('figure.entry-image img[src]').select { |image| image['src'].start_with?('https') }
22-
images_urls = imgs.map { |t| t[:src] }
23-
puts images_urls
24-
end
25-
26-
def text_jokes
27-
my_jokes = @parsed_page_text.css('.joke-wrapper').map(&:text)
28-
puts my_jokes.sample
29-
end
30-
end
31-
32-
web = Web.new
33-
web.images
34-

lib/command/slack_command.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ module JokerTheNerd
55
module Commands
66
class Jokes < SlackRubyBot::Commands::Base
77
command 'jokes_image' do |client, data, _match|
8-
client.say(channel: data.channel, text: JokesImage.image_jokes)
8+
client.say(channel: data.channel, text: JokesImage.image_random_joke)
99
end
1010

1111
command 'jokes_text' do |client, data, _match|
12-
client.say(channel: data.channel, text: JokesText.text_jokes)
12+
client.say(channel: data.channel, text: JokesText.text_random_joke)
1313
end
1414

1515
command 'say_hello' do |client, data, _match|
@@ -20,7 +20,7 @@ class Jokes < SlackRubyBot::Commands::Base
2020

2121
class HelloText
2222
def self.say_hello
23-
'Hello, Joker the Nerd Here'
23+
'Heyyy, ready for some humors?'
2424
end
2525
end
2626

@@ -29,8 +29,11 @@ def self.text_jokes
2929
@text_url = 'https://upjoke.com/programmer-jokes'
3030
unparsed_page_text = HTTParty.get(@text_url)
3131
@parsed_page_text = Nokogiri::HTML(unparsed_page_text.body)
32-
my_jokes = @parsed_page_text.css('.joke-wrapper').map(&:text)
33-
my_jokes.sample
32+
@parsed_page_text.css('.joke-wrapper').map(&:text)
33+
end
34+
35+
def self.text_random_joke
36+
text_jokes.sample
3437
end
3538
end
3639

@@ -40,8 +43,11 @@ def self.image_jokes
4043
unparsed_page = HTTParty.get(@base_url)
4144
@parsed_page = Nokogiri::HTML(unparsed_page.body)
4245
imgs = @parsed_page.css('figure.entry-image img[src]').select { |image| image['src'].start_with?('https') }
43-
images_urls = imgs.map { |t| t[:src] }
44-
images_urls.sample
46+
imgs.map { |t| t[:src] }
47+
end
48+
49+
def self.image_random_joke
50+
image_jokes.sample
4551
end
4652
end
4753
end

0 commit comments

Comments
 (0)