class DiscourseCli::CLI

Public Instance Methods

categories() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
users() click to toggle source
# File lib/discourse_cli/cli.rb, line 94
def users
  client = DiscourseCli::Client.client
  u = client.group_members('trust_level_0')
  puts u 
end