From 3ee902b8cfb214439b1c317d86325c153f4486ba Mon Sep 17 00:00:00 2001 From: magynhard Date: Mon, 21 Aug 2023 14:01:31 +0200 Subject: [PATCH] add support to configure and use proxy --- README.md | 6 ++++++ lib/fcmpush/client.rb | 11 +++++++++++ lib/fcmpush/configuration.rb | 2 +- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ea5554a..67b9ce1 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,12 @@ Fcmpush.configure do |config| config.server_key = 'your firebase server key' # Or set environment variables # ENV['FCM_SERVER_KEY'] = 'your firebase server key' + + # Proxy ENV variables are considered by default if set by net/http, but you can explicitly define your proxy host here + # user and password are optional + # config.proxy = { uri: "http://proxy.host:3128", user: nil, password: nil } + # explicitly disable using proxy, even ignore environment variables if set + # config.proxy = false end ``` diff --git a/lib/fcmpush/client.rb b/lib/fcmpush/client.rb index 91abbc0..59ca0fc 100644 --- a/lib/fcmpush/client.rb +++ b/lib/fcmpush/client.rb @@ -25,6 +25,17 @@ def initialize(domain, project_id, configuration, **options) @access_token_expiry = Time.now.utc + access_token_response['expires_in'] @server_key = configuration.server_key @connection = Net::HTTP::Persistent.new + if configuration.proxy && configuration.proxy[:uri] + uri = URI(configuration.proxy[:uri]) + # user name must not be a empty string, password can + if configuration.proxy[:user] && configuration.proxy[:user].strip != '' + uri.user = configuration.proxy[:user] + uri.password = configuration.proxy[:password] if configuration.proxy[:password] + end + @connection.proxy = uri + elsif configuration.proxy != false + @connection.proxy = :ENV + end end def v1_authorize diff --git a/lib/fcmpush/configuration.rb b/lib/fcmpush/configuration.rb index 81237fd..26f2652 100644 --- a/lib/fcmpush/configuration.rb +++ b/lib/fcmpush/configuration.rb @@ -1,6 +1,6 @@ module Fcmpush class Configuration - attr_accessor :scope, :json_key_io, :server_key + attr_accessor :scope, :json_key_io, :server_key, :proxy def initialize @scope = ['https://www.googleapis.com/auth/firebase.messaging']