Last active
April 4, 2018 00:41
-
-
Save okbm/4f596edf8ff621c535326dba9c90c7c3 to your computer and use it in GitHub Desktop.
repro push rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Repro | |
class BasePushWorker | |
include Sidekiq::Worker | |
sidekiq_options retry: 1 | |
def perform(user_id, content_id = nil) | |
@user = User.find(user_id) | |
@content_id = content_id | |
return unless permit? | |
send_request | |
end | |
private | |
def send_request | |
# http://docs.repro.io/ja/dev/push-api/index.html | |
setting = api_endpoint | |
conn = Faraday.new | |
res = conn.post do |req| | |
req.url setting[:endpoint] | |
req.headers['X-Repro-Token'] = setting[:token] | |
req.headers['Accept'] = 'application/json' | |
req.headers['Content-Type'] = 'application/json' | |
req.body = create_send_json | |
end | |
# エラー処理いれる? | |
# return if response.status < 300 | |
p "HTTP Response: #{res.status} #{res.body} #{res.headers}" | |
end | |
def create_send_json | |
{ | |
"audience": { "user_ids": @user.uuid }, | |
"notification": { "message": message, 'deeplink_url': deeplink_url } | |
}.to_json | |
end | |
def permit? | |
raise NotImplementedError.new("You must implement #{self.class}##{__method__}") | |
end | |
def message | |
raise NotImplementedError.new("You must implement #{self.class}##{__method__}") | |
end | |
def deeplink_url | |
raise NotImplementedError.new("You must implement #{self.class}##{__method__}") | |
end | |
def api_endpoint | |
raise NotImplementedError.new("You must implement #{self.class}##{__method__}") | |
end | |
end | |
end | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Repro | |
class CommentWriteWorker < BasePushWorker | |
private | |
def permit? | |
true | |
end | |
def message | |
'あなたが書いた記事にコメントがきました' | |
end | |
def deeplink_url | |
"hoge://article?id=#{@content_id}" if @content_id.present? | |
end | |
def api_endpoint | |
setting[:endpoint] = 'https://marketing.repro.io/v1/push/xxxxx/deliver' | |
setting[:token] = 'xxxxx' | |
setting | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment