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



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

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

#category(id) ⇒ Object



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

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

#category_latest_topics(args = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/discourse_api/api/categories.rb', line 41

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



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

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

#category_set_user_notification(args = {}) ⇒ Object



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

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_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 = 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



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

def create_category(args = {})
  args = API.params(args)
    .required(:name, :color, :text_color)
    .optional(:slug, :permissions, :auto_close_hours, :auto_close_based_on_last_post, :position, :email_in,
                       :email_in_allow_strangers, :logo_url, :background_url, :allow_badges, :topic_template, :custom_fields, :description,
                       :reviewable_by_group_name, :show_subcategory_list, :subcategory_list_style)
    .default(parent_category_id: nil)
  response = post("/categories", args)
  response['category']
end

#delete_category(id) ⇒ Object



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

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

#update_category(args = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/discourse_api/api/categories.rb', line 19

def update_category(args = {})
  category_id = args[:id]
  args = API.params(args)
    .required(:id, :name, :color, :text_color)
    .optional(:slug, :permissions, :auto_close_hours, :auto_close_based_on_last_post, :position, :email_in,
                       :email_in_allow_strangers, :logo_url, :background_url, :allow_badges, :topic_template, :custom_fields, :description,
                       :reviewable_by_group_name, :show_subcategory_list, :subcategory_list_style)
    .default(parent_category_id: nil)
  response = put("/categories/#{category_id}", args)
  response['body']['category'] if response['body']
end