class Ruboty::HaikuWoYome::Actions::Haiku

Public Instance Methods

call(options = {type: :haiku}) click to toggle source
# File lib/ruboty/haiku_wo_yome/actions/haiku.rb, line 5
def call(options = {type: :haiku})
  list = case options[:type]
         when :tanka
           tanka_list
         else
           haiku_list
         end
  haiku = list.sample || '「アイエエエエ!?」'
  message.reply(haiku)
end

Private Instance Methods

client() click to toggle source
# File lib/ruboty/haiku_wo_yome/actions/haiku.rb, line 18
def client
  @client ||= Slack.client(token: ENV['SLACK_TOKEN_FOR_SEARCH'])
end
haiku_list() click to toggle source
# File lib/ruboty/haiku_wo_yome/actions/haiku.rb, line 22
def haiku_list
  user_public_messages_text.inject([]) do |acc, message|
    haiku_reviewer.search(message).each do |node|
      acc << node.phrases.join
    end
    acc
  end
end
haiku_reviewer(options = {}) click to toggle source
# File lib/ruboty/haiku_wo_yome/actions/haiku.rb, line 81
def haiku_reviewer(options = {})
  rule = options[:rule] || [5, 7, 5]
  @reviewer ||= Ikku::Reviewer.new(rule: rule)
end
reset!() click to toggle source
# File lib/ruboty/haiku_wo_yome/actions/haiku.rb, line 86
def reset!
  @reviewer = nil
end
search_word() click to toggle source
# File lib/ruboty/haiku_wo_yome/actions/haiku.rb, line 77
def search_word
  message[:search_word]
end
tanka_list() click to toggle source
# File lib/ruboty/haiku_wo_yome/actions/haiku.rb, line 31
def tanka_list
  kaminoku_nodes = user_public_messages_text.inject([]) do |acc, message|
    haiku_reviewer.search(message).each do |node|
      acc << node.phrases.join
    end
    acc
  end

  reset!
  shimonoku_nodes = user_public_messages_text.inject([]) do |acc, message|
    haiku_reviewer(rule: [7, 7]).search(message).each do |node|
      acc << node.phrases.join
    end
    acc
  end

  return [] if shimonoku_nodes.empty?

  kaminoku_nodes.map do |kaminoku|
    "#{kaminoku}  #{shimonoku_nodes.sample}"
  end
end
user_name() click to toggle source
# File lib/ruboty/haiku_wo_yome/actions/haiku.rb, line 71
def user_name
  name = message[:user_name]
  name = name.gsub(':', '')
  name.start_with?('@') ? name : "@#{name}"
end
user_public_messages() click to toggle source
# File lib/ruboty/haiku_wo_yome/actions/haiku.rb, line 58
def user_public_messages
  query = nil
  begin
    query = "from:#{user_name}"
  rescue
    query = search_word
  end
  search_result = client.search_messages(query: query, count: 1000)
  return [] if search_result.nil?
  return [] if search_result['messages'].nil? 
  search_result['messages']['matches']
end
user_public_messages_text() click to toggle source
# File lib/ruboty/haiku_wo_yome/actions/haiku.rb, line 54
def user_public_messages_text
  @public_messages ||= user_public_messages.select { |m| m['type'] != 'group' }.map { |t| t['text'] }
end