Sha256: 92803b749ce905c5a0cde3e0a2d4cdd8531cbcc94dcabe7ebd56e717b5a8e46b

Contents?: true

Size: 1.47 KB

Versions: 13

Compression:

Stored size: 1.47 KB

Contents

require 'excon'
require 'launchy'
require 'json'
require 'yaml'

module Devcenter::Commands

  class Base

    include Devcenter::Logger
    include Devcenter::Helpers

    def self.run(*args)
      if Devcenter::GemVersionChecker.new_version_available?
        say "devcenter has a new version available, please update with: gem install devcenter"
        return unless agree('Continue executing your command? (yes/no)')
      end

      command = self.new(*args)
    end

    def initialize(*args)
      @validation_errors = []
      validate
      if @validation_errors.any?
        @validation_errors.each{ |e| say e }
      else
        run
      end
    end

    def validate; end # add error messages to @validation_errors
    def run; end

    protected

    def article_not_found!(slug)
      message = ["No #{slug} article found."]
      suggestions = JSON.parse(Devcenter::Client.get(:path => search_api_path, :query => { :q => slug, :source => 'devcenter-cli' }).body)['devcenter']
      suggestions.select!{ |s| article_url?(s['full_url']) }
      suggestions.each{ |s| s['slug'] = slug_from_article_url(s['full_url']) }
      unless suggestions.empty?
        message << "Perhaps you meant one of these:"
        longest = suggestions.map {|suggestion| suggestion['slug'].size }.max
        suggestions.each do |suggestion|
          message << "  %-#{longest}s # %s" % [suggestion['slug'], suggestion['title']]
        end
      end
      say message.join("\n")
      exit
    end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
devcenter-1.0.7 lib/devcenter/commands/base.rb
devcenter-1.0.6 lib/devcenter/commands/base.rb
devcenter-1.0.4 lib/devcenter/commands/base.rb
devcenter-1.0.3 lib/devcenter/commands/base.rb
devcenter-1.0.1 lib/devcenter/commands/base.rb
devcenter-1.0.0 lib/devcenter/commands/base.rb
devcenter-0.0.14 lib/devcenter/commands/base.rb
devcenter-0.0.13 lib/devcenter/commands/base.rb
devcenter-0.0.12 lib/devcenter/commands/base.rb
devcenter-0.0.11 lib/devcenter/commands/base.rb
devcenter-0.0.10 lib/devcenter/commands/base.rb
devcenter-0.0.9 lib/devcenter/commands/base.rb
devcenter-0.0.8 lib/devcenter/commands/base.rb