Module: LitaGithub::General

Included in:
Lita::Handlers::GithubIssues, Lita::Handlers::GithubOrg, Lita::Handlers::GithubRepo
Defined in:
lib/lita-github/general.rb

Overview

Github handler common-use methods

Author:

Instance Method Summary collapse

Instance Method Details

#opts_parse(cmd) ⇒ Hash

Parse the options in the command using the option regex

Parameters:

  • cmd (String)

    the full command line provided to Lita

Returns:

  • (Hash)

    the key:value pairs that were in the command string

Author:


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lita-github/general.rb', line 50

def opts_parse(cmd)
  o = {}
  cmd.scan(LitaGithub::R::OPT_REGEX).flatten.each do |opt|
    k, v = symbolize_opt_key(*opt.strip.split(':'))
    next if o.key?(k)

    # if it looks like we're using the extended option (first character is a " or '):
    #   slice off the first and last character of the string
    # otherwise:
    #   do nothing
    v = v.slice!(1, (v.length - 2)) if %w(' ").include?(v.slice(0))

    o[k] = to_i_if_numeric(v)
  end
  o
end

#symbolize_opt_key(k, v) ⇒ Array

For use when parsing options using opt_parse(), this method converts the key to a downcased symbol for use within the option Hash.

Parameters:

  • k (String)

    the key

  • v (Any)

    the value

Returns:

  • (Array)

    the first element is key as a downcased symbol, the second is v with no changes

Author:


41
42
43
# File 'lib/lita-github/general.rb', line 41

def symbolize_opt_key(k, v)
  [k.downcase.to_sym, v]
end

#to_i_if_numeric(value) ⇒ Integer, String

Convert the value of parameter 1 to an Integer if it looks like it should be one

Parameters:

  • value (String)

    the value to see if it should be an Integer

Returns:

  • (Integer)

    if the String value looks like an Integer (/^\d+$/), return it as one

  • (String)

    if the String value is not an Integer, return as is

Author:


30
31
32
# File 'lib/lita-github/general.rb', line 30

def to_i_if_numeric(value)
  /^\d+$/.match(value) ? value.to_i : value
end