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



31
32
33
# File 'lib/discourse_api/api/categories.rb', line 31

def categories(params = {})
  categories_full(params)["category_list"]["categories"]
end

#categories_full(params = {}) ⇒ Object



35
36
37
38
# File 'lib/discourse_api/api/categories.rb', line 35

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

#category(id) ⇒ Object



81
82
83
84
# File 'lib/discourse_api/api/categories.rb', line 81

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

#category_latest_topics(args = {}) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/discourse_api/api/categories.rb', line 40

def category_latest_topics(args = {})
  response = category_latest_topics_full(args)
  if response["errors"]
    response["errors"]
  else
    response["topic_list"]["topics"]
  end
end

#category_latest_topics_full(args = {}) ⇒ Object



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

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

#category_new_topics(category_slug) ⇒ Object



71
72
73
74
# File 'lib/discourse_api/api/categories.rb', line 71

def category_new_topics(category_slug)
  response = category_new_topics_full(category_slug)
  response["topic_list"]["topics"]
end

#category_new_topics_full(category_slug) ⇒ Object



76
77
78
79
# File 'lib/discourse_api/api/categories.rb', line 76

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

#category_set_user_notification(args = {}) ⇒ Object

TODO: Deprecated. Remove after 20210727



87
88
89
90
91
# File 'lib/discourse_api/api/categories.rb', line 87

def category_set_user_notification(args = {})
  category_id = args[:id]
  args = API.params(args).required(:notification_level)
  post("/category/#{category_id}/notifications", args)
end

#category_set_user_notification_level(category_id, params) ⇒ Object



93
94
95
96
# File 'lib/discourse_api/api/categories.rb', line 93

def category_set_user_notification_level(category_id, params)
  params = API.params(params).required(:notification_level)
  post("/category/#{category_id}/notifications", params)
end

#category_top_topics(category_slug) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/discourse_api/api/categories.rb', line 57

def category_top_topics(category_slug)
  response = category_top_topics_full(category_slug)
  if response["errors"]
    response["errors"]
  else
    response["topic_list"]["topics"]
  end
end

#category_top_topics_full(category_slug) ⇒ Object



66
67
68
69
# File 'lib/discourse_api/api/categories.rb', line 66

def category_top_topics_full(category_slug)
  response = get("/c/#{category_slug}/l/top.json")
  response[:body]
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



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

def create_category(args = {})
  params = common_category_params(args)
  response = post("/categories", params.to_h)
  response["category"]
end

#delete_category(id) ⇒ Object



26
27
28
29
# File 'lib/discourse_api/api/categories.rb', line 26

def delete_category(id)
  response = delete("/categories/#{id}")
  response[:body]["success"]
end

#reorder_categories(args = {}) ⇒ Object



21
22
23
24
# File 'lib/discourse_api/api/categories.rb', line 21

def reorder_categories(args = {})
  params = API.params(args).required(:mapping)
  post("/categories/reorder", params)
end

#update_category(args = {}) ⇒ Object



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

def update_category(args = {})
  category_id = args[:id]
  params = common_category_params(args, include_id: true)
  response = put("/categories/#{category_id}", params.to_h)
  response["body"]["category"] if response["body"]
end