discourse_api 0.3.6 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 10a7f2e9839bfeea9edbb43a4f2fe535723e237e
4
- data.tar.gz: d8de8c9500231bd8ba26593e2ac7b266e4be067c
3
+ metadata.gz: ef1ea595c7e724545877e4a688f7f192a1f63573
4
+ data.tar.gz: f97ed183f55cae5b6cb01616264cabf5249bdbdb
5
5
  SHA512:
6
- metadata.gz: 14b552567338b8f699eeb77efa4de86e334052989a5cdbf6007ecfde4e1a128e9d03ae1d9d9c2c5a501fe1f3da2f76572122b26472f60c462c909bbf2107aaa4
7
- data.tar.gz: 61ac275015ae01fef9b9e0b6434e2c175a1f17b7dab3bd291418b53284063e7da45373affab2a741e4f74eaf4f3de6b5748c5683258307411feee37e2a8ee442
6
+ metadata.gz: 31b68b7b2101e74ca1dcb84c8528c129b630cdd1ff74a1c2eeadc42529562fc8dcfb5f13ed899dc94fe8513f1e4c0c314d6609fe7a1f0c3eef5436cb344b9751
7
+ data.tar.gz: eac6dbdd5cdecae47f0b2b4fac2a819b324437831b8aabb984db2f0b9f86efe0d4872b6a1970f67d2472665ce893ad13a09b238b60cff6c3726a7f94e31094f2
data/CHANGELOG.md CHANGED
@@ -21,3 +21,10 @@
21
21
  - be able to specify SSL connection settings
22
22
  - list api keys generated
23
23
  - list backups created
24
+
25
+ ## 0.4.0
26
+
27
+ - [FEATURE] generate an api key for a user
28
+ - [FEATURE] revoke an api key for a user
29
+ - [FEATURE] update user trust level
30
+ - [FEATURE] grant user badge
@@ -13,5 +13,7 @@ puts client.update_user("batman", name: "Bruce Wayne")
13
13
  puts client.update_email("batman", "batman@gotham.com")
14
14
  # update avatar of user whose username is "batman"
15
15
  puts client.update_avatar("batman", "http://meta-discourse.r.worldssl.net/uploads/default/2497/724a6ef2e79d2bc7.png")
16
+ # update trust level of user whose id is "102"
17
+ puts client.update_trust_level(user_id: 102, level: 2)
16
18
  # log out everywhere and refresh browser of user whose id is "2"
17
19
  puts client.log_out(2)
@@ -5,6 +5,14 @@ module DiscourseApi
5
5
  response = get("/admin/api.json")
6
6
  response.body
7
7
  end
8
+
9
+ def generate_api_key(user_id)
10
+ response = post("/admin/users/#{user_id}/generate_api_key.json")
11
+ end
12
+
13
+ def revoke_api_key(user_id)
14
+ response = delete("/admin/users/#{user_id}/revoke_api_key.json")
15
+ end
8
16
  end
9
17
  end
10
18
  end
@@ -10,6 +10,10 @@ module DiscourseApi
10
10
  response = get("/users/#{username}/activity/badges.json")
11
11
  response.body['badges']
12
12
  end
13
+
14
+ def grant_user_badge(params={})
15
+ post("/user_badges", params)
16
+ end
13
17
  end
14
18
  end
15
19
  end
@@ -3,11 +3,11 @@ module DiscourseApi
3
3
  module Categories
4
4
  # :color and :text_color are RGB hexadecimal strings
5
5
  def create_category(args)
6
- params = API.params(args)
7
- .required(:name, :color, :text_color)
8
- .optional(:description)
9
- .default(parent_category_id: nil)
10
- response = (post "/categories", params)
6
+ args = API.params(args)
7
+ .required(:name, :color, :text_color)
8
+ .optional(:description)
9
+ .default(parent_category_id: nil)
10
+ response = post("/categories", args)
11
11
  response['category']
12
12
  end
13
13
 
@@ -2,11 +2,11 @@ module DiscourseApi
2
2
  module API
3
3
  module Groups
4
4
  def create_group(args)
5
- params = API.params(args)
6
- .required(:name)
7
- .default(visible: true)
8
- .to_h
9
- post("/admin/groups", group: params)
5
+ args = API.params(args)
6
+ .required(:name)
7
+ .default(visible: true)
8
+ .to_h
9
+ post("/admin/groups", group: args)
10
10
  end
11
11
 
12
12
  def groups
@@ -2,13 +2,15 @@ module DiscourseApi
2
2
  module API
3
3
  module Posts
4
4
  def create_post(args)
5
- post("/posts", API.params(args)
6
- .required(:topic_id, :raw))
5
+ args = API.params(args)
6
+ .required(:topic_id, :raw)
7
+ post("/posts", args)
7
8
  end
8
9
 
9
10
  def get_post(id, args = {})
10
- response = get("/posts/#{id}.json", API.params(args)
11
- .optional(:version))
11
+ args = API.params(args)
12
+ .optional(:version)
13
+ response = get("/posts/#{id}.json", args)
12
14
  response[:body]
13
15
  end
14
16
 
@@ -5,12 +5,9 @@ module DiscourseApi
5
5
  # :skip_validations OPTIONAL boolean
6
6
  # :auto_track OPTIONAL boolean
7
7
  def create_topic(args)
8
-
9
- args =
10
- API.params(args)
11
- .required(:title, :raw)
12
- .optional(:skip_validations, :category, :auto_track)
13
-
8
+ args = API.params(args)
9
+ .required(:title, :raw)
10
+ .optional(:skip_validations, :category, :auto_track)
14
11
  post("/posts", args.to_h)
15
12
  end
16
13
 
@@ -11,11 +11,11 @@ module DiscourseApi
11
11
  end
12
12
 
13
13
  def update_avatar(args)
14
- params = API.params(args)
15
- .required(:username, :file)
16
- .default(image_type: 'avatar')
17
- .to_h
18
- upload_response = post("/users/#{args[:username]}/preferences/user_image", params)
14
+ args = API.params(args)
15
+ .required(:username, :file)
16
+ .default(image_type: 'avatar')
17
+ .to_h
18
+ upload_response = post("/users/#{args[:username]}/preferences/user_image", args)
19
19
  put("/users/#{args[:username]}/preferences/avatar/pick", { upload_id: upload_response['upload_id'] })
20
20
  end
21
21
 
@@ -31,12 +31,20 @@ module DiscourseApi
31
31
  put("/users/#{username}/preferences/username", { new_username: new_username, api_key: api_key })
32
32
  end
33
33
 
34
+ def update_trust_level(args)
35
+ args = API.params(args)
36
+ .required(:user_id, :level)
37
+ .to_h
38
+ response = put("/admin/users/#{args[:user_id]}/trust_level", args)
39
+ response[:body]
40
+ end
41
+
34
42
  def create_user(args)
35
- params = API.params(args)
36
- .required(:name, :email, :password, :username)
37
- .optional(:active)
38
- .to_h
39
- post("/users", params)
43
+ args = API.params(args)
44
+ .required(:name, :email, :password, :username)
45
+ .optional(:active)
46
+ .to_h
47
+ post("/users", args)
40
48
  end
41
49
 
42
50
  def log_out(id)
@@ -1,3 +1,3 @@
1
1
  module DiscourseApi
2
- VERSION = "0.3.6"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -1,16 +1,27 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe DiscourseApi::API::ApiAdmin do
4
- subject { DiscourseApi::Client.new("http://localhost:3000", "test_d7fd0429940", "test_user" )}
4
+ subject {
5
+ DiscourseApi::Client.new(
6
+ "http://localhost:3000",
7
+ "test_d7fd0429940",
8
+ "test_user"
9
+ )
10
+ }
5
11
 
6
12
  describe "#api" do
7
13
  before do
8
- stub_get("http://localhost:3000/admin/api.json?api_key=test_d7fd0429940&api_username=test_user").to_return(body: fixture("api.json"), headers: { content_type: "application/json" })
14
+ url = "http://localhost:3000/admin/api.json?" +
15
+ "api_key=test_d7fd0429940&api_username=test_user"
16
+ stub_get(url).to_return(body: fixture("api.json"),
17
+ headers: { content_type: "application/json" })
9
18
  end
10
19
 
11
20
  it "requests the correct resource" do
12
21
  subject.api
13
- expect(a_get("http://localhost:3000/admin/api.json?api_key=test_d7fd0429940&api_username=test_user")).to have_been_made
22
+ url = "http://localhost:3000/admin/api.json?" +
23
+ "api_key=test_d7fd0429940&api_username=test_user"
24
+ expect(a_get(url)).to have_been_made
14
25
  end
15
26
 
16
27
  it "returns the requested api keys" do
@@ -20,4 +31,33 @@ describe DiscourseApi::API::ApiAdmin do
20
31
  expect(api.first).to have_key('key')
21
32
  end
22
33
  end
34
+
35
+ describe "#generate_api_key" do
36
+ before do
37
+ url = "http://localhost:3000/admin/users/2/generate_api_key.json?" +
38
+ "api_key=test_d7fd0429940&api_username=test_user"
39
+ stub_post(url).to_return(body: fixture("generate_api_key.json"),
40
+ headers: { content_type: "application/json" })
41
+ end
42
+
43
+ it "returns the generated api key" do
44
+ api_key = subject.generate_api_key(2)
45
+ expect(api_key).to be_a Hash
46
+ expect(api_key['api_key']).to have_key('key')
47
+ end
48
+ end
49
+
50
+ describe "#revoke_api_key" do
51
+ before do
52
+ url = "http://localhost:3000/admin/users/2/revoke_api_key.json?" +
53
+ "api_key=test_d7fd0429940&api_username=test_user"
54
+ stub_post(url).to_return(body: "",
55
+ headers: { content_type: "application/json" })
56
+ end
57
+
58
+ it "returns 200" do
59
+ response = subject.revoke_api_key(2)
60
+ expect(response['status']).to eq(200)
61
+ end
62
+ end
23
63
  end
@@ -166,4 +166,26 @@ describe DiscourseApi::API::Users do
166
166
  expect(users.first).to be_a Hash
167
167
  end
168
168
  end
169
+
170
+ describe "#update_trust_level" do
171
+ before do
172
+ url = "http://localhost:3000/admin/users/2/trust_level?api_key=test_d7fd0429940&api_username=test_user"
173
+ stub_get(url).to_return(body: fixture("update_trust_level.json"),
174
+ headers: { content_type: "application/json" })
175
+ end
176
+
177
+ it "makes the correct put request" do
178
+ params = { user_id: 2, level: 2 }
179
+ subject.update_trust_level(params)
180
+ url = "http://localhost:3000/admin/users/2/trust_level?api_key=test_d7fd0429940&api_username=test_user"
181
+ expect(a_put(url)).to have_been_made
182
+ end
183
+
184
+ it "updates the trust_level" do
185
+ params = { user_id: 2, level: 2 }
186
+ admin_user = subject.update_trust_level(params)
187
+ expect(admin_user).to be_a Hash
188
+ expect(admin_user['admin_user']).to have_key('trust_level')
189
+ end
190
+ end
169
191
  end
@@ -0,0 +1,12 @@
1
+ {
2
+ "api_key": {
3
+ "id": 5,
4
+ "key": "e722e04df8cf6d097d565ca04eea1ff8e9e8f09beb405bae6f0c79852916f334",
5
+ "user": {
6
+ "id": 2,
7
+ "username": "robin",
8
+ "uploaded_avatar_id": 3,
9
+ "avatar_template": "/user_avatar/localhost/robin/{size}/3.png"
10
+ }
11
+ }
12
+ }
@@ -0,0 +1,37 @@
1
+ {
2
+ "admin_user": {
3
+ "id": 2,
4
+ "username": "robin",
5
+ "uploaded_avatar_id": 3,
6
+ "avatar_template": "/user_avatar/localhost/robin/{size}/3.png",
7
+ "active": true,
8
+ "admin": false,
9
+ "moderator": false,
10
+ "last_seen_at": "2014-09-19T13:00:20.939Z",
11
+ "last_emailed_at": "2015-01-01T01:01:24.438Z",
12
+ "created_at": "2014-09-13T14:27:07.352Z",
13
+ "last_seen_age": "4mon",
14
+ "last_emailed_age": "13d",
15
+ "created_at_age": "4mon",
16
+ "username_lower": "robin",
17
+ "trust_level": 2,
18
+ "trust_level_locked": false,
19
+ "flag_level": 0,
20
+ "title": null,
21
+ "suspended_at": null,
22
+ "suspended_till": null,
23
+ "suspended": null,
24
+ "blocked": false,
25
+ "time_read": "< 1m",
26
+ "days_visited": 3,
27
+ "posts_read_count": 2,
28
+ "topics_entered": 2,
29
+ "post_count": 0,
30
+ "can_send_activation_email": true,
31
+ "can_activate": false,
32
+ "can_deactivate": true,
33
+ "ip_address": "127.0.0.1",
34
+ "registration_ip_address": "127.0.0.1",
35
+ "single_sign_on_record": null
36
+ }
37
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discourse_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-01-11 00:00:00.000000000 Z
13
+ date: 2015-01-15 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: faraday
@@ -249,6 +249,7 @@ files:
249
249
  - spec/fixtures/category_latest_topics.json
250
250
  - spec/fixtures/email_list_all.json
251
251
  - spec/fixtures/email_settings.json
252
+ - spec/fixtures/generate_api_key.json
252
253
  - spec/fixtures/groups.json
253
254
  - spec/fixtures/hot.json
254
255
  - spec/fixtures/latest.json
@@ -260,6 +261,7 @@ files:
260
261
  - spec/fixtures/topic.json
261
262
  - spec/fixtures/topic_invite_user.json
262
263
  - spec/fixtures/topics_created_by.json
264
+ - spec/fixtures/update_trust_level.json
263
265
  - spec/fixtures/user.json
264
266
  - spec/fixtures/user_activate_success.json
265
267
  - spec/fixtures/user_badges.json
@@ -316,6 +318,7 @@ test_files:
316
318
  - spec/fixtures/category_latest_topics.json
317
319
  - spec/fixtures/email_list_all.json
318
320
  - spec/fixtures/email_settings.json
321
+ - spec/fixtures/generate_api_key.json
319
322
  - spec/fixtures/groups.json
320
323
  - spec/fixtures/hot.json
321
324
  - spec/fixtures/latest.json
@@ -327,6 +330,7 @@ test_files:
327
330
  - spec/fixtures/topic.json
328
331
  - spec/fixtures/topic_invite_user.json
329
332
  - spec/fixtures/topics_created_by.json
333
+ - spec/fixtures/update_trust_level.json
330
334
  - spec/fixtures/user.json
331
335
  - spec/fixtures/user_activate_success.json
332
336
  - spec/fixtures/user_badges.json