Skip to content

Commit cd6df08

Browse files
authored
Add _full methods to return full category response (#221)
1 parent 74c423c commit cd6df08

File tree

3 files changed

+100
-11
lines changed

3 files changed

+100
-11
lines changed

examples/category.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
# get sub categories for parent category with id 2
1515
puts client.categories(parent_category_id: 2)
1616

17+
# get the full categories response
18+
puts client.categories_full()
19+
1720
# List topics in a category
1821
category_topics = client.category_latest_topics(category_slug: "test-category")
1922
puts category_topics

lib/discourse_api/api/categories.rb

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,24 @@ def delete_category(id)
3636
end
3737

3838
def categories(params = {})
39+
categories_full(params)['category_list']['categories']
40+
end
41+
42+
def categories_full(params = {})
3943
response = get('/categories.json', params)
40-
response[:body]['category_list']['categories']
44+
response[:body]
4145
end
4246

4347
def category_latest_topics(args = {})
48+
response = category_latest_topics_full(args)
49+
if response['errors']
50+
response['errors']
51+
else
52+
response['topic_list']['topics']
53+
end
54+
end
55+
56+
def category_latest_topics_full(args = {})
4457
params = API.params(args)
4558
.required(:category_slug)
4659
.optional(:page).to_h
@@ -49,25 +62,31 @@ def category_latest_topics(args = {})
4962
url = "#{url}?page=#{params[:page]}"
5063
end
5164
response = get(url)
52-
if response[:body]['errors']
53-
response[:body]['errors']
54-
else
55-
response[:body]['topic_list']['topics']
56-
end
65+
response[:body]
5766
end
5867

5968
def category_top_topics(category_slug)
60-
response = get("/c/#{category_slug}/l/top.json")
61-
if response[:body]['errors']
62-
response[:body]['errors']
69+
response = category_top_topics_full(category_slug)
70+
if response['errors']
71+
response['errors']
6372
else
64-
response[:body]['topic_list']['topics']
73+
response['topic_list']['topics']
6574
end
6675
end
6776

77+
def category_top_topics_full(category_slug)
78+
response = get("/c/#{category_slug}/l/top.json")
79+
response[:body]
80+
end
81+
6882
def category_new_topics(category_slug)
83+
response = category_new_topics_full(category_slug)
84+
response['topic_list']['topics']
85+
end
86+
87+
def category_new_topics_full(category_slug)
6988
response = get("/c/#{category_slug}/l/new.json")
70-
response[:body]['topic_list']['topics']
89+
response[:body]
7190
end
7291

7392
def category(id)

spec/discourse_api/api/categories_spec.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@
2828
end
2929
end
3030

31+
describe "#categories_full" do
32+
before do
33+
stub_get("#{host}/categories.json")
34+
.to_return(body: fixture("categories.json"), headers: { content_type: "application/json" })
35+
end
36+
37+
it "requests the correct resource" do
38+
subject.categories
39+
expect(a_get("#{host}/categories.json")).to have_been_made
40+
end
41+
42+
it "returns the entire categories response" do
43+
categories = subject.categories_full
44+
expect(categories).to be_a Hash
45+
expect(categories).to have_key 'category_list'
46+
expect(categories).to have_key 'featured_users'
47+
end
48+
end
49+
3150
describe '#category_latest_topics' do
3251
before do
3352
stub_get("#{host}/c/category-slug/l/latest.json")
@@ -40,6 +59,20 @@
4059
end
4160
end
4261

62+
describe '#category_latest_topics_full' do
63+
before do
64+
stub_get("#{host}/c/category-slug/l/latest.json")
65+
.to_return(body: fixture("category_latest_topics.json"), headers: { content_type: "application/json" })
66+
end
67+
68+
it "returns the entire latest topics in a category response" do
69+
latest_topics = subject.category_latest_topics_full(category_slug: 'category-slug')
70+
expect(latest_topics).to be_a Hash
71+
expect(latest_topics).to have_key 'topic_list'
72+
expect(latest_topics).to have_key 'users'
73+
end
74+
end
75+
4376
describe '#category_top_topics' do
4477
before do
4578
stub_get("#{host}/c/category-slug/l/top.json")
@@ -55,6 +88,23 @@
5588
end
5689
end
5790

91+
describe '#category_top_topics_full' do
92+
before do
93+
stub_get("#{host}/c/category-slug/l/top.json")
94+
.to_return(
95+
body: fixture("category_topics.json"),
96+
headers: { content_type: "application/json" }
97+
)
98+
end
99+
100+
it "returns the entire top topics in a category response" do
101+
topics = subject.category_top_topics_full('category-slug')
102+
expect(topics).to be_a Hash
103+
expect(topics).to have_key 'topic_list'
104+
expect(topics).to have_key 'users'
105+
end
106+
end
107+
58108
describe '#category_new_topics' do
59109
before do
60110
stub_get("#{host}/c/category-slug/l/new.json")
@@ -70,6 +120,23 @@
70120
end
71121
end
72122

123+
describe '#category_new_topics_full' do
124+
before do
125+
stub_get("#{host}/c/category-slug/l/new.json")
126+
.to_return(
127+
body: fixture("category_topics.json"),
128+
headers: { content_type: "application/json" }
129+
)
130+
end
131+
132+
it "returns the new topics in a category" do
133+
topics = subject.category_new_topics_full('category-slug')
134+
expect(topics).to be_a Hash
135+
expect(topics).to have_key 'topic_list'
136+
expect(topics).to have_key 'users'
137+
end
138+
end
139+
73140
describe '#category_new_category' do
74141
before do
75142
stub_post("#{host}/categories")

0 commit comments

Comments
 (0)