discourse_api 0.34.0 → 0.35.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.rubocop.yml +6 -0
  4. data/.travis.yml +10 -4
  5. data/CHANGELOG.md +12 -0
  6. data/Gemfile +1 -0
  7. data/Guardfile +2 -1
  8. data/Rakefile +7 -2
  9. data/discourse_api.gemspec +23 -22
  10. data/examples/backups.rb +1 -0
  11. data/examples/badges.rb +1 -0
  12. data/examples/category.rb +1 -0
  13. data/examples/change_topic_status.rb +3 -3
  14. data/examples/create_private_message.rb +1 -0
  15. data/examples/create_topic.rb +1 -0
  16. data/examples/create_update_category.rb +1 -1
  17. data/examples/create_user.rb +1 -0
  18. data/examples/dashboard.rb +3 -2
  19. data/examples/disposable_invite_tokens.rb +1 -0
  20. data/examples/example.rb +1 -0
  21. data/examples/group_set_user_notification_level.rb +1 -1
  22. data/examples/groups.rb +1 -0
  23. data/examples/invite_users.rb +1 -0
  24. data/examples/post_action.rb +1 -0
  25. data/examples/search.rb +1 -0
  26. data/examples/sent_private_messages.rb +1 -0
  27. data/examples/sso.rb +1 -0
  28. data/examples/topic_lists.rb +1 -0
  29. data/examples/update_user.rb +1 -0
  30. data/examples/upload_file.rb +1 -0
  31. data/lib/discourse_api.rb +1 -0
  32. data/lib/discourse_api/api/api_key.rb +3 -2
  33. data/lib/discourse_api/api/backups.rb +1 -0
  34. data/lib/discourse_api/api/badges.rb +5 -4
  35. data/lib/discourse_api/api/categories.rb +16 -15
  36. data/lib/discourse_api/api/dashboard.rb +7 -6
  37. data/lib/discourse_api/api/email.rb +1 -0
  38. data/lib/discourse_api/api/groups.rb +11 -10
  39. data/lib/discourse_api/api/invite.rb +4 -3
  40. data/lib/discourse_api/api/notifications.rb +2 -1
  41. data/lib/discourse_api/api/params.rb +3 -2
  42. data/lib/discourse_api/api/posts.rb +7 -6
  43. data/lib/discourse_api/api/private_messages.rb +4 -3
  44. data/lib/discourse_api/api/search.rb +2 -1
  45. data/lib/discourse_api/api/site_settings.rb +3 -2
  46. data/lib/discourse_api/api/sso.rb +3 -2
  47. data/lib/discourse_api/api/tags.rb +1 -0
  48. data/lib/discourse_api/api/topics.rb +17 -16
  49. data/lib/discourse_api/api/uploads.rb +4 -3
  50. data/lib/discourse_api/api/user_actions.rb +3 -2
  51. data/lib/discourse_api/api/users.rb +23 -20
  52. data/lib/discourse_api/client.rb +8 -7
  53. data/lib/discourse_api/error.rb +2 -1
  54. data/lib/discourse_api/single_sign_on.rb +1 -0
  55. data/lib/discourse_api/version.rb +2 -1
  56. data/spec/discourse_api/api/users_spec.rb +13 -13
  57. metadata +38 -25
  58. data/routes.txt +0 -203
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module DiscourseApi
2
3
  module API
3
4
  module Email
@@ -1,11 +1,12 @@
1
+ # frozen_string_literal: true
1
2
  module DiscourseApi
2
3
  module API
3
4
  module Groups
4
5
  def create_group(args)
5
6
  args = API.params(args)
6
- .required(:name)
7
- .default(visibility_level: 0)
8
- .optional(:mentionable_level,
7
+ .required(:name)
8
+ .default(visibility_level: 0)
9
+ .optional(:mentionable_level,
9
10
  :messageable_level,
10
11
  :automatic_membership_email_domains,
11
12
  :automatic_membership_retroactive,
@@ -25,14 +26,14 @@ module DiscourseApi
25
26
  :usernames,
26
27
  :owner_usernames,
27
28
  :membership_request_template)
28
- .to_h
29
+ .to_h
29
30
  post("/admin/groups", group: args)
30
31
  end
31
32
 
32
33
  def update_group(group_id, args)
33
34
  args = API.params(args)
34
- .default(visibility_level: 0)
35
- .optional(:mentionable_level,
35
+ .default(visibility_level: 0)
36
+ .optional(:mentionable_level,
36
37
  :messageable_level,
37
38
  :name,
38
39
  :automatic_membership_email_domains,
@@ -53,7 +54,7 @@ module DiscourseApi
53
54
  :usernames,
54
55
  :owner_usernames,
55
56
  :membership_request_template)
56
- .to_h
57
+ .to_h
57
58
  put("/groups/#{group_id}", group: args)
58
59
  end
59
60
 
@@ -107,9 +108,9 @@ module DiscourseApi
107
108
 
108
109
  def group_members(group_name, params = {})
109
110
  params = API.params(params)
110
- .optional(:offset, :limit)
111
- .default(offset: 0, limit: 100)
112
- .to_h
111
+ .optional(:offset, :limit)
112
+ .default(offset: 0, limit: 100)
113
+ .to_h
113
114
  response = get("/groups/#{group_name}/members.json", params)
114
115
  response.body['members']
115
116
  end
@@ -1,16 +1,17 @@
1
+ # frozen_string_literal: true
1
2
  module DiscourseApi
2
3
  module API
3
4
  module Invite
4
- def invite_user(params={})
5
+ def invite_user(params = {})
5
6
  post("/invites", params)
6
7
  end
7
8
 
8
- def invite_user_to_topic(params={})
9
+ def invite_user_to_topic(params = {})
9
10
  post("/t/#{params[:topic_id]}/invite", params)
10
11
  end
11
12
 
12
13
  # requires this plugin => https://github.com/discourse/discourse-invite-tokens
13
- def disposable_tokens(params={})
14
+ def disposable_tokens(params = {})
14
15
  post("/invite-token/generate", params)
15
16
  end
16
17
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module DiscourseApi
2
3
  module API
3
4
  module Notifications
@@ -7,4 +8,4 @@ module DiscourseApi
7
8
  end
8
9
  end
9
10
  end
10
- end
11
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module DiscourseApi
2
3
  module API
3
4
  def self.params(args)
@@ -24,7 +25,7 @@ module DiscourseApi
24
25
  end
25
26
 
26
27
  def default(args)
27
- args.each do |k,v|
28
+ args.each do |k, v|
28
29
  @defaults[k] = v
29
30
  end
30
31
  self
@@ -42,7 +43,7 @@ module DiscourseApi
42
43
  h[k] = @args[k] if @args.include?(k)
43
44
  end
44
45
 
45
- @defaults.each do |k,v|
46
+ @defaults.each do |k, v|
46
47
  @args.key?(k) ? h[k] = @args[k] : h[k] = v
47
48
  end
48
49
 
@@ -1,22 +1,23 @@
1
+ # frozen_string_literal: true
1
2
  module DiscourseApi
2
3
  module API
3
4
  module Posts
4
5
  def create_post(args)
5
6
  args = API.params(args)
6
- .required(:topic_id, :raw)
7
- .optional(:created_at, :api_username)
7
+ .required(:topic_id, :raw)
8
+ .optional(:created_at, :api_username)
8
9
  post("/posts", args)
9
10
  end
10
11
 
11
12
  def create_post_action(args)
12
13
  args = API.params(args)
13
- .required(:id, :post_action_type_id)
14
+ .required(:id, :post_action_type_id)
14
15
  post("/post_actions", args.to_h.merge(flag_topic: false))
15
16
  end
16
17
 
17
18
  def get_post(id, args = {})
18
19
  args = API.params(args)
19
- .optional(:version)
20
+ .optional(:version)
20
21
  response = get("/posts/#{id}.json", args)
21
22
  response[:body]
22
23
  end
@@ -26,7 +27,7 @@ module DiscourseApi
26
27
  end
27
28
 
28
29
  def edit_post(id, raw)
29
- put("/posts/#{id}", post: {raw: raw})
30
+ put("/posts/#{id}", post: { raw: raw })
30
31
  end
31
32
 
32
33
  def delete_post(id)
@@ -38,7 +39,7 @@ module DiscourseApi
38
39
  end
39
40
 
40
41
  def post_action_users(post_id, post_action_type_id)
41
- response = get("/post_action_users.json", {id: post_id, post_action_type_id: post_action_type_id})
42
+ response = get("/post_action_users.json", id: post_id, post_action_type_id: post_action_type_id)
42
43
  response[:body]
43
44
  end
44
45
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module DiscourseApi
2
3
  module API
3
4
  module PrivateMessages
@@ -5,11 +6,11 @@ module DiscourseApi
5
6
  # :target_usernames REQUIRED comma separated list of usernames
6
7
  # :category OPTIONAL name of category, not ID
7
8
  # :created_at OPTIONAL seconds since epoch.
8
- def create_private_message(args={})
9
+ def create_private_message(args = {})
9
10
  args[:archetype] = 'private_message'
10
11
  args = API.params(args)
11
- .required(:title, :raw, :target_usernames, :archetype)
12
- .optional(:category, :created_at, :api_username)
12
+ .required(:title, :raw, :target_usernames, :archetype)
13
+ .optional(:category, :created_at, :api_username)
13
14
  post("/posts", args.to_h)
14
15
  end
15
16
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module DiscourseApi
2
3
  module API
3
4
  module Search
@@ -7,7 +8,7 @@ module DiscourseApi
7
8
  # @param options [Hash] A customizable set of options
8
9
  # @option options [String] :type_filter Returns results of the specified type.
9
10
  # @return [Array] Return results as an array of Hashes.
10
- def search(term, options={})
11
+ def search(term, options = {})
11
12
  raise ArgumentError.new("#{term} is required but not specified") unless term
12
13
  raise ArgumentError.new("#{term} is required but not specified") unless !term.empty?
13
14
 
@@ -1,9 +1,10 @@
1
+ # frozen_string_literal: true
1
2
  module DiscourseApi
2
3
  module API
3
4
  module SiteSettings
4
- def site_setting_update(args={})
5
+ def site_setting_update(args = {})
5
6
  params = API.params(args)
6
- .required(:name, :value).to_h
7
+ .required(:name, :value).to_h
7
8
  new_site_setting = { params[:name] => params[:value] }
8
9
  response = put("/admin/site_settings/#{params[:name]}", new_site_setting)
9
10
  end
@@ -1,7 +1,8 @@
1
+ # frozen_string_literal: true
1
2
  module DiscourseApi
2
3
  module API
3
4
  module SSO
4
- def sync_sso(params={})
5
+ def sync_sso(params = {})
5
6
  sso = DiscourseApi::SingleSignOn.new
6
7
  sso.sso_secret = params[:sso_secret]
7
8
  sso.name = params[:name]
@@ -14,7 +15,7 @@ module DiscourseApi
14
15
  sso.avatar_force_update = params[:avatar_force_update] === true
15
16
  sso.add_groups = params[:add_groups]
16
17
  sso.remove_groups = params[:remove_groups]
17
- params.keys.select{|key| key.to_s.start_with?("custom") }.each do |custom_key|
18
+ params.keys.select { |key| key.to_s.start_with?("custom") }.each do |custom_key|
18
19
  sso.custom_fields[custom_key] = params[custom_key]
19
20
  end
20
21
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module DiscourseApi
2
3
  module API
3
4
  module Tags
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module DiscourseApi
2
3
  module API
3
4
  module Topics
@@ -5,55 +6,55 @@ module DiscourseApi
5
6
  # :skip_validations OPTIONAL boolean
6
7
  # :auto_track OPTIONAL boolean
7
8
  # :created_at OPTIONAL seconds since epoch.
8
- def create_topic(args={})
9
+ def create_topic(args = {})
9
10
  args = API.params(args)
10
- .required(:title, :raw)
11
- .optional(:skip_validations, :category, :auto_track, :created_at, :api_username)
11
+ .required(:title, :raw)
12
+ .optional(:skip_validations, :category, :auto_track, :created_at, :api_username)
12
13
  post("/posts", args.to_h)
13
14
  end
14
15
 
15
16
  def create_topic_action(args)
16
17
  args = API.params(args)
17
- .required(:id, :post_action_type_id)
18
+ .required(:id, :post_action_type_id)
18
19
  post("/post_actions", args.to_h.merge(flag_topic: true))
19
20
  end
20
21
 
21
22
  # timestamp is seconds past the epoch.
22
- def edit_topic_timestamp(topic_id,timestamp)
23
- put("/t/#{topic_id}/change-timestamp", { timestamp: timestamp })
23
+ def edit_topic_timestamp(topic_id, timestamp)
24
+ put("/t/#{topic_id}/change-timestamp", timestamp: timestamp)
24
25
  end
25
26
 
26
- def latest_topics(params={})
27
+ def latest_topics(params = {})
27
28
  response = get('/latest.json', params)
28
29
  response[:body]['topic_list']['topics']
29
30
  end
30
31
 
31
- def new_topics(params={})
32
+ def new_topics(params = {})
32
33
  response = get("/new.json", params)
33
34
  response[:body]['topic_list']['topics']
34
35
  end
35
36
 
36
37
  def rename_topic(topic_id, title)
37
- put("/t/#{topic_id}.json", { topic_id: topic_id, title: title })
38
+ put("/t/#{topic_id}.json", topic_id: topic_id, title: title)
38
39
  end
39
40
 
40
41
  def recategorize_topic(topic_id, category_id)
41
- put("/t/#{topic_id}.json", { topic_id: topic_id, category_id: category_id })
42
+ put("/t/#{topic_id}.json", topic_id: topic_id, category_id: category_id)
42
43
  end
43
44
 
44
- def change_topic_status(topic_slug, topic_id, params={})
45
+ def change_topic_status(topic_slug, topic_id, params = {})
45
46
  params = API.params(params)
46
- .required(:status, :enabled)
47
- .optional(:api_username)
47
+ .required(:status, :enabled)
48
+ .optional(:api_username)
48
49
  put("/t/#{topic_slug}/#{topic_id}/status", params.to_h)
49
50
  end
50
51
 
51
- def topic(id, params={})
52
+ def topic(id, params = {})
52
53
  response = get("/t/#{id}.json", params)
53
54
  response[:body]
54
55
  end
55
56
 
56
- def topics_by(username, params={})
57
+ def topics_by(username, params = {})
57
58
  response = get("/topics/created-by/#{username}.json", params)
58
59
  response[:body]['topic_list']['topics']
59
60
  end
@@ -62,7 +63,7 @@ module DiscourseApi
62
63
  delete("/t/#{id}.json")
63
64
  end
64
65
 
65
- def topic_posts(topic_id, post_ids=[])
66
+ def topic_posts(topic_id, post_ids = [])
66
67
  url = "/t/#{topic_id}/posts.json"
67
68
  if post_ids.count > 0
68
69
  url << '?'
@@ -1,11 +1,12 @@
1
+ # frozen_string_literal: true
1
2
  module DiscourseApi
2
3
  module API
3
4
  module Uploads
4
5
  def upload_file(args)
5
6
  args = API.params(args)
6
- .optional(:file, :url, :user_id)
7
- .default(type: 'composer', synchronous: true)
8
- .to_h
7
+ .optional(:file, :url, :user_id)
8
+ .default(type: 'composer', synchronous: true)
9
+ .to_h
9
10
  post('/uploads', args)
10
11
  end
11
12
  end
@@ -1,14 +1,15 @@
1
+ # frozen_string_literal: true
1
2
  module DiscourseApi
2
3
  module API
3
4
  module UserActions
4
5
  def user_replies(username)
5
- params = {"username": username, "filter": '5'}
6
+ params = { "username": username, "filter": '5' }
6
7
  response = get("/user_actions.json", params)
7
8
  response.body["user_actions"]
8
9
  end
9
10
 
10
11
  def user_topics_and_replies(username)
11
- params = {"username": username, "filter": "4,5"}
12
+ params = { "username": username, "filter": "4,5" }
12
13
  response = get("/user_actions.json", params)
13
14
  response.body["user_actions"]
14
15
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module DiscourseApi
2
3
  module API
3
4
  module Users
@@ -5,7 +6,7 @@ module DiscourseApi
5
6
  put("/admin/users/#{id}/activate")
6
7
  end
7
8
 
8
- def user(username, params={})
9
+ def user(username, params = {})
9
10
  response = get("/users/#{username}.json", params)
10
11
  response[:body]['user']
11
12
  end
@@ -15,41 +16,43 @@ module DiscourseApi
15
16
  response[:body]['single_sign_on_record']
16
17
  end
17
18
 
18
- def update_avatar(args)
19
+ def update_avatar(username, args)
19
20
  args = API.params(args)
20
- .required(:username)
21
- .optional(:file, :url)
22
- .default(type: 'avatar', synchronous: true)
23
- .to_h
21
+ .optional(:file, :url)
22
+ .default(type: 'avatar', synchronous: true)
23
+ .to_h
24
24
  upload_response = post("/uploads", args)
25
- put("/users/#{args[:username]}/preferences/avatar/pick", { upload_id: upload_response['id'] })
25
+ put("/u/#{username}/preferences/avatar/pick", upload_id: upload_response['id'])
26
26
  end
27
27
 
28
28
  def update_email(username, email)
29
- put("/users/#{username}/preferences/email", { email: email, api_key: api_key })
29
+ put("/u/#{username}/preferences/email", email: email)
30
30
  end
31
31
 
32
- def update_user(username, params={})
33
- put("/users/#{username}", params)
32
+ def update_user(username, args)
33
+ args = API.params(args)
34
+ .optional(:name, :title, :bio_raw, :location, :website, :profile_background, :card_background)
35
+ .to_h
36
+ put("/u/#{username}", args)
34
37
  end
35
38
 
36
39
  def update_username(username, new_username)
37
- put("/users/#{username}/preferences/username", { new_username: new_username, api_key: api_key })
40
+ put("/u/#{username}/preferences/username", new_username: new_username)
38
41
  end
39
42
 
40
- def update_trust_level(args)
43
+ def update_trust_level(user_id, args)
41
44
  args = API.params(args)
42
- .required(:user_id, :level)
43
- .to_h
44
- response = put("/admin/users/#{args[:user_id]}/trust_level", args)
45
+ .required(:level)
46
+ .to_h
47
+ response = put("/admin/users/#{user_id}/trust_level", args)
45
48
  response[:body]
46
49
  end
47
50
 
48
51
  def create_user(args)
49
52
  args = API.params(args)
50
- .required(:name, :email, :password, :username)
51
- .optional(:active, :staged, :user_fields)
52
- .to_h
53
+ .required(:name, :email, :password, :username)
54
+ .optional(:active, :staged, :user_fields)
55
+ .to_h
53
56
  post("/users", args)
54
57
  end
55
58
 
@@ -57,7 +60,7 @@ module DiscourseApi
57
60
  post("/admin/users/#{id}/log_out")
58
61
  end
59
62
 
60
- def invite_admin(args={})
63
+ def invite_admin(args = {})
61
64
  post("/admin/users/invite_admin", args)
62
65
  end
63
66
 
@@ -82,7 +85,7 @@ module DiscourseApi
82
85
  end
83
86
 
84
87
  def suspend(user_id, suspend_until, reason)
85
- put("/admin/users/#{user_id}/suspend", {suspend_until: suspend_until, reason: reason})
88
+ put("/admin/users/#{user_id}/suspend", suspend_until: suspend_until, reason: reason)
86
89
  end
87
90
 
88
91
  def unsuspend(user_id)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'faraday'
2
3
  require 'faraday_middleware'
3
4
  require 'json'
@@ -58,7 +59,7 @@ module DiscourseApi
58
59
 
59
60
  def api_username=(api_username)
60
61
  @api_username = api_username
61
- @connection.params['api_username'] = api_username unless @connection.nil?
62
+ @connection.headers['Api-username'] = api_username unless @connection.nil?
62
63
  end
63
64
 
64
65
  def connection_options
@@ -75,15 +76,15 @@ module DiscourseApi
75
76
  connection_options[:ssl] = options
76
77
  end
77
78
 
78
- def delete(path, params={})
79
+ def delete(path, params = {})
79
80
  request(:delete, path, params)
80
81
  end
81
82
 
82
- def get(path, params={})
83
+ def get(path, params = {})
83
84
  request(:get, path, params)
84
85
  end
85
86
 
86
- def post(path, params={})
87
+ def post(path, params = {})
87
88
  response = request(:post, path, params)
88
89
  case response.status
89
90
  when 200, 201, 204
@@ -93,11 +94,11 @@ module DiscourseApi
93
94
  end
94
95
  end
95
96
 
96
- def put(path, params={})
97
+ def put(path, params = {})
97
98
  request(:put, path, params)
98
99
  end
99
100
 
100
- def patch(path, params={})
101
+ def patch(path, params = {})
101
102
  request(:patch, path, params)
102
103
  end
103
104
 
@@ -127,7 +128,7 @@ module DiscourseApi
127
128
  end
128
129
  end
129
130
 
130
- def request(method, path, params={})
131
+ def request(method, path, params = {})
131
132
  unless Hash === params
132
133
  params = params.to_h if params.respond_to? :to_h
133
134
  end