Skip to content

Commit

Permalink
Help and words
Browse files Browse the repository at this point in the history
  • Loading branch information
vanakenm committed Jan 27, 2018
1 parent 9431924 commit d11b9a1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
42 changes: 30 additions & 12 deletions app/controllers/commands_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,42 @@ class CommandsController < ApplicationController
protect_from_forgery except: [:create]

def create
quote,random = EffinQuote.find_or_random(command_params[:text])
if command_params[:text] == 'help'
render json: help_contents.to_json, status: :created
elsif command_params[:text] == 'words'
render json: words_contents.to_json, status: :created
else
quote,random = EffinQuote.find_or_random(command_params[:text])

EffinLog.create(
effin_quote: quote,
random: random,
team_doman: Digest::SHA256.hexdigest(command_params[:team_domain]),
text: command_params[:text]
)
EffinLog.create(
effin_quote: quote,
random: random,
team_doman: Digest::SHA256.hexdigest(command_params[:team_domain]),
text: command_params[:text]
)

HTTParty.post(command_params[:response_url], { body: contents(quote).to_json, headers: {
"Content-Type" => "application/json"
}
})
HTTParty.post(command_params[:response_url], { body: contents(quote).to_json, headers: {
"Content-Type" => "application/json"
}
})

render json: { response_type: "in_channel" }, status: :created
render json: { response_type: "in_channel" }, status: :created
end
end

private
def words_contents
words = EffinQuote.words.keys.sample(20).join(", ")
{
"text": "Here are some possible words to use: #{words}"
}
end

def help_contents
{
"text": "Call the command with any words to get a quote using this text. If no proper quote can be found, a random one will be send. Use /effin help for this message, /effin words to get some possible words to use. Thanks for using effinbot."
}
end

def contents(quote)
{
Expand Down
15 changes: 15 additions & 0 deletions app/models/effin_quote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ def self.find_by_word(word)
complete.search_by_word(word).first
end

def self.words
words = Hash.new(0)
complete.each do |quote|
quote.words.each do |word|
words[word.downcase] += 1
end
end

words
end

def words
contents.split(" ")
end

def self.find_or_random(text)
quote = EffinQuote.find_by_word(text)
quote ? [quote, false] : [EffinQuote.complete.sample, true]
Expand Down
2 changes: 2 additions & 0 deletions app/views/effin_quotes/home.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

<%= image_tag("effinbot-1.gif") %>

<p>Typing <pre>/effin help</pre> will get you a short explanation. If you lack inspiration, just type <pre>/effin words</pre> and some random words will be proposed.</p>

<p>Thanks to <a href="https://twitter.com/aaronreynolds" target="_blank">Aaron Reynolds</a> for creating Effin' Birds and allowing me to create this and to <a href="https://twitter.com/Boejurt">Joe</a> for outstanding support.</p>

<p>Created by <a href="https://twitter.com/martinvanaken">Martin</a> - let me know if you find any problem</p>
Expand Down

0 comments on commit d11b9a1

Please sign in to comment.