diff --git a/lib/apilayer/mailbox.rb b/lib/apilayer/mailbox.rb index fed0731..4b295a3 100644 --- a/lib/apilayer/mailbox.rb +++ b/lib/apilayer/mailbox.rb @@ -4,13 +4,14 @@ module Apilayer module Mailbox extend ConnectionHelper - INVALID_OPTIONS_MSG = 'You have provided an invalid option. Only :smtp is allowed' + DEFAULT_OPTIONS = { smtp: 1, catch_all: 0 } + INVALID_OPTIONS_MSG = "You have provided an invalid option. Only :smtp and :catch_all are allowed" ## Validations # def self.validate_options(options) options.keys.each do |key| - unless [:smtp].include? key + unless DEFAULT_OPTIONS.keys.include? key raise Apilayer::Error.new(INVALID_OPTIONS_MSG) end end @@ -22,19 +23,16 @@ def self.validate_options(options) # Examples: # Apilayer::Mailbox.check('support@apilayer.com') # Apilayer::Mailbox.check('support@apilayer.com', {smtp: false}) # If you don't want to perform smtp checks + # Apilayer::Mailbox.check('support@apilayer.com', {catch_all: true}) # If you want to perform catch_all detection def self.check(email, opts={}) validate_options(opts) - opts = assign_smtp_value(opts) + opts = assign_options_values(opts) params = { email: email }.merge(opts) get_and_parse("check", params) end - def self.assign_smtp_value(opts) - if opts[:smtp] == false - { smtp: 0 } - else - { smtp: 1 } - end + def self.assign_options_values(opts) + DEFAULT_OPTIONS.merge Hash[opts.map{ |k,v| [k, (v ? 1 : 0)] }] end end end diff --git a/spec/fixtures/vcr_cassettes/mailbox/check_without_options.yml b/spec/fixtures/vcr_cassettes/mailbox/check_without_options.yml index dfea5c5..df82eb5 100644 --- a/spec/fixtures/vcr_cassettes/mailbox/check_without_options.yml +++ b/spec/fixtures/vcr_cassettes/mailbox/check_without_options.yml @@ -2,7 +2,7 @@ http_interactions: - request: method: get - uri: http://apilayer.net/api/check?access_key=foobar123&email=support@apilayer.com&smtp=1 + uri: http://apilayer.net/api/check?access_key=foobar123&catch_all=0&email=support@apilayer.com&smtp=1 body: encoding: US-ASCII string: '' @@ -33,6 +33,6 @@ http_interactions: body: encoding: UTF-8 string: '{"email":"support@apilayer.com","did_you_mean":"","user":"support","domain":"apilayer.com","format_valid":true,"mx_found":true,"smtp_check":false,"catch_all":null,"role":true,"disposable":false,"free":false,"score":0.64}' - http_version: + http_version: recorded_at: Wed, 26 Oct 2016 21:00:45 GMT recorded_with: VCR 3.0.3 diff --git a/spec/mailbox_spec.rb b/spec/mailbox_spec.rb index f39c281..70d81bf 100644 --- a/spec/mailbox_spec.rb +++ b/spec/mailbox_spec.rb @@ -49,5 +49,25 @@ subject end end + + context 'with catch_all: false' do + subject { Apilayer::Mailbox.check(email, catch_all: false) } + specify do + expect(Apilayer::Mailbox).to receive(:get_and_parse).with( + 'check', hash_including( catch_all: 0, email: email) + ) + subject + end + end + + context 'with catch_all: true' do + subject { Apilayer::Mailbox.check(email, catch_all: true) } + specify do + expect(Apilayer::Mailbox).to receive(:get_and_parse).with( + 'check', hash_including( catch_all: 1, email: email) + ) + subject + end + end end end