Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

Best practice to add a shared function in Ruby on Rails

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...