Skip to content

Commit 5030a0b

Browse files
AndrewSverdrupoblakeerickson
authored andcommitted
Updates and fixes for topic and category methods. (#188)
* Cleanup and fixes for topic and category methods. - Fix bug with required params that are false. - Allow more optional paramters when creating a category. - Don't require topic slug when updating topic status. * Add spec for when required parameter is false.
1 parent 58f83cb commit 5030a0b

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

lib/discourse_api/api/categories.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ module Categories
88
def create_category(args = {})
99
args = API.params(args)
1010
.required(:name, :color, :text_color)
11-
.optional(:description, :permissions, :custom_fields)
11+
.optional(:slug, :permissions, :auto_close_hours, :auto_close_based_on_last_post, :position, :email_in,
12+
:email_in_allow_strangers, :logo_url, :background_url, :allow_badges, :topic_template, :custom_fields, :description)
1213
.default(parent_category_id: nil)
1314
response = post("/categories", args)
1415
response['category']
@@ -19,7 +20,7 @@ def update_category(args = {})
1920
args = API.params(args)
2021
.required(:id, :name, :color, :text_color)
2122
.optional(:slug, :permissions, :auto_close_hours, :auto_close_based_on_last_post, :position, :email_in,
22-
:email_in_allow_strangers, :logo_url, :background_url, :allow_badges, :topic_template, :custom_fields)
23+
:email_in_allow_strangers, :logo_url, :background_url, :allow_badges, :topic_template, :custom_fields, :description)
2324
.default(parent_category_id: nil)
2425
response = put("/categories/#{category_id}", args)
2526
response['body']['category'] if response['body']

lib/discourse_api/api/params.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ def initialize(args)
1616

1717
def required(*keys)
1818
@required.concat(keys)
19+
@required.each do |k|
20+
raise ArgumentError.new("#{k} is required but not specified") unless @args.key?(k)
21+
end
1922
self
2023
end
2124

@@ -36,7 +39,6 @@ def to_h
3639

3740
@required.each do |k|
3841
h[k] = @args[k]
39-
raise ArgumentError.new("#{k} is required but not specified") unless h[k]
4042
end
4143

4244
@optional.each do |k|

lib/discourse_api/api/topics.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def change_topic_status(topic_slug, topic_id, params = {})
4646
params = API.params(params)
4747
.required(:status, :enabled)
4848
.optional(:api_username)
49-
put("/t/#{topic_slug}/#{topic_id}/status", params.to_h)
49+
put("/t/#{topic_id}/status", params.to_h)
5050
end
5151

5252
def topic(id, params = {})

spec/discourse_api/api/params_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ def params_for(h)
99
expect { params_for({o1: "test"}).to_h }.to raise_error(ArgumentError)
1010
end
1111

12+
it "should not raise when a required param is false" do
13+
expect { params_for({r1: false}).to_h }.not_to raise_error
14+
end
15+
1216
it "should not include optional params when not provided" do
1317
expect(params_for({r1: "test"}).to_h).not_to include(:o1)
1418
end

spec/discourse_api/api/topics_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
describe DiscourseApi::API::Topics do
44
subject { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") }
55

6+
describe "#change_topic_status" do
7+
before do
8+
stub_put("#{host}/t/57/status").to_return(body: fixture("topic.json"), headers: { content_type: "application/json" })
9+
end
10+
11+
it "changes the topic status" do
12+
subject.change_topic_status(nil, 57, { status: 'visible', enabled: false })
13+
expect(a_put("#{host}/t/57/status")).to have_been_made
14+
end
15+
end
16+
617
describe "#invite_user_to_topic" do
718
before do
819
stub_post("#{host}/t/12/invite").to_return(body: fixture("topic_invite_user.json"), headers: { content_type: "application/json" })

0 commit comments

Comments
 (0)