Sha256: 1eae13257a1795022c9b876b307987c660761eacf826a328eb71f79bca17667e

Contents?: true

Size: 1.18 KB

Versions: 7

Compression:

Stored size: 1.18 KB

Contents

module Devcenter::Commands

  class Pull < Base

    def initialize(*args)
      @slug = slug_from_article_url(args[0])
      @force_overwrite = args[1]
      super
    end

    def validate
      unless @slug && !@slug.strip.empty?
        @validation_errors << 'Please provide an article slug or full (e.g: ps or https://devcenter.heroku.com/articles/ps)'
      end
    end

    def run
      response = Devcenter::Client.get(path: article_api_path(@slug))
      article_received = response.ok? && response.body['article'] && response.body['article']['id']
      article_not_found!(@slug) unless article_received

      article = response.body['article']
      metadata = {'title' => article['title'], 'id' => article['id'], 'markdown_flavour' => article['markdown_flavour']}
      file_path = md_file_path(@slug)

      unless @force_overwrite
        cancel_save = File.exists?(file_path) && !agree("The file #{file_path} already exists - overwrite? (yes/no)")
        return if cancel_save
      end

      file_content = [metadata.to_yaml, article['content']].join("\n\n")
      write_file(file_path, file_content)
      say "\"#{metadata['title']}\" article saved as #{file_path}"
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
devcenter-1.1.4 lib/devcenter/commands/pull.rb
devcenter-1.1.3 lib/devcenter/commands/pull.rb
devcenter-1.1.2 lib/devcenter/commands/pull.rb
devcenter-1.1.1 lib/devcenter/commands/pull.rb
devcenter-1.1.0 lib/devcenter/commands/pull.rb
devcenter-1.1.0rc2 lib/devcenter/commands/pull.rb
devcenter-1.1.0rc1 lib/devcenter/commands/pull.rb