Skip to content

Commit

Permalink
Allow overriding the default hostname (#5)
Browse files Browse the repository at this point in the history
* allow specifying a host to EngagingNetworksRest::Client

* allow specifying a host to EngagingNetworksRest.new

* update readme
  • Loading branch information
lavaturtle authored Oct 16, 2023
1 parent a1988b6 commit 2bdf732
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions lib/engaging_networks_rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/engaging_networks_rest/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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$/

Expand Down
24 changes: 20 additions & 4 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}" }

Expand All @@ -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

0 comments on commit 2bdf732

Please sign in to comment.