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 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 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/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/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 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