Class: DiscourseCli::Commands::Topics

Inherits:
Base
  • Object
show all
Defined in:
lib/discourse_cli/commands/topics.rb

Instance Method Summary collapse

Methods inherited from Base

exit_on_failure?

Instance Method Details

#createObject



28
29
30
31
32
33
34
35
36
# File 'lib/discourse_cli/commands/topics.rb', line 28

def create
  raw = resolve_raw(options[:raw])
  params = { title: options[:title], raw: raw }
  params[:category] = options[:category]                      if options[:category]
  params[:tags]     = options[:tags].split(",").map(&:strip)  if options[:tags]
  formatter.print_item(client.create_topic(params))
rescue DiscourseApi::DiscourseError => e
  handle_error(e)
end

#delete(id) ⇒ Object



76
77
78
79
80
81
# File 'lib/discourse_cli/commands/topics.rb', line 76

def delete(id)
  client.delete_topic(id.to_i)
  formatter.print_success("Deleted topic #{id}")
rescue DiscourseApi::DiscourseError => e
  handle_error(e)
end

#listObject



8
9
10
11
12
13
14
# File 'lib/discourse_cli/commands/topics.rb', line 8

def list
  params = {}
  params[:category] = options[:category] if options[:category]
  formatter.print_list(client.latest_topics(params))
rescue DiscourseApi::DiscourseError => e
  handle_error(e)
end

#show(id) ⇒ Object



17
18
19
20
21
# File 'lib/discourse_cli/commands/topics.rb', line 17

def show(id)
  formatter.print_item(client.topic(id.to_i))
rescue DiscourseApi::DiscourseError => e
  handle_error(e)
end

#update(id) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/discourse_cli/commands/topics.rb', line 42

def update(id)
  updated_anything = false

  if options[:title]
    client.rename_topic(id.to_i, options[:title])
    updated_anything = true
  end

  if options[:category]
    client.recategorize_topic(id.to_i, options[:category])
    updated_anything = true
  end

  raw =
    if options[:raw]
      options[:raw]
    elsif !updated_anything && !$stdin.tty?
      $stdin.read
    elsif !updated_anything
      Editor.new.open
    end

  if raw
    topic_data = client.topic(id.to_i)
    first_post_id = topic_data["post_stream"]["posts"][0]["id"]
    client.edit_post(first_post_id, raw)
  end

  formatter.print_success("Updated topic #{id}")
rescue DiscourseApi::DiscourseError => e
  handle_error(e)
end