Created
August 21, 2017 20:17
-
-
Save lagartoflojo/32612c8e602d5af3346a50ab64e42862 to your computer and use it in GitHub Desktop.
Dependency Injection example in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'resolv' | |
class ConferenceDomainsService | |
def initialize(conference, resolver = Resolv::DNS.new) | |
@conference = conference | |
@resolver = resolver | |
end | |
## | |
# Checks if domain correctly points to the hosted version | |
# This feature is enabled only if ENV['OSEM_HOSTNAME'] is present | |
# | |
# ====Returns | |
# * +true+ -> If the custom domain has a CNAME record for the hosted version | |
# * +false+ -> If the custom domain does not have a CNAME record for the hosted version | |
def check_custom_domain | |
cname_record = @resolver.getresource(@conference.custom_domain, Resolv::DNS::Resource::IN::CNAME) | |
cname_record.present? ? ENV['OSEM_HOSTNAME'] == cname_record.name.to_s : false | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe ConferenceDomainsService do | |
let(:conference) { create(:conference, custom_domain: 'demo.osem.io') } | |
let(:resolver) { double('resolver') } | |
let(:service) { ConferenceDomainsService.new(conference, resolver) } | |
describe '#check_custom_domain' do | |
subject { service.check_custom_domain } | |
let(:cname_record) { double(name: cname_domain) } | |
before do | |
ENV['OSEM_HOSTNAME'] = 'osem-demo.herokuapp.com' | |
allow(resolver).to receive(:getresources).with(conference.custom_domain, Resolv::DNS::Resource::IN::CNAME) | |
.and_return(cname_record) | |
end | |
context 'when the cname record matches the domain name' do | |
let(:cname_domain) { conference.custom_domain } | |
it { is_expected.to eq true } | |
end | |
context 'when the cname record does not match the domain name' do | |
let(:cname_domain) { 'random.domain.com' } | |
it { is_expected.to eq false } | |
end | |
context 'when there is not cname record present' do | |
let(:cname_record) { nil } | |
it { is_expected.to eq false } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment