diff --git a/README.markdown b/README.markdown index bcc01ea..a86b2e7 100644 --- a/README.markdown +++ b/README.markdown @@ -33,6 +33,8 @@ Vero::App.init do |config| config.api_key = "Your Development API key goes here" config.secret = "Your Development API secret goes here" end + + config.http_timeout = 30 # default timeout per API request is set to 60 (seconds) end ``` diff --git a/lib/vero/api/base_api.rb b/lib/vero/api/base_api.rb index d716b6d..d55c1b6 100644 --- a/lib/vero/api/base_api.rb +++ b/lib/vero/api/base_api.rb @@ -7,12 +7,14 @@ module Vero module Api module Workers class BaseAPI + DEFAULT_API_TIMEOUT = 60 + ALLOWED_HTTP_METHODS = %i[post put].freeze + attr_accessor :domain attr_reader :options def self.perform(domain, options) - caller = new(domain, options) - caller.perform + new(domain, options).perform end def initialize(domain, options) @@ -27,7 +29,13 @@ def perform end def options=(val) - @options = options_with_symbolized_keys(val) + new_options = options_with_symbolized_keys(val) + + if (extra_config = new_options.delete(:_config)) && extra_config.is_a?(Hash) + @http_timeout = extra_config[:http_timeout] + end + + @options = new_options end protected @@ -42,20 +50,51 @@ def proxy.<<(message) end end - def url; end + def url + "#{@domain}/api/v2/#{api_url}" + end + + def http_method + raise NotImplementedError + end + + def api_url + raise NotImplementedError + end def validate! raise "#{self.class.name}#validate! should be overridden" end - def request; end + def request + do_request(http_method, url, @options) + end + + def do_request(method, a_url, params) + raise ArgumentError, ":method must be one of the follow: #{ALLOWED_HTTP_METHODS.join(', ')}" unless ALLOWED_HTTP_METHODS.include?(method) + + rest_client_args = { + method: method, + url: a_url, + timeout: http_timeout + } + + if method == :get + rest_client_args.merge!(headers: { params: params }) + else + rest_client_args.merge!(payload: JSON.dump(params), headers: { content_type: :json, accept: :json }) + end - def request_content_type - { content_type: :json, accept: :json } + RestClient::Request.execute( + method: method, + url: a_url, + headers: { params: params }, + timeout: http_timeout + ) end - def request_params_as_json - JSON.dump(@options) + def http_timeout + @http_timeout || DEFAULT_API_TIMEOUT end def options_with_symbolized_keys(val) diff --git a/lib/vero/api/events/track_api.rb b/lib/vero/api/events/track_api.rb index 9c54911..eeca13a 100644 --- a/lib/vero/api/events/track_api.rb +++ b/lib/vero/api/events/track_api.rb @@ -5,12 +5,12 @@ module Api module Workers module Events class TrackAPI < BaseAPI - def url - "#{@domain}/api/v2/events/track.json" + def api_url + 'events/track.json' end - def request - RestClient.post(url, request_params_as_json, request_content_type) + def http_method + :post end def validate! diff --git a/lib/vero/api/users/edit_api.rb b/lib/vero/api/users/edit_api.rb index c0426fe..c89cc31 100644 --- a/lib/vero/api/users/edit_api.rb +++ b/lib/vero/api/users/edit_api.rb @@ -5,12 +5,12 @@ module Api module Workers module Users class EditAPI < BaseAPI - def url - "#{@domain}/api/v2/users/edit.json" + def api_url + 'users/edit.json' end - def request - RestClient.put(url, request_params_as_json, request_content_type) + def http_method + :put end def validate! diff --git a/lib/vero/api/users/edit_tags_api.rb b/lib/vero/api/users/edit_tags_api.rb index bce1994..9a66ec4 100644 --- a/lib/vero/api/users/edit_tags_api.rb +++ b/lib/vero/api/users/edit_tags_api.rb @@ -5,12 +5,12 @@ module Api module Workers module Users class EditTagsAPI < BaseAPI - def url - "#{@domain}/api/v2/users/tags/edit.json" + def api_url + 'users/tags/edit.json' end - def request - RestClient.put(url, request_params_as_json, request_content_type) + def http_method + :put end def validate! diff --git a/lib/vero/api/users/reidentify_api.rb b/lib/vero/api/users/reidentify_api.rb index cb22397..652302b 100644 --- a/lib/vero/api/users/reidentify_api.rb +++ b/lib/vero/api/users/reidentify_api.rb @@ -5,12 +5,12 @@ module Api module Workers module Users class ReidentifyAPI < BaseAPI - def url - "#{@domain}/api/v2/users/reidentify.json" + def api_url + 'users/reidentify.json' end - def request - RestClient.put(url, request_params_as_json, request_content_type) + def http_method + :put end def validate! diff --git a/lib/vero/api/users/resubscribe_api.rb b/lib/vero/api/users/resubscribe_api.rb index 9ac6ba8..85a0f5b 100644 --- a/lib/vero/api/users/resubscribe_api.rb +++ b/lib/vero/api/users/resubscribe_api.rb @@ -5,12 +5,12 @@ module Api module Workers module Users class ResubscribeAPI < BaseAPI - def url - "#{@domain}/api/v2/users/resubscribe.json" + def api_url + 'users/resubscribe.json' end - def request - RestClient.post(url, @options) + def http_method + :post end def validate! diff --git a/lib/vero/api/users/track_api.rb b/lib/vero/api/users/track_api.rb index 41b1f10..5d381d6 100644 --- a/lib/vero/api/users/track_api.rb +++ b/lib/vero/api/users/track_api.rb @@ -5,12 +5,12 @@ module Api module Workers module Users class TrackAPI < BaseAPI - def url - "#{@domain}/api/v2/users/track.json" + def api_url + 'users/track.json' end - def request - RestClient.post(url, request_params_as_json, request_content_type) + def http_method + :post end def validate! diff --git a/lib/vero/api/users/unsubscribe_api.rb b/lib/vero/api/users/unsubscribe_api.rb index 7ed6b3d..0eb6df7 100644 --- a/lib/vero/api/users/unsubscribe_api.rb +++ b/lib/vero/api/users/unsubscribe_api.rb @@ -5,12 +5,12 @@ module Api module Workers module Users class UnsubscribeAPI < BaseAPI - def url - "#{@domain}/api/v2/users/unsubscribe.json" + def api_url + 'users/unsubscribe.json' end - def request - RestClient.post(url, @options) + def http_method + :post end def validate! diff --git a/lib/vero/config.rb b/lib/vero/config.rb index 219cb3b..1037915 100644 --- a/lib/vero/config.rb +++ b/lib/vero/config.rb @@ -5,10 +5,10 @@ module Vero class Config attr_writer :domain - attr_accessor :api_key, :secret, :development_mode, :async, :disabled, :logging + attr_accessor :api_key, :secret, :development_mode, :async, :disabled, :logging, :http_timeout def self.available_attributes - %i[api_key secret development_mode async disabled logging domain] + %i[api_key secret development_mode async disabled logging domain http_timeout] end def initialize @@ -22,7 +22,8 @@ def config_params def request_params { auth_token: auth_token, - development_mode: development_mode + development_mode: development_mode, + _config: { http_timeout: http_timeout } }.compact end @@ -53,12 +54,13 @@ def disable_requests! end def reset! - self.disabled = false + self.disabled = false self.development_mode = false - self.async = true - self.logging = false - self.api_key = nil - self.secret = nil + self.async = true + self.logging = false + self.api_key = nil + self.secret = nil + self.http_timeout = nil end def update_attributes(attributes = {}) diff --git a/spec/lib/api/events/track_api_spec.rb b/spec/lib/api/events/track_api_spec.rb index 7be3166..458bc20 100644 --- a/spec/lib/api/events/track_api_spec.rb +++ b/spec/lib/api/events/track_api_spec.rb @@ -3,7 +3,15 @@ require 'spec_helper' describe Vero::Api::Workers::Events::TrackAPI do - subject { Vero::Api::Workers::Events::TrackAPI.new('https://api.getvero.com', { auth_token: 'abcd', identity: { email: 'test@test.com' }, event_name: 'test_event' }) } + let(:payload) do + { + auth_token: 'abcd', + identity: { email: 'test@test.com' }, + event_name: 'test_event' + } + end + + subject { Vero::Api::Workers::Events::TrackAPI.new('https://api.getvero.com', payload) } it_behaves_like 'a Vero wrapper' do let(:end_point) { '/api/v2/events/track.json' } @@ -16,22 +24,21 @@ subject.options = options expect { subject.send(:validate!) }.to raise_error(ArgumentError) - options = { auth_token: 'abcd', identity: { email: 'test@test.com' }, event_name: 'test_event' } - subject.options = options + subject.options = payload expect { subject.send(:validate!) }.to_not raise_error end it 'should raise an error if data is not either nil or a Hash' do - options = { auth_token: 'abcd', identity: { email: 'test@test.com' }, event_name: 'test_event', data: [] } - subject.options = options + payload[:data] = [] + subject.options = payload expect { subject.send(:validate!) }.to raise_error(ArgumentError) - options = { auth_token: 'abcd', identity: { email: 'test@test.com' }, event_name: 'test_event', data: nil } - subject.options = options + payload[:data] = nil + subject.options = payload expect { subject.send(:validate!) }.to_not raise_error - options = { auth_token: 'abcd', identity: { email: 'test@test.com' }, event_name: 'test_event', data: {} } - subject.options = options + payload[:data] = {} + subject.options = payload expect { subject.send(:validate!) }.to_not raise_error end @@ -49,18 +56,40 @@ describe :request do it 'should send a JSON request to the Vero API' do - expect(RestClient).to receive(:post).with('https://api.getvero.com/api/v2/events/track.json', { auth_token: 'abcd', identity: { email: 'test@test.com' }, event_name: 'test_event' }.to_json, { content_type: :json, accept: :json }) - allow(RestClient).to receive(:post).and_return(200) + expect(RestClient::Request).to( + receive(:execute).with( + method: :post, + url: 'https://api.getvero.com/api/v2/events/track.json', + payload: { auth_token: 'abcd', identity: { email: 'test@test.com' }, event_name: 'test_event' }.to_json, + headers: { content_type: :json, accept: :json }, + timeout: 60 + ) + ) + allow(RestClient::Request).to receive(:execute).and_return(200) subject.send(:request) end + + it 'should allow configurable timeout' do + expect(RestClient::Request).to( + receive(:execute).with( + method: :post, + url: 'https://api.getvero.com/api/v2/events/track.json', + payload: { auth_token: 'abcd', identity: { email: 'test@test.com' }, event_name: 'test_event' }.to_json, + headers: { content_type: :json, accept: :json }, + timeout: 30 + ) + ) + allow(RestClient::Request).to receive(:execute).and_return(200) + Vero::Api::Workers::Events::TrackAPI.new('https://api.getvero.com', payload.merge(_config: { http_timeout: 30 })).send(:request) + end end end describe 'integration test' do it 'should not raise any errors' do - obj = Vero::Api::Workers::Events::TrackAPI.new('https://api.getvero.com', { auth_token: 'abcd', identity: { email: 'test@test.com' }, event_name: 'test_event' }) + obj = Vero::Api::Workers::Events::TrackAPI.new('https://api.getvero.com', payload) - allow(RestClient).to receive(:post).and_return(200) + allow(RestClient::Request).to receive(:execute).and_return(200) expect { obj.perform }.to_not raise_error end end diff --git a/spec/lib/api/users/edit_api_spec.rb b/spec/lib/api/users/edit_api_spec.rb index 10419ce..d688d07 100644 --- a/spec/lib/api/users/edit_api_spec.rb +++ b/spec/lib/api/users/edit_api_spec.rb @@ -3,7 +3,15 @@ require 'spec_helper' describe Vero::Api::Workers::Users::EditAPI do - subject { Vero::Api::Workers::Users::EditAPI.new('https://api.getvero.com', { auth_token: 'abcd', email: 'test@test.com', changes: { email: 'test@test.com' } }) } + let(:payload) do + { + auth_token: 'abcd', + email: 'test@test.com', + changes: { email: 'test@test.com' } + } + end + + subject { Vero::Api::Workers::Users::EditAPI.new('https://api.getvero.com', payload) } it_behaves_like 'a Vero wrapper' do let(:end_point) { '/api/v2/users/edit.json' } @@ -19,15 +27,23 @@ describe :request do it 'should send a request to the Vero API' do - expect(RestClient).to receive(:put).with('https://api.getvero.com/api/v2/users/edit.json', { auth_token: 'abcd', email: 'test@test.com', changes: { email: 'test@test.com' } }.to_json, { content_type: :json, accept: :json }) - allow(RestClient).to receive(:put).and_return(200) + expect(RestClient::Request).to( + receive(:execute).with( + method: :put, + url: 'https://api.getvero.com/api/v2/users/edit.json', + payload: { auth_token: 'abcd', email: 'test@test.com', changes: { email: 'test@test.com' } }.to_json, + headers: { content_type: :json, accept: :json }, + timeout: 60 + ) + ) + allow(RestClient::Request).to receive(:execute).and_return(200) subject.send(:request) end end describe 'integration test' do it 'should not raise any errors' do - allow(RestClient).to receive(:put).and_return(200) + allow(RestClient::Request).to receive(:execute).and_return(200) expect { subject.perform }.to_not raise_error end end diff --git a/spec/lib/api/users/edit_tags_api_spec.rb b/spec/lib/api/users/edit_tags_api_spec.rb index 8d327dd..a943200 100644 --- a/spec/lib/api/users/edit_tags_api_spec.rb +++ b/spec/lib/api/users/edit_tags_api_spec.rb @@ -3,7 +3,15 @@ require 'spec_helper' describe Vero::Api::Workers::Users::EditTagsAPI do - subject { Vero::Api::Workers::Users::EditTagsAPI.new('https://api.getvero.com', { auth_token: 'abcd', email: 'test@test.com', add: ['test'] }) } + let(:payload) do + { + auth_token: 'abcd', + id: 'test@test.com', + add: ['test'] + } + end + + subject { Vero::Api::Workers::Users::EditTagsAPI.new('https://api.getvero.com', payload) } it_behaves_like 'a Vero wrapper' do let(:end_point) { '/api/v2/users/tags/edit.json' } @@ -11,53 +19,52 @@ describe :validate! do it 'should raise an error if email is a blank String' do - options = { auth_token: 'abcd', identity: { email: 'test@test.com' }, email: nil, add: [] } - subject.options = options + subject.options = payload.merge(id: nil, add: []) expect { subject.send(:validate!) }.to raise_error(ArgumentError) - options = { auth_token: 'abcd', identity: { email: 'test@test.com' }, email: 'test@test.com', add: [] } - subject.options = options + subject.options = payload.merge(add: []) expect { subject.send(:validate!) }.to_not raise_error end it 'should raise an error if add is not an Array or missing' do - options = { auth_token: 'abcd', identity: { email: 'test@test.com' }, email: 'test@test.com', add: 'foo' } - - subject.options = options + subject.options = payload.merge(add: 'foo') expect { subject.send(:validate!) }.to raise_error(ArgumentError) end it 'should raise an error if remove is not an Array or missing' do - options = { auth_token: 'abcd', identity: { email: 'test@test.com' }, email: 'test@test.com', remove: 'foo' } - - subject.options = options + subject.options = payload.merge(remove: 'foo') expect { subject.send(:validate!) }.to raise_error(ArgumentError) end it 'should raise an error if botha add and remove are missing' do - options = { auth_token: 'abcd', identity: { email: 'test@test.com' }, email: 'test@test.com' } - - subject.options = options + payload.delete(:add) + subject.options = payload expect { subject.send(:validate!) }.to raise_error(ArgumentError) end it 'should not raise an error if the correct arguments are passed' do - options = { auth_token: 'abcd', identity: { email: 'test@test.com' }, email: 'test@test.com', remove: ['Hi'] } - - subject.options = options + payload.delete(:add) + subject.options = payload.merge(remove: ['Hi']) expect { subject.send(:validate!) }.to_not raise_error end it 'should not raise an error when the keys are Strings' do - options = { 'auth_token' => 'abcd', 'identity' => { 'email' => 'test@test.com' }, 'email' => 'test@test.com', 'remove' => ['Hi'] } - subject.options = options + subject.options = { 'auth_token' => 'abcd', 'id' => 'test@test.com', 'remove' => ['Hi'] } expect { subject.send(:validate!) }.to_not raise_error end end describe :request do it 'should send a request to the Vero API' do - expect(RestClient).to receive(:put).with('https://api.getvero.com/api/v2/users/tags/edit.json', { auth_token: 'abcd', email: 'test@test.com', add: ['test'] }.to_json, { content_type: :json, accept: :json }) + expect(RestClient::Request).to( + receive(:execute).with( + method: :put, + url: 'https://api.getvero.com/api/v2/users/tags/edit.json', + payload: { auth_token: 'abcd', id: 'test@test.com', add: ['test'] }.to_json, + headers: { content_type: :json, accept: :json }, + timeout: 60 + ) + ) allow(RestClient).to receive(:put).and_return(200) subject.send(:request) end @@ -65,7 +72,7 @@ describe 'integration test' do it 'should not raise any errors' do - allow(RestClient).to receive(:put).and_return(200) + allow(RestClient::Request).to receive(:execute).and_return(200) expect { subject.perform }.to_not raise_error end end diff --git a/spec/lib/api/users/reidentify_spec.rb b/spec/lib/api/users/reidentify_spec.rb index 0625591..f58ecc0 100644 --- a/spec/lib/api/users/reidentify_spec.rb +++ b/spec/lib/api/users/reidentify_spec.rb @@ -3,7 +3,15 @@ require 'spec_helper' describe Vero::Api::Workers::Users::ReidentifyAPI do - subject { Vero::Api::Workers::Users::ReidentifyAPI.new('https://api.getvero.com', { auth_token: 'abcd', id: 'test@test.com', new_id: 'test2@test.com' }) } + let(:payload) do + { + auth_token: 'abcd', + id: 'test@test.com', + new_id: 'test2@test.com' + } + end + + subject { Vero::Api::Workers::Users::ReidentifyAPI.new('https://api.getvero.com', payload) } it_behaves_like 'a Vero wrapper' do let(:end_point) { '/api/v2/users/reidentify.json' } @@ -17,27 +25,37 @@ end it 'should raise an error if id is missing' do - subject.options = { auth_token: 'abcd', new_id: 'test2@test.com' } + payload.delete(:id) + subject.options = payload expect { subject.send(:validate!) }.to raise_error(ArgumentError) end it 'should raise an error if new_id is missing' do - subject.options = { auth_token: 'abcd', id: 'test@test.com' } + payload.delete(:new_id) + subject.options = payload expect { subject.send(:validate!) }.to raise_error(ArgumentError) end end describe :request do it 'should send a request to the Vero API' do - expect(RestClient).to receive(:put).with('https://api.getvero.com/api/v2/users/reidentify.json', { auth_token: 'abcd', id: 'test@test.com', new_id: 'test2@test.com' }.to_json, { content_type: :json, accept: :json }) - allow(RestClient).to receive(:put).and_return(200) + expect(RestClient::Request).to( + receive(:execute).with( + method: :put, + url: 'https://api.getvero.com/api/v2/users/reidentify.json', + payload: { auth_token: 'abcd', id: 'test@test.com', new_id: 'test2@test.com' }.to_json, + headers: { content_type: :json, accept: :json }, + timeout: 60 + ) + ) + allow(RestClient::Request).to receive(:execute).and_return(200) subject.send(:request) end end describe 'integration test' do it 'should not raise any errors' do - allow(RestClient).to receive(:put).and_return(200) + allow(RestClient::Request).to receive(:execute).and_return(200) expect { subject.perform }.to_not raise_error end end diff --git a/spec/lib/api/users/resubscribe_api_spec.rb b/spec/lib/api/users/resubscribe_api_spec.rb index 6b77da0..808edc8 100644 --- a/spec/lib/api/users/resubscribe_api_spec.rb +++ b/spec/lib/api/users/resubscribe_api_spec.rb @@ -3,7 +3,11 @@ require 'spec_helper' describe Vero::Api::Workers::Users::ResubscribeAPI do - subject { Vero::Api::Workers::Users::ResubscribeAPI.new('https://api.getvero.com', { auth_token: 'abcd', id: '1234' }) } + let(:payload) do + { auth_token: 'abcd', id: '1234' } + end + + subject { Vero::Api::Workers::Users::ResubscribeAPI.new('https://api.getvero.com', payload) } it_behaves_like 'a Vero wrapper' do let(:end_point) { '/api/v2/users/resubscribe.json' } @@ -23,12 +27,16 @@ describe :request do it 'should send a request to the Vero API' do - expect(RestClient).to( - receive(:post) - .with('https://api.getvero.com/api/v2/users/resubscribe.json', { auth_token: 'abcd', id: '1234' }) + expect(RestClient::Request).to( + receive(:execute).with( + method: :post, + url: 'https://api.getvero.com/api/v2/users/resubscribe.json', + payload: { auth_token: 'abcd', id: '1234' }.to_json, + headers: { content_type: :json, accept: :json }, + timeout: 60 + ) ) - allow(RestClient).to receive(:post).and_return(200) - + allow(RestClient::Request).to receive(:execute).and_return(200) subject.send(:request) end end diff --git a/spec/lib/api/users/track_api_spec.rb b/spec/lib/api/users/track_api_spec.rb index 81f49f8..5d7bc86 100644 --- a/spec/lib/api/users/track_api_spec.rb +++ b/spec/lib/api/users/track_api_spec.rb @@ -3,7 +3,15 @@ require 'spec_helper' describe Vero::Api::Workers::Users::TrackAPI do - subject { Vero::Api::Workers::Users::TrackAPI.new('https://api.getvero.com', { auth_token: 'abcd', identity: { email: 'test@test.com' }, email: 'test@test.com' }) } + let(:payload) do + { + auth_token: 'abcd', + identity: { email: 'test@test.com' }, + email: 'test@test.com' + } + end + + subject { Vero::Api::Workers::Users::TrackAPI.new('https://api.getvero.com', payload) } it_behaves_like 'a Vero wrapper' do let(:end_point) { '/api/v2/users/track.json' } @@ -11,34 +19,28 @@ describe :validate! do it 'should raise an error if email and id are are blank String' do - options = { auth_token: 'abcd', identity: { email: 'test@test.com' }, id: nil, email: nil } - subject.options = options - expect { subject.send(:validate!) }.to raise_error(ArgumentError) - - options = { auth_token: 'abcd', identity: { email: 'test@test.com' }, id: nil, email: 'test@test.com' } - subject.options = options + subject.options = payload expect { subject.send(:validate!) }.to_not raise_error - options = { auth_token: 'abcd', identity: { email: 'test@test.com' }, id: '', email: nil } - subject.options = options + payload.delete(:email) + subject.options = payload expect { subject.send(:validate!) }.to raise_error(ArgumentError) - options = { auth_token: 'abcd', identity: { email: 'test@test.com' }, id: 'user123', email: nil } - subject.options = options + subject.options = payload.merge(id: '') + expect { subject.send(:validate!) }.to raise_error(ArgumentError) + + subject.options = payload.merge(id: 'user123') expect { subject.send(:validate!) }.to_not raise_error end it 'should raise an error if data is not either nil or a Hash' do - options = { auth_token: 'abcd', identity: { email: 'test@test.com' }, email: 'test@test.com', data: [] } - subject.options = options + subject.options = payload.merge(data: []) expect { subject.send(:validate!) }.to raise_error(ArgumentError) - options = { auth_token: 'abcd', identity: { email: 'test@test.com' }, email: 'test@test.com', data: nil } - subject.options = options + subject.options = payload.merge(data: nil) expect { subject.send(:validate!) }.to_not raise_error - options = { auth_token: 'abcd', identity: { email: 'test@test.com' }, email: 'test@test.com', data: {} } - subject.options = options + subject.options = payload.merge(data: {}) expect { subject.send(:validate!) }.to_not raise_error end @@ -51,15 +53,23 @@ describe :request do it 'should send a request to the Vero API' do - expect(RestClient).to receive(:post).with('https://api.getvero.com/api/v2/users/track.json', { auth_token: 'abcd', identity: { email: 'test@test.com' }, email: 'test@test.com' }.to_json, { content_type: :json, accept: :json }) - allow(RestClient).to receive(:post).and_return(200) + expect(RestClient::Request).to( + receive(:execute).with( + method: :post, + url: 'https://api.getvero.com/api/v2/users/track.json', + payload: { auth_token: 'abcd', identity: { email: 'test@test.com' }, email: 'test@test.com' }.to_json, + headers: { content_type: :json, accept: :json }, + timeout: 60 + ) + ) + allow(RestClient::Request).to receive(:execute).and_return(200) subject.send(:request) end end describe 'integration test' do it 'should not raise any errors' do - allow(RestClient).to receive(:post).and_return(200) + allow(RestClient::Request).to receive(:execute).and_return(200) expect { subject.perform }.to_not raise_error end end diff --git a/spec/lib/api/users/unsubscribe_api_spec.rb b/spec/lib/api/users/unsubscribe_api_spec.rb index 86db882..f444f5c 100644 --- a/spec/lib/api/users/unsubscribe_api_spec.rb +++ b/spec/lib/api/users/unsubscribe_api_spec.rb @@ -3,7 +3,15 @@ require 'spec_helper' describe Vero::Api::Workers::Users::UnsubscribeAPI do - subject { Vero::Api::Workers::Users::UnsubscribeAPI.new('https://api.getvero.com', { auth_token: 'abcd', email: 'test@test.com', changes: { email: 'test@test.com' } }) } + let(:payload) do + { + auth_token: 'abcd', + email: 'test@test.com', + changes: { email: 'test@test.com' } + } + end + + subject { Vero::Api::Workers::Users::UnsubscribeAPI.new('https://api.getvero.com', payload) } it_behaves_like 'a Vero wrapper' do let(:end_point) { '/api/v2/users/unsubscribe.json' } @@ -19,15 +27,23 @@ describe :request do it 'should send a request to the Vero API' do - expect(RestClient).to receive(:post).with('https://api.getvero.com/api/v2/users/unsubscribe.json', { auth_token: 'abcd', email: 'test@test.com', changes: { email: 'test@test.com' } }) - allow(RestClient).to receive(:post).and_return(200) + expect(RestClient::Request).to( + receive(:execute).with( + method: :post, + url: 'https://api.getvero.com/api/v2/users/unsubscribe.json', + payload: { auth_token: 'abcd', email: 'test@test.com', changes: { email: 'test@test.com' } }.to_json, + headers: { content_type: :json, accept: :json }, + timeout: 60 + ) + ) + allow(RestClient::Request).to receive(:execute).and_return(200) subject.send(:request) end end describe 'integration test' do it 'should not raise any errors' do - allow(RestClient).to receive(:post).and_return(200) + allow(RestClient::Request).to receive(:execute).and_return(200) expect { subject.perform }.to_not raise_error end end diff --git a/spec/lib/api_spec.rb b/spec/lib/api_spec.rb index c07755d..d5c39cb 100644 --- a/spec/lib/api_spec.rb +++ b/spec/lib/api_spec.rb @@ -5,6 +5,21 @@ describe Vero::Api::Events do let(:subject) { Vero::Api::Events } + it 'should pass http_timeout to API requests' do + mock_context = Vero::Context.new + allow(mock_context.config).to receive(:configured?).and_return(true) + allow(mock_context.config).to receive(:auth_token).and_return('abc123') + allow(mock_context.config).to receive(:http_timeout).and_return(30) + allow(Vero::App).to receive(:default_context).and_return(mock_context) + + expect(Vero::Sender).to( + receive(:send).with( + Vero::Api::Workers::Events::TrackAPI, true, 'https://api.getvero.com', hash_including(_config: { http_timeout: 30 }) + ) + ) + subject.track!({}) + end + describe :track! do it 'should call the TrackAPI object via the configured sender' do input = { event_name: 'test_event', identity: { email: 'james@getvero.com' }, data: { test: 'test' } } @@ -17,7 +32,7 @@ allow(Vero::App).to receive(:default_context).and_return(mock_context) - expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Events::TrackAPI, true, 'https://api.getvero.com', expected) + expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Events::TrackAPI, true, 'https://api.getvero.com', hash_including(expected)) subject.track!(input) end @@ -41,7 +56,7 @@ let(:input) { { email: 'james@getvero.com', data: { age: 25 } } } specify do - expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::TrackAPI, true, 'https://api.getvero.com', expected) + expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::TrackAPI, true, 'https://api.getvero.com', hash_including(expected)) subject.track!(input) end end @@ -52,7 +67,7 @@ let(:input) { { email: 'james@getvero.com', changes: { age: 25 } } } specify do - expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::EditAPI, true, 'https://api.getvero.com', expected) + expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::EditAPI, true, 'https://api.getvero.com', hash_including(expected)) subject.edit_user!(input) end end @@ -63,7 +78,7 @@ let(:input) { { add: ['boom'], remove: ['tish'] } } specify do - expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::EditTagsAPI, true, 'https://api.getvero.com', expected) + expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::EditTagsAPI, true, 'https://api.getvero.com', hash_including(expected)) subject.edit_user_tags!(input) end end @@ -74,7 +89,7 @@ let(:input) { { email: 'james@getvero' } } specify do - expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::UnsubscribeAPI, true, 'https://api.getvero.com', expected) + expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::UnsubscribeAPI, true, 'https://api.getvero.com', hash_including(expected)) subject.unsubscribe!(input) end end @@ -87,7 +102,7 @@ specify do expect(Vero::Sender).to( receive(:send) - .with(Vero::Api::Workers::Users::ResubscribeAPI, true, 'https://api.getvero.com', expected) + .with(Vero::Api::Workers::Users::ResubscribeAPI, true, 'https://api.getvero.com', hash_including(expected)) ) subject.resubscribe!(input) end diff --git a/spec/lib/config_spec.rb b/spec/lib/config_spec.rb index 120b042..0c3dd10 100644 --- a/spec/lib/config_spec.rb +++ b/spec/lib/config_spec.rb @@ -47,14 +47,14 @@ config.api_key = nil config.secret = nil config.development_mode = nil - expect(config.request_params).to eq({}) + expect(config.request_params).to eq(_config: { http_timeout: nil }) config.api_key = 'abcd1234' config.secret = 'abcd1234' - expect(config.request_params).to eq({ auth_token: 'YWJjZDEyMzQ6YWJjZDEyMzQ=' }) + expect(config.request_params).to match(hash_including(auth_token: 'YWJjZDEyMzQ6YWJjZDEyMzQ=')) config.development_mode = true - expect(config.request_params).to eq({ auth_token: 'YWJjZDEyMzQ6YWJjZDEyMzQ=', development_mode: true }) + expect(config.request_params).to match(hash_including(auth_token: 'YWJjZDEyMzQ6YWJjZDEyMzQ=', development_mode: true)) end end diff --git a/spec/lib/trackable_spec.rb b/spec/lib/trackable_spec.rb index 0ba954c..43594dd 100644 --- a/spec/lib/trackable_spec.rb +++ b/spec/lib/trackable_spec.rb @@ -322,7 +322,7 @@ def vero_context(user, logging = true, async = false, disabled = true) allow(user).to receive(:with_vero_context).and_return(context) - allow(RestClient).to receive(:post).and_return(200) + allow(RestClient::Request).to receive(:execute).and_return(200) expect(user.vero_track(request_params[:event_name], request_params[:data])).to eq(200) end end