REST API vs. the Ruby API


(Kim Miller) #1

Is it safe to assume there is not a 1 to 1 mapping, i.e. the Ruby API does not do everything the REST API can do?

I did not see a way to update a user’s group notification level, so tried to write the method:

Am I on the right track here? If so, where do I place the method so it has access to the post method? Any examples I could reference on how this works, as I’m somewhat new to RoR.


(Kim Miller) #2

Ok, looks as if placing my method in the api code and calling thought the client works:

response = client.set_group_user_note_lev(@group_name, user['id'], 2)
puts response

Though the response did not seem to print as it does in Postman, e.g.:

{
  "success": "OK"
}

It seems messy adding my code to the published Ruby API’s code. Best practices?


(Simon Cossar) #3

From the discourse_api README:

Over time this project intends to have a full Discourse API. At the moment there are only a few endpoints available

Reopening the module where you are placing your code and adding your method there might work. Have a look at the second example here: modules_and_classes - Documentation for Ruby 2.3.0. Another option would be to make a pull request to have your code added to the gem.


(Simon Cossar) #4

Since I posted about reopening the module, I figured I should try it out with your method to make sure it’s possible. Adding this code to a file and running it from the command line is working for me.

require 'discourse_api'

module DiscourseApi
  module API
    module Notifications
    	def set_group_user_notification_level group, user_id, notification_level
    		post("/groups/#{group}/notifications?user_id=#{user_id}&notification_level=#{notification_level}")
    	end
    end
  end
end

client = DiscourseApi::Client.new("http://localhost:3000")
client.api_key = "<your-api-key>"
client.api_username = "system"

response = client.set_group_user_notification_level 'running', 1, 3

puts "Response: #{response}"

(Kim Miller) #5

Thanks for that guidance on reopening modules, which worked for me too. But in the end I submitted a pull request to add the endpoint & an example script to update users Group Notifications to the Group Default.