DEV: separates required & optional params in invite module (#228)

DEV: separates required & optional params in invite module (#228)

  • DEV: seperates required & optional params in invite module

  • DEV: adds ‘agrs’ variable to post method

  • DEV: adds topic_id as seperate param

  • DEV: deprecated ‘invite_user_to_topic’ method

  • DEV: deprecated ‘invite_user_to_topic’ method

  • FIX: changes remove date to 2022

  • DEV: rubocop

diff --git a/examples/invite_users.rb b/examples/invite_users.rb
index 1e9d4a0..72c7f4d 100644
--- a/examples/invite_users.rb
+++ b/examples/invite_users.rb
@@ -15,4 +15,4 @@ client.invite_user(email: "name@example.com", group_ids: "41,42")
 client.invite_user_to_topic(email: "foo@bar.com", topic_id: 1)
 
 # if the user is an admin you may invite to a group as well
-client.invite_user_to_topic(email: "foo@bar.com", topic_id: 1, group_ids: "1,2,3")
+client.invite_user_to_topic(email: "foo@bar.com", group_ids: "1,2,3", topic_id: 1)
diff --git a/lib/discourse_api/api/invite.rb b/lib/discourse_api/api/invite.rb
index 558e5b8..2660edc 100644
--- a/lib/discourse_api/api/invite.rb
+++ b/lib/discourse_api/api/invite.rb
@@ -3,11 +3,36 @@ module DiscourseApi
   module API
     module Invite
       def invite_user(params = {})
-        post("/invites", params)
+        args = API.params(params)
+          .optional(
+            :email,
+            :skip_email,
+            :custom_message,
+            :max_redemptions_allowed,
+            :topic_id,
+            :group_ids,
+            :expires_at
+          ).to_h
+
+        post("/invites", args)
       end
 
+      # TODO: Deprecated. Remove after 20220506
       def invite_user_to_topic(params = {})
-        post("/t/#{params[:topic_id]}/invite", params)
+        deprecated(__method__, 'invite_to_topic')
+        invite_to_topic(params[:topic_id], params)
+      end
+
+      def invite_to_topic(topic_id, params = {})
+        args = API.params(params)
+          .optional(
+            :email,
+            :user,
+            :group_ids,
+            :custom_message
+          ).to_h
+
+        post("/t/#{topic_id}/invite", args)
       end
 
       # requires this plugin => https://github.com/discourse/discourse-invite-tokens
diff --git a/spec/discourse_api/api/topics_spec.rb b/spec/discourse_api/api/topics_spec.rb
index ee2584a..0adbabe 100644
--- a/spec/discourse_api/api/topics_spec.rb
+++ b/spec/discourse_api/api/topics_spec.rb
@@ -25,6 +25,11 @@ describe DiscourseApi::API::Topics do
       expect(a_post("#{host}/t/12/invite")).to have_been_made
     end
 
+    it "requests the correct resource with new method 'invite_to_topic'" do
+      subject.invite_to_topic(12, email: "fake_user@example.com")
+      expect(a_post("#{host}/t/12/invite")).to have_been_made
+    end
+
     it "returns success" do
       response = subject.invite_user_to_topic(email: "fake_user@example.com", topic_id: 12)
       expect(response).to be_a Hash

GitHub sha: 3227bbdd

This commit appears in #228 which was approved by blake. It was merged by blake.