From 2bdf732a81cbbbc6fc0f89eb3497bdfd0494c3b0 Mon Sep 17 00:00:00 2001 From: Grey Moore Date: Mon, 16 Oct 2023 10:36:30 -0400 Subject: [PATCH] Allow overriding the default hostname (#5) * allow specifying a host to EngagingNetworksRest::Client * allow specifying a host to EngagingNetworksRest.new * update readme --- README.md | 2 +- lib/engaging_networks_rest.rb | 4 ++-- lib/engaging_networks_rest/client.rb | 4 ++-- spec/client_spec.rb | 24 ++++++++++++++++++++---- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 374c76c..500302b 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Client gem for the ENS API to Engaging Networks ## Usage ``` -client = EngagingNetworksRest.new(api_key: 'YOUR API KEY HERE') +client = EngagingNetworksRest.new(api_key: 'YOUR API KEY HERE', host: 'us.engagingnetworks.app') # get pages by type pages = client.pages(type: 'dcf', status: 'live') diff --git a/lib/engaging_networks_rest.rb b/lib/engaging_networks_rest.rb index 5887b1b..cf8d176 100644 --- a/lib/engaging_networks_rest.rb +++ b/lib/engaging_networks_rest.rb @@ -4,8 +4,8 @@ module EngagingNetworksRest class << self - def new(api_key:) - EngagingNetworksRest::Client.new(api_key: api_key) + def new(api_key:, host: EngagingNetworksRest::Client::ENS_DOMAIN) + EngagingNetworksRest::Client.new(api_key: api_key, host: host) end end end diff --git a/lib/engaging_networks_rest/client.rb b/lib/engaging_networks_rest/client.rb index 67a13fb..0b797e6 100644 --- a/lib/engaging_networks_rest/client.rb +++ b/lib/engaging_networks_rest/client.rb @@ -11,10 +11,10 @@ class Client ENS_DOMAIN = 'www.e-activist.com' - def initialize(api_key:) + def initialize(api_key:, host: ENS_DOMAIN) @api_key = api_key - @connection = Faraday.new(url: "https://#{ENS_DOMAIN}") do |conn| + @connection = Faraday.new(url: "https://#{host}") do |conn| conn.request :json conn.response :json, content_type: /\bjson$/ diff --git a/spec/client_spec.rb b/spec/client_spec.rb index f93e6bb..90a6918 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -9,7 +9,6 @@ subject { EngagingNetworksRest::Client.new(api_key: api_key) } describe '#authenticate!' do - let(:auth_url) { "https://#{EngagingNetworksRest::Client::ENS_DOMAIN}/ens/service/authenticate" } let(:auth_key) { '75491e42-99dc-45ce-b637-a681bede875c' } let(:auth_key_body) { "{\"ens-auth-token\":\"#{auth_key}\",\"expires\":3600000}" } @@ -19,10 +18,27 @@ .to_return(body: auth_key_body, headers: content_type_header) end - it 'should set the ens_auth_key on the client' do - subject.authenticate! + context 'with no host specified' do + let(:auth_url) { "https://#{EngagingNetworksRest::Client::ENS_DOMAIN}/ens/service/authenticate" } - expect(subject.ens_auth_key).to eq auth_key + it 'should set the ens_auth_key on the client' do + subject.authenticate! + + expect(subject.ens_auth_key).to eq auth_key + end + end + + context 'with a host specified' do + let(:host) { 'example.com' } + let(:auth_url) { "https://#{host}/ens/service/authenticate" } + + subject { EngagingNetworksRest::Client.new(api_key: api_key, host: host) } + + it 'should set the ens_auth_key on the client' do + subject.authenticate! + + expect(subject.ens_auth_key).to eq auth_key + end end end end