class Plz::CommandBuilder
Constants
- SCHEMA_FILE_PATH_PATTERN
Public Class Methods
Builds callable command object from given ARGV @return [Plz::Command, Plz::ErrorCommand] @note This is a shortcut for call
method @example
Plz::CommandBuilder.call(ARGV).call
# File lib/plz/command_builder.rb, line 10 def self.call(arguments) new(arguments).call end
@param argv [Array<String>] Raw ARGV
# File lib/plz/command_builder.rb, line 24 def initialize(argv) @argv = argv end
Public Instance Methods
@return [Plz::Command] Callable command object
# File lib/plz/command_builder.rb, line 29 def call case when !has_schema_file? Commands::SchemaFileNotFound.new when !has_decodable_schema_file? Commands::UndecodableSchemaFile.new(pathname: schema_file_pathname) when !has_valid_schema_file? Commands::InvalidSchema.new(pathname: schema_file_pathname, error: @json_schema_error) when !has_base_url? Commands::BaseUrlNotFound.new(pathname: schema_file_pathname) when has_help? Commands::Help.new(options: options, schema: json_schema) when !has_action_name? Commands::NoActionName.new when !has_target_name? Commands::NoTargetName.new when !has_link? Commands::LinkNotFound.new( pathname: schema_file_pathname, action_name: action_name, target_name: target_name, ) when has_invalid_json_input? Commands::InvalidJsonFromStdin.new when has_unparsable_json_param? Commands::UnparsableJsonParam.new(error: @json_parse_error) else Commands::Request.new( method: method, base_url: base_url, path: path, headers: headers, params: request_params, options: options, ) end end
Private Instance Methods
@return [Plz::Arguments] Wrapper of Raw ARGV
# File lib/plz/command_builder.rb, line 236 def arguments @arguments ||= Arguments.new(@argv) end
@return [String, nil] Base URL of the API @example
base_url #=> "http://localhost:8080"
# File lib/plz/command_builder.rb, line 190 def base_url @base_url ||= begin if url = (options[:host] || base_url_from_schema) if url.start_with?("http") url else "http://#{url}" end end end end
Extracts the base url of the API from JSON Schema @return [String, nil] @example
base_url_from_schema #=> "https://api.example.com/"
# File lib/plz/command_builder.rb, line 206 def base_url_from_schema json_schema.links.find do |link| if link.href && link.rel == "self" return link.href end end end
@return [true, false] True if given arguments include action name
# File lib/plz/command_builder.rb, line 83 def has_action_name? !!action_name end
@return [true, false] True if given JSON Schema has a link of base URL of API
# File lib/plz/command_builder.rb, line 108 def has_base_url? !!base_url end
@return [true, false] True if no error occured in decoding schema file
# File lib/plz/command_builder.rb, line 103 def has_decodable_schema_file? !!schema end
@return [true, false] True if –help or -h given
# File lib/plz/command_builder.rb, line 78 def has_help? options[:help] end
@return [true, false] True if any link for given action and target found
# File lib/plz/command_builder.rb, line 113 def has_link? !!link end
@return [true, false] True if schema file exists
# File lib/plz/command_builder.rb, line 93 def has_schema_file? !!schema_file_pathname end
@return [true, false] True if given arguments include target name
# File lib/plz/command_builder.rb, line 88 def has_target_name? !!target_name end
# File lib/plz/command_builder.rb, line 69 def has_unparsable_json_param? params false rescue UnparsableJsonParam => exception @json_parse_error = exception true end
@return [true, false] True if no error occured in parsing schema file as JSON Schema
# File lib/plz/command_builder.rb, line 98 def has_valid_schema_file? !json_schema.nil? end
@return [JsonSchema::Schema, nil]
# File lib/plz/command_builder.rb, line 228 def json_schema @json_schema ||= JsonSchema.parse!(@schema).tap(&:expand_references!) rescue => exception @json_schema_error = exception nil end
@return [JsonSchema::Schema::Link, nil]
# File lib/plz/command_builder.rb, line 215 def link @link ||= json_schema.properties.find do |key, schema| if key == target_name schema.links.find do |link| if link.href && link.method && link.title.underscore == action_name return link end end end end end
@return [String] @example
method #=> "GET"
# File lib/plz/command_builder.rb, line 183 def method link.method.to_s.upcase end
@return [Hash] Command line options
# File lib/plz/command_builder.rb, line 241 def options @options ||= Slop.parse!(@argv) do banner Error::USAGE on "h", "help", "Display help message" on "H", "host=", "API host" on "no-color", "Disable coloring output" on "no-response-body", "Hide response body" on "no-response-header", "Hide response header" end end
@return [String] @example
path #=> "/users"
# File lib/plz/command_builder.rb, line 143 def path path_with_template % path_params.symbolize_keys end
@return [Array<String>] Parameter names required for path @exmaple
path_keys #=> ["id"]
# File lib/plz/command_builder.rb, line 160 def path_keys link.href.scan(/{(.+?)}/).map do |gr| CGI.unescape(gr.first).gsub(/[()]/, "").split("/").last end end
@return [Hash] Params to be embedded into path @example
path_params #=> { "id" => 1 }
# File lib/plz/command_builder.rb, line 169 def path_params params.slice(*path_keys) end
@return [String] @example
path_with_template #=> "/apps/%{id}"
# File lib/plz/command_builder.rb, line 150 def path_with_template link.href.gsub(/{(.+?)}/) do |matched| key = CGI.unescape($1).gsub(/[()]/, "").split("/").last "%{#{key}}" end end
@return [Hash] Params to be used for request body or query string @example
request_params #=> { "name" => "example" }
# File lib/plz/command_builder.rb, line 176 def request_params params.except(*path_keys) end
@return [Hash]
# File lib/plz/command_builder.rb, line 118 def schema @schema ||= begin case schema_file_pathname.extname when ".yml" YAML.load(schema_file_body) else JSON.parse(schema_file_body) end end rescue end
@return [String]
# File lib/plz/command_builder.rb, line 131 def schema_file_body @schema_file_body ||= schema_file_pathname.read end
@return [Pathname, nil] Found schema file path
# File lib/plz/command_builder.rb, line 136 def schema_file_pathname @schema_file_pathname ||= Pathname.glob(SCHEMA_FILE_PATH_PATTERN).first end