class Docomoru::Arguments

Public Class Methods

new(argv) click to toggle source
# File lib/docomoru/arguments.rb, line 6
def initialize(argv)
  @argv = argv
end

Public Instance Methods

api_key() click to toggle source
# File lib/docomoru/arguments.rb, line 10
def api_key
  slop_options["api-key"] || ENV["DOCOMO_API_KEY"]
end
arguments() click to toggle source
# File lib/docomoru/arguments.rb, line 14
def arguments
  parsed_argv_data[:arguments]
end
error_message() click to toggle source
# File lib/docomoru/arguments.rb, line 18
def error_message
  slop_options.to_s
end
headers() click to toggle source
# File lib/docomoru/arguments.rb, line 22
def headers
  parsed_argv_data[:headers]
end
method_name() click to toggle source
# File lib/docomoru/arguments.rb, line 26
def method_name
  slop_options.arguments[0]
end
params() click to toggle source
# File lib/docomoru/arguments.rb, line 30
def params
  params_from_stdin.merge(parsed_argv_data[:params])
end
show_body() click to toggle source
# File lib/docomoru/arguments.rb, line 34
def show_body
  !slop_options["no-body"]
end
show_header() click to toggle source
# File lib/docomoru/arguments.rb, line 38
def show_header
  slop_options["header"]
end
valid?() click to toggle source
# File lib/docomoru/arguments.rb, line 42
def valid?
  has_valid_slop_options? && has_valid_method_name? && has_valid_arguments? &&
    !has_invalid_json_input? && has_api_key?
end

Private Instance Methods

has_api_key?() click to toggle source
# File lib/docomoru/arguments.rb, line 49
def has_api_key?
  !!api_key
end
has_input_from_stdin?() click to toggle source
# File lib/docomoru/arguments.rb, line 53
def has_input_from_stdin?
  has_pipe_input? || has_redirect_input?
end
has_invalid_json_input?() click to toggle source
# File lib/docomoru/arguments.rb, line 57
def has_invalid_json_input?
  params_from_stdin
  false
rescue JSON::ParserError
  true
end
has_pipe_input?() click to toggle source
# File lib/docomoru/arguments.rb, line 64
def has_pipe_input?
  File.pipe?(STDIN)
end
has_redirect_input?() click to toggle source
# File lib/docomoru/arguments.rb, line 68
def has_redirect_input?
  File.select([STDIN], [], [], 0) != nil
end
has_valid_arguments?() click to toggle source
# File lib/docomoru/arguments.rb, line 72
def has_valid_arguments?
  -(Client.instance_method(method_name).arity) - 1 == arguments.length
end
has_valid_method_name?() click to toggle source
# File lib/docomoru/arguments.rb, line 76
def has_valid_method_name?
  !method_name.nil? && Client.instance_methods.include?(method_name.to_sym)
end
has_valid_slop_options?() click to toggle source
# File lib/docomoru/arguments.rb, line 80
def has_valid_slop_options?
  !slop_options["help"]
rescue
  false
end
params_from_stdin() click to toggle source
# File lib/docomoru/arguments.rb, line 86
def params_from_stdin
  @params_from_stdin ||= begin
    if has_input_from_stdin?
      JSON.parse(STDIN.read)
    else
      {}
    end
  end
end
parsed_argv_data() click to toggle source
# File lib/docomoru/arguments.rb, line 96
def parsed_argv_data
  @parsed_argv_data ||= begin
    params = {}
    headers = {}
    arguments = []
    slop_options.arguments[1..-1].each do |section|
      case
      when /(?<key>.+):(?<value>[^=]+)/ =~ section
        headers[key] = value
      when /(?<key>.+):=(?<value>.+)/ =~ section
        params[key] = JSON.parse(%<{"key":#{value}}>)["key"]
      when /(?<key>.+)=(?<value>.+)/ =~ section
        params[key] = value
      else
        arguments << section
      end
    end
    {
      arguments: arguments,
      headers: headers,
      params: params,
    }
  end
end
slop_options() click to toggle source
# File lib/docomoru/arguments.rb, line 121
def slop_options
  @slop_options ||= Slop.parse(@argv, suppress_errors: true) do |options|
    options.banner = "Usage: docomoru <method> [arguments] [headers|params] [options]"
    if Slop::VERSION >= "4.0.0"
      options.string("-a", "--api-key", "Pass API Key or use DOCOMO_API_KEY instead")
    else
      options.on("-a", "--api-key=", "Pass API Key or use DOCOMO_API_KEY instead")
    end
    options.on("-h", "--help", "Display help message")
    options.on("--header", "Show response header")
    options.on("--no-body", "Hide response body")
  end
end