Class: Devcenter::Commands::Pull

Inherits:
Base
  • Object
show all
Defined in:
lib/devcenter/commands/pull.rb

Instance Method Summary collapse

Methods inherited from Base

run

Methods included from Helpers

#article_api_path, #article_path, #article_url?, #broken_link_checks_path, #devcenter_base_url, #get_oauth_token, #html_file_path, #md_file_path, #netrc_path, #search_api_path, #slug_from_article_url, #update_article_path, #validate_article_path, #write_file

Methods included from Logger

active=, active?, #log

Constructor Details

#initialize(*args) ⇒ Pull

Returns a new instance of Pull.



5
6
7
8
9
# File 'lib/devcenter/commands/pull.rb', line 5

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

Instance Method Details

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/devcenter/commands/pull.rb', line 17

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']
   = {'title' => article['title'], 'id' => article['id']}
  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 = [YAML.dump(), article['content']].join("\n\n")
  write_file(file_path, file_content)
  say "\"#{metadata['title']}\" article saved as #{file_path}"
end

#validateObject



11
12
13
14
15
# File 'lib/devcenter/commands/pull.rb', line 11

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