class Plz::Arguments
Public Class Methods
new(argv)
click to toggle source
@param arguments [Array] Raw ARGV
# File lib/plz/arguments.rb, line 4 def initialize(argv) @argv = argv end
Public Instance Methods
action_name()
click to toggle source
@return [String, nil] Given action name
# File lib/plz/arguments.rb, line 9 def action_name @argv[0] end
has_invalid_json_input?()
click to toggle source
@return [true, false] true if invalid JSON input is given via STDIN
# File lib/plz/arguments.rb, line 37 def has_invalid_json_input? params_from_stdin false rescue JSON::ParserError true end
headers()
click to toggle source
@return [Hash] Headers parsed from given arguments
# File lib/plz/arguments.rb, line 25 def headers @argv[2..-1].inject({}) do |result, section| case when /(?<key>.+):(?<value>[^=]+)/ =~ section result.merge(key => value) else result end end end
params()
click to toggle source
@return [Hash] Params parsed from given arguments & STDIN @raise [Plz::UnparsableJsonParam]
# File lib/plz/arguments.rb, line 20 def params params_from_stdin.merge(params_from_argv) end
target_name()
click to toggle source
@return [String, nil] Given target name
# File lib/plz/arguments.rb, line 14 def target_name @argv[1] end
Private Instance Methods
has_input_from_stdin?()
click to toggle source
@return [true, false] True if any input given via STDIN
# File lib/plz/arguments.rb, line 76 def has_input_from_stdin? has_pipe_input? || has_redirect_input? end
has_pipe_input?()
click to toggle source
@return [true, false] True if any input given from pipe
# File lib/plz/arguments.rb, line 86 def has_pipe_input? File.pipe?(STDIN) end
has_redirect_input?()
click to toggle source
@return [true, false] True if any input given from redirection
# File lib/plz/arguments.rb, line 81 def has_redirect_input? File.select([STDIN], [], [], 0) != nil end
params_from_argv()
click to toggle source
@return [Hash] Params extracted from ARGV
# File lib/plz/arguments.rb, line 58 def params_from_argv @params_from_argv ||= @argv[2..-1].inject({}) do |result, section| case when /(?<key>.+):=(?<value>.+)/ =~ section begin result.merge(key => JSON.parse(%<{"key":#{value}}>)["key"]) rescue JSON::ParserError raise UnparsableJsonParam, value: value end when /(?<key>.+)=(?<value>.+)/ =~ section result.merge(key => value) else result end end end
params_from_stdin()
click to toggle source
@return [Hash] Params extracted from STDIN
# File lib/plz/arguments.rb, line 47 def params_from_stdin @params_from_stdin ||= begin if has_input_from_stdin? JSON.parse(STDIN.read) else {} end end end