class PivotalButler::Cli

Public Class Methods

start() click to toggle source
# File lib/pivotal_butler/cli.rb, line 3
def self.start
  self.new.start
end

Public Instance Methods

add_story(type, name) click to toggle source
# File lib/pivotal_butler/cli.rb, line 66
def add_story(type, name)
  [] unless name
  project = PivotalTracker::Project.find(PivotalButler::Setting.pivotal_tracker.project_id)
  project.stories.create(name: name, story_type: type)
  ["「#{name}」を #{type} として登録しました"]
end
get_stories(type, state) click to toggle source
# File lib/pivotal_butler/cli.rb, line 73
def get_stories(type, state)
  project = PivotalTracker::Project.find(PivotalButler::Setting.pivotal_tracker.project_id)
  stories = project.stories.all(current_state: state.downcase, story_type: [type]).take(10).map do |story|
    humanize story
  end
  stories = ["#{type} の #{state} は見付かりませんでした"] if stories == []
  stories
end
get_type(type) click to toggle source
# File lib/pivotal_butler/cli.rb, line 49
def get_type(type)
  case type.upcase
  when "FEATURE", "TODO", "TASK", "MEMO", "メモ", "タスク" then "feature"
  when "CHORE" then "chore"
  when "BUG" then "bug"
  end
end
humanize(story) click to toggle source
# File lib/pivotal_butler/cli.rb, line 82
def humanize(story)
  message = PivotalButler::Setting.pivotal_tracker.format
  %w(nick id type state story url).each do |convert_str|
    message = message.gsub(convert_str, PivotalButler::Story.send(convert_str, story))
  end
  message
end
messages(type, action) click to toggle source
# File lib/pivotal_butler/cli.rb, line 57
def messages(type, action)
  case action.downcase
  when 'started', 'unscheduled', 'finished', 'deliverd', 'rejected', 'accepted'
    get_stories(type, action)
  else
    add_story(type, action)
  end
end
parse_message(message) click to toggle source
# File lib/pivotal_butler/cli.rb, line 40
def parse_message(message)
  match = message.match(/#{self.bot.nick}: (.*) (.*)/)
  if match
    [get_type(match[1]), match[2]]
  else
    [nil, nil]
  end
end
send_message(m, message) click to toggle source
# File lib/pivotal_butler/cli.rb, line 29
def send_message(m, message)
  case PivotalButler::Setting.irc.message_mode.downcase
  when 'notice'
    m.channel.notice message
  when 'private'
    m.user.send message
  else
    m.reply message
  end
end
start() click to toggle source
# File lib/pivotal_butler/cli.rb, line 7
def start
  PivotalTracker::Client.token = PivotalButler::Setting.pivotal_tracker.api_token
  PivotalTracker::Client.use_ssl = true

  bot = Cinch::Bot.new do
    configure do |c|
      c.nick     = PivotalButler::Setting.irc.nick
      c.realname = PivotalButler::Setting.irc.realname

      c.server   = PivotalButler::Setting.irc.server
      c.channels = PivotalButler::Setting.irc.channels
      c.password = PivotalButler::Setting.irc.password
      c.port     = PivotalButler::Setting.irc.port
      c.ssl.use  = PivotalButler::Setting.irc.use_ssl
      c.encoding = PivotalButler::Setting.irc.encoding
    end

    on :channel do |m|
      messages(*parse_message(m.params[1])).each { |message| send_message(m, message) }
    end

    helpers do
      def send_message(m, message)
        case PivotalButler::Setting.irc.message_mode.downcase
        when 'notice'
          m.channel.notice message
        when 'private'
          m.user.send message
        else
          m.reply message
        end
      end

      def parse_message(message)
        match = message.match(/#{self.bot.nick}: (.*) (.*)/)
        if match
          [get_type(match[1]), match[2]]
        else
          [nil, nil]
        end
      end

      def get_type(type)
        case type.upcase
        when "FEATURE", "TODO", "TASK", "MEMO", "メモ", "タスク" then "feature"
        when "CHORE" then "chore"
        when "BUG" then "bug"
        end
      end

      def messages(type, action)
        case action.downcase
        when 'started', 'unscheduled', 'finished', 'deliverd', 'rejected', 'accepted'
          get_stories(type, action)
        else
          add_story(type, action)
        end
      end

      def add_story(type, name)
        [] unless name
        project = PivotalTracker::Project.find(PivotalButler::Setting.pivotal_tracker.project_id)
        project.stories.create(name: name, story_type: type)
        ["「#{name}」を #{type} として登録しました"]
      end

      def get_stories(type, state)
        project = PivotalTracker::Project.find(PivotalButler::Setting.pivotal_tracker.project_id)
        stories = project.stories.all(current_state: state.downcase, story_type: [type]).take(10).map do |story|
          humanize story
        end
        stories = ["#{type} の #{state} は見付かりませんでした"] if stories == []
        stories
      end

      def humanize(story)
        message = PivotalButler::Setting.pivotal_tracker.format
        %w(nick id type state story url).each do |convert_str|
          message = message.gsub(convert_str, PivotalButler::Story.send(convert_str, story))
        end
        message
      end
    end
  end

  bot.start
end