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



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

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

#category(id) ⇒ Object



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

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

#category_latest_topics(args = {}) ⇒ Object



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

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



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

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



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

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

#update_category(args = {}) ⇒ Object



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

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