Class: Devcenter::Commands::Push

Inherits:
Base
  • Object
show all
Defined in:
lib/devcenter/commands/push.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) ⇒ Push

Returns a new instance of Push.



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

def initialize(*args)
  @slug = args[0].to_s.gsub(/.md\z/, '') # maybe the user provides the filename by mistake
  @md_path = md_file_path(@slug)
  super
end

Instance Method Details



49
50
51
52
53
54
55
56
57
# File 'lib/devcenter/commands/push.rb', line 49

def display_broken_links(token)
  response = Devcenter::Client.check_broken_links(token, @article.content)
  broken_links = response.body
  if broken_links.any?
    say "The article \"#{@slug}\" contains broken link/s:"
    broken_links.each{ |link| say("- [#{link['text']}](#{link['url']})") }
    say "\n"
  end
end

#push_article(token) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/devcenter/commands/push.rb', line 59

def push_article(token)
  article_id = @article..id
  article_params = {
    'article[content]' => @article.content,
    'article[title]' => @article..title,
  }
  response = Devcenter::Client.update_article(token, article_id, article_params)
  if response.ok?
    body = response.body
    verb = case body['status']
    when 'published'
      'published'
    when 'published_quietly'
      'published quietly'
    when 'draft'
      'pushed in draft mode'
    when 'archived'
      'archived'
    when 'staging'
      'pushed as staging mode'
    end
    say "Article \"#{body['title']}\" #{verb} to #{body['url']}"
  else
    error = response.body['error']
    abort "Error pushing \"#{@slug}\": #{error}"
  end
end

#runObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/devcenter/commands/push.rb', line 21

def run
  @article = ::Devcenter::ArticleFile.read(@md_path)
  if @article.parsing_error
    abort "The content of #{@md_path} can't be parsed properly, fix it and try again."
  end
  if token = get_oauth_token
    display_broken_links(token)
    if validate_article(token)
      push_article(token)
    end
  end
end

#validateObject



11
12
13
14
15
16
17
18
19
# File 'lib/devcenter/commands/push.rb', line 11

def validate
  empty_slug = @slug.nil? || @slug.to_s.strip.empty?
  file_exists = !empty_slug && File.exists?(@md_path)
  if empty_slug
    @validation_errors << 'Please provide an article slug'
  elsif !file_exists
    @validation_errors << "Can't find #{@md_path} file - you may want to `devcenter pull #{@slug}`"
  end
end

#validate_article(token) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/devcenter/commands/push.rb', line 34

def validate_article(token)
  article_id = @article..id
  article_params = {
    'article[content]' => @article.content,
    'article[title]' => @article..title,
  }
  response = Devcenter::Client.validate_article(token, article_id, article_params)
  errors = response.body
  if errors.any?
    say "The article \"#{@slug}\" can't be saved:"
    abort YAML.dump(errors).gsub(/\A(\-+)\n-/, '')
  end
  errors.empty?
end