From 3aae0d039eb01a236d7a41824d3e58e872f841ef Mon Sep 17 00:00:00 2001 From: Mhd Sami Almouhtaseb Date: Mon, 10 Feb 2020 18:39:25 +0800 Subject: [PATCH 1/4] fix, fix an issue when loading the child class before the parent class --- lib/rtpush/adapters/fcm_adapter.rb | 2 ++ lib/rtpush/adapters/instapush_adapter.rb | 2 ++ lib/rtpush/adapters/rpush_adapter.rb | 2 ++ lib/rtpush/adapters/slack_adapter.rb | 2 ++ lib/rtpush/adapters/twilio_adapter.rb | 2 ++ 5 files changed, 10 insertions(+) diff --git a/lib/rtpush/adapters/fcm_adapter.rb b/lib/rtpush/adapters/fcm_adapter.rb index a759529..6f6adef 100644 --- a/lib/rtpush/adapters/fcm_adapter.rb +++ b/lib/rtpush/adapters/fcm_adapter.rb @@ -1,3 +1,5 @@ +require_relative './base_adapter' + module RTPush class FcmAdapter < RTPush::BaseAdapter class << self diff --git a/lib/rtpush/adapters/instapush_adapter.rb b/lib/rtpush/adapters/instapush_adapter.rb index 3a23415..489b507 100644 --- a/lib/rtpush/adapters/instapush_adapter.rb +++ b/lib/rtpush/adapters/instapush_adapter.rb @@ -1,3 +1,5 @@ +require_relative './base_adapter' + module RTPush class InstapushAdapter < RTPush::BaseAdapter class << self diff --git a/lib/rtpush/adapters/rpush_adapter.rb b/lib/rtpush/adapters/rpush_adapter.rb index 582a684..456a5d5 100644 --- a/lib/rtpush/adapters/rpush_adapter.rb +++ b/lib/rtpush/adapters/rpush_adapter.rb @@ -1,3 +1,5 @@ +require_relative './base_adapter' + module RTPush class RpushAdapter < RTPush::BaseAdapter class << self diff --git a/lib/rtpush/adapters/slack_adapter.rb b/lib/rtpush/adapters/slack_adapter.rb index 5b72343..c88688c 100644 --- a/lib/rtpush/adapters/slack_adapter.rb +++ b/lib/rtpush/adapters/slack_adapter.rb @@ -1,3 +1,5 @@ +require_relative './base_adapter' + module RTPush class SlackAdapter < RTPush::BaseAdapter class << self diff --git a/lib/rtpush/adapters/twilio_adapter.rb b/lib/rtpush/adapters/twilio_adapter.rb index ca88d77..5e5635a 100644 --- a/lib/rtpush/adapters/twilio_adapter.rb +++ b/lib/rtpush/adapters/twilio_adapter.rb @@ -1,3 +1,5 @@ +require_relative './base_adapter' + module RTPush class TwilioAdapter < RTPush::BaseAdapter class << self From c3bfa8e834652e096ea53ba75ffcb3f1b179d4ad Mon Sep 17 00:00:00 2001 From: Mhd Sami Almouhtaseb Date: Mon, 10 Feb 2020 18:40:21 +0800 Subject: [PATCH 2/4] impl, add http post callback notification implementation --- lib/rtpush.rb | 3 +++ lib/rtpush/adapters/http_adapter.rb | 42 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 lib/rtpush/adapters/http_adapter.rb diff --git a/lib/rtpush.rb b/lib/rtpush.rb index bf4ea50..bfd7211 100644 --- a/lib/rtpush.rb +++ b/lib/rtpush.rb @@ -6,6 +6,7 @@ module RTPush def self.initialize(options, message) raise ArgumentError, 'Missing Message param' if message.to_s.empty? raise ArgumentError, 'Missing Arguments params' if options.empty? + push(strategies(options), message) end @@ -21,6 +22,8 @@ def self.strategies(options) strategies = [] options.each do |option| case option + when 'post' + strategies << RTPush::HttpAdapter when 'sms' strategies << RTPush::TwilioAdapter when 'mobile' diff --git a/lib/rtpush/adapters/http_adapter.rb b/lib/rtpush/adapters/http_adapter.rb new file mode 100644 index 0000000..acc65c6 --- /dev/null +++ b/lib/rtpush/adapters/http_adapter.rb @@ -0,0 +1,42 @@ +require_relative './base_adapter' + +module RTPush + class HttpAdapter < RTPush::BaseAdapter + class << self + def push(message) + payload = { + notification: { + title: ENV['NOTIFICATION_TITLE'], + body: message + } + } + http_request(ENV['NOTIFICATION_URL'], 'post', payload) + rescue StandardError => e + raise Errors::AdapterError, e.message + end + + def http_request(url, http_method, payload) + uri = URI.parse url.strip + http = Net::HTTP.new(uri.host, uri.port) + if uri.scheme == 'https' + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + end + http.open_timeout = 5 + http.read_timeout = 30 + # http.set_debug_output($stdout) + case http_method + when 'get' + request = Net::HTTP::Get.new(safe_url(uri.path, uri.query, payload)) + else + request = Net::HTTP::Post.new(uri.request_uri) + request['Content-Type'] = 'application/json' + request.body = payload.to_json + end + http.request(request) + rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Net::ReadTimeout, Net::OpenTimeout => e + OpenStruct.new(code: 0, body: e.message) + end + end + end +end From 37350c99ccf76f8d01d1e1d01dbfed4c6b13218d Mon Sep 17 00:00:00 2001 From: Mhd Sami Almouhtaseb Date: Mon, 10 Feb 2020 18:40:50 +0800 Subject: [PATCH 3/4] update readme, config --- .env.development | 2 ++ README.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/.env.development b/.env.development index e37687e..2939aaa 100644 --- a/.env.development +++ b/.env.development @@ -1,5 +1,7 @@ NOTIFICATION_TITLE= +NOTIFICATION_URL= + REDIS_URL= SLACK_WEBHOOK= diff --git a/README.md b/README.md index 725df81..ff9a937 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,8 @@ In order of precedence RTPush uses: ``` NOTIFICATION_TITLE=XX +NOTIFICATION_URL=XX + REDIS_URL=XX SLACK_WEBHOOK=XX From 163d49b78a3b4e5f069f7d450177cbe137cfaa93 Mon Sep 17 00:00:00 2001 From: Mhd Sami Almouhtaseb Date: Mon, 10 Feb 2020 18:41:10 +0800 Subject: [PATCH 4/4] add .editorconfig --- .editorconfig | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..bdef6d3 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,39 @@ +# This file is for unifying the coding style for different editors and IDEs +# editorconfig.org + +root = true + +[*] +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.{js,rb,less,yml,yaml,jsx,rake}] +indent_style = space +indent_size = 2 + +[{package.json,.travis.yml}] +indent_style = space +indent_size = 2 + +[*.{css,html}] +indent_style = tab +indent_size = 2 + +[*.py] +indent_style = space +indent_size = 4 + +[Makefile] +indent_style = tab + +[*.md] +trim_trailing_whitespace = false + +[*.{sh,markdown}] +indent_size = 4 + +[*.go] +indent_style = tab +indent_size = 4