Module: DiscourseApi::API::Categories

Included in:
Client
Defined in:
lib/discourse_api/api/categories.rb

Instance Method Summary collapse

Instance Method Details

#categories(params = {}) ⇒ Object



16
17
18
19
# File 'lib/discourse_api/api/categories.rb', line 16

def categories(params={})
  response = get('/categories.json', params)
  response[:body]['category_list']['categories']
end

#category(id) ⇒ Object



51
52
53
54
# File 'lib/discourse_api/api/categories.rb', line 51

def category(id)
  response = get("/c/#{id}/show")
  response.body['category']
end

#category_latest_topics(args = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/discourse_api/api/categories.rb', line 21

def category_latest_topics(args={})
  params = API.params(args)
              .required(:category_slug)
              .optional(:page).to_h
  url = "/c/#{params[:category_slug]}/l/latest.json"
  if params.include?(:page)
    url = "#{url}?page=#{params[:page]}"
  end 
  response = get(url)
  if response[:body]['errors']
    response[:body]['errors']
  else
    response[:body]['topic_list']['topics']
  end
end

#category_new_topics(category_slug) ⇒ Object



46
47
48
49
# File 'lib/discourse_api/api/categories.rb', line 46

def category_new_topics(category_slug)
  response = get("/c/#{category_slug}/l/new.json")
  response[:body]['topic_list']['topics']
end

#category_top_topics(category_slug) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/discourse_api/api/categories.rb', line 37

def category_top_topics(category_slug)
  response = get("/c/#{category_slug}/l/top.json")
  if response[:body]['errors']
    response[:body]['errors']
  else
    response[:body]['topic_list']['topics']
  end
end

#create_category(args = {}) ⇒ Object

:color and :text_color are RGB hexadecimal strings :permissions is a hash with the group name and permission_type which is an integer 1 = Full 2 = Create Post 3 = Read Only



7
8
9
10
11
12
13
14
# File 'lib/discourse_api/api/categories.rb', line 7

def create_category(args={})
  args = API.params(args)
            .required(:name, :color, :text_color)
            .optional(:description, :permissions)
            .default(parent_category_id: nil)
  response = post("/categories", args)
  response['category']
end