class Plz::CommandBuilder

Constants

SCHEMA_FILE_PATH_PATTERN

Public Class Methods

call(arguments) click to toggle source

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
new(argv) click to toggle source

@param argv [Array<String>] Raw ARGV

# File lib/plz/command_builder.rb, line 24
def initialize(argv)
  @argv = argv
end

Public Instance Methods

call() click to toggle source

@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

arguments() click to toggle source

@return [Plz::Arguments] Wrapper of Raw ARGV

# File lib/plz/command_builder.rb, line 236
def arguments
  @arguments ||= Arguments.new(@argv)
end
base_url() click to toggle source

@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
base_url_from_schema() click to toggle source

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
has_action_name?() click to toggle source

@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
has_base_url?() click to toggle source

@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
has_decodable_schema_file?() click to toggle source

@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
has_help?() click to toggle source

@return [true, false] True if –help or -h given

# File lib/plz/command_builder.rb, line 78
def has_help?
  options[:help]
end
has_schema_file?() click to toggle source

@return [true, false] True if schema file exists

# File lib/plz/command_builder.rb, line 93
def has_schema_file?
  !!schema_file_pathname
end
has_target_name?() click to toggle source

@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
has_unparsable_json_param?() click to toggle source
# File lib/plz/command_builder.rb, line 69
def has_unparsable_json_param?
  params
  false
rescue UnparsableJsonParam => exception
  @json_parse_error = exception
  true
end
has_valid_schema_file?() click to toggle source

@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
json_schema() click to toggle source

@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
method() click to toggle source

@return [String] @example

method #=> "GET"
# File lib/plz/command_builder.rb, line 183
def method
  link.method.to_s.upcase
end
options() click to toggle source

@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
path() click to toggle source

@return [String] @example

path #=> "/users"
# File lib/plz/command_builder.rb, line 143
def path
  path_with_template % path_params.symbolize_keys
end
path_keys() click to toggle source

@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
path_params() click to toggle source

@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
path_with_template() click to toggle source

@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
request_params() click to toggle source

@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
schema() click to toggle source

@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
schema_file_body() click to toggle source

@return [String]

# File lib/plz/command_builder.rb, line 131
def schema_file_body
  @schema_file_body ||= schema_file_pathname.read
end
schema_file_pathname() click to toggle source

@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