Class: DiscourseCli::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/discourse_cli/cli.rb

Instance Method Summary collapse

Instance Method Details

#categoriesObject



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/discourse_cli/cli.rb', line 8

def categories
  client = DiscourseCli::Client.client
  categories = client.categories

  puts "The following #{categories.count.to_s} categories were found:" 
  puts

  categories.each do |c|
    puts "#{c['id']} #{c['name']} #{c['slug']}"
  end
end

#posts(topic_id) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/discourse_cli/cli.rb', line 62

def posts(topic_id)
  client = DiscourseCli::Client.client
  post_ids = []
  
  # fetch topic
  topic = client.topic(topic_id)

  # array of all post id's in topic
  stream = topic['post_stream']['stream']
  
  # get the first ~20 posts in the topic 
  posts = topic['post_stream']['posts']
  posts.each do |p|
    post_ids.push(p['id'])
    puts "#{p['id']} #{p['cooked'][3..13]}..."
  end 

  # get the rest of the posts in chunks of 20
  diff = stream - post_ids
  while diff.count > 0 do
    response = client.topic_posts(topic_id, diff.slice(0, 19))
    response_posts = response['post_stream']['posts']
    response_posts.each do |p|
      post_ids.push(p['id'])
      puts "#{p['id']} #{p['cooked'][3..13]}..."
    end
    diff = stream - post_ids
  end
  
end

#sub_categories(parent_category_id) ⇒ Object



21
22
23
24
25
# File 'lib/discourse_cli/cli.rb', line 21

def sub_categories(parent_category_id)
  client = DiscourseCli::Client.client
  results = client.categories(parent_category_id: parent_category_id)
  puts results 
end

#topics(category_slug) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/discourse_cli/cli.rb', line 28

def topics(category_slug)
  client = DiscourseCli::Client.client
  count = 30
  page = 0
  total = 0
  results = {}
  while count == 30 do
    topics = client.category_latest_topics(category_slug: category_slug, page: page)   
    if !topics.include?("The requested URL or resource could not be found.")
      count = topics.count
      if count > 0
        topics.each do |t|
          results[t['id']] = t
        end
      end
    else
      count = 0 
    end
    page += 1
  end
  
  if count == 0
    puts topics
  else
    puts "The following #{results.count} topics were found in the #{category_slug} category:"
  end 
  puts

  results.each do |k, v|
    puts "#{v['id']} #{v['title']}"
  end
end