Please note: My question is not Rpush specific, but using this gem raises the question I have.
I have a Ruby on Rails 5 app, and have rudimentarily integrated the Rpush gem to support Push Notifications. I've set it up, and it works great for basic tests, All Good. Now I'm confident in the library, I'd now like to properly integrate it into my app.
The API to queue the notification for sending requires passing in the message itself, but also some params that are fixed for the app (for instance, I will always use the same sound), and I'd also like to use shared code to look up a user based on their app reference, rather than their changeable device token.
Rpush API
n = Rpush::Apns::Notification.new
n.app = Rpush::Apnsp8::App.find_by_name("ios_app")
n.device_token = "..." # hex string
n.alert = "Hello, World!"
n.data = { foo: :bar }
n.save!
So, I'd like to add a wrapper to take the interesting fields, and then call the Rpush API. This could be called from any of the app controllers.
MyPush(user_id: user_id, message: "Hello, World!")
...which would invoke a custom (and shared) function that I will write to look up the user's device and complete the call to the Rpush API with my standard params.
As the Rpush library is distributed as a gem, so I don't want to make any changes to that code.
What is the best practice to add a wrapper like this that can be invoked from across the app?
question from:
https://stackoverflow.com/questions/65936472/best-practice-to-add-a-shared-function-in-ruby-on-rails 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…