Sha256: 3e948d38799816d1fb9de2b605003d7bc642b951cc370f43543644b9c5368048

Contents?: true

Size: 1.88 KB

Versions: 4

Compression:

Stored size: 1.88 KB

Contents

require "json_schema-cromulent_links"
require "json_schema-example_parsing"
require "multi_json"
require "open-uri"
require "pathname"
require "yaml"

module Acter
  class << self
    def load_schema_data(path = nil)
      path ||= Pathname.glob("schema.{json,yml}").first or raise NoSchema
      if path.is_a?(String)
        uri = URI(path)
        source = uri.scheme ? uri : Pathname.new(path)
      elsif path.respond_to?(:read) && path.respond_to?(:to_s)
        source = path
      else
        raise ArgumentError, "Argument to load_schema must be a String or a Pathname-like object"
      end
      if source.to_s =~ /\.ya?ml$/
        YAML.load(source.read)
      else
        MultiJson.load(source.read)
      end
    end

    def handle_invalid_command(exn)
      puts exn
      puts
      help = Help.new(exn.schema)
      case exn
      when HelpWanted, MissingParameters
        puts help.help_for_action(exn.action, exn.subject)
      when InvalidAction
        puts help.help_for_subject(exn.subject)
      else
        puts help.general_help
      end
    end

    def run(args, schema_path = nil, render_options = nil)
      schema_data = load_schema_data(schema_path)
      action = Action.new(args, schema_data)
      result = action.send_request
      puts result.render(render_options)
      result.success?
    rescue InvalidCommand => e
      handle_invalid_command(e)
    rescue NoSchema
      raise unless args.empty? || args == %w"help"
      handle_invalid_command(InvalidCommand.new(nil))
    end

    def program_name
      @program_name ||= File.basename($0, ".rb")
    end

    attr_accessor :help_wanted, :options_text
    alias help_wanted? help_wanted
  end
end

require_relative "acter/action"
require_relative "acter/error"
require_relative "acter/help"
require_relative "acter/request"
require_relative "acter/response"
require_relative "acter/result"
require_relative "acter/version"

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
acter-0.2.0 lib/acter.rb
acter-0.1.3 lib/acter.rb
acter-0.1.2 lib/acter.rb
acter-0.1.1 lib/acter.rb