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



28
29
30
31
# File 'lib/discourse_api/api/categories.rb', line 28

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

#category(id) ⇒ Object



63
64
65
66
# File 'lib/discourse_api/api/categories.rb', line 63

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

#category_latest_topics(args = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/discourse_api/api/categories.rb', line 33

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



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

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



68
69
70
71
72
73
# File 'lib/discourse_api/api/categories.rb', line 68

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



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

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
# File 'lib/discourse_api/api/categories.rb', line 8

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

#update_category(args = {}) ⇒ Object



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

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)
    .default(parent_category_id: nil)
  response = put("/categories/#{category_id}", args)
  response['body']['category'] if response['body']
end