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



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
74
# File 'lib/discourse_cli/cli.rb', line 45

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

#topics(category_slug) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/discourse_cli/cli.rb', line 21

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)   
    count = topics.count
    topics.each do |t|
      results[t['id']] = t
    end
    page += 1
  end
   
  puts "The following #{results.count} topics were found in the #{category_slug} category:"
  puts

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