From d650b18668e65971955ad05ccf2cdda7481927d7 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Fri, 10 Jun 2016 16:38:19 +0300 Subject: [PATCH] Raise error if API key is not set --- lib/packlink_lite/client.rb | 6 +++++- lib/packlink_lite/version.rb | 2 +- spec/client_spec.rb | 10 ++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/packlink_lite/client.rb b/lib/packlink_lite/client.rb index b566958..fb784d1 100644 --- a/lib/packlink_lite/client.rb +++ b/lib/packlink_lite/client.rb @@ -34,8 +34,12 @@ def with_error_handling end def connection(api_key) + token = api_key || PacklinkLite.config.api_key + + raise(Error, 'API key is not set') unless token + @connection ||= build_connection - @connection.headers['Authorization'] = api_key || PacklinkLite.config.api_key + @connection.headers['Authorization'] = token @connection end diff --git a/lib/packlink_lite/version.rb b/lib/packlink_lite/version.rb index 7d514e6..1a09e99 100644 --- a/lib/packlink_lite/version.rb +++ b/lib/packlink_lite/version.rb @@ -1,3 +1,3 @@ module PacklinkLite - VERSION = '0.2.0' + VERSION = '0.2.1' end diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 98fcd52..acba081 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -1,4 +1,14 @@ describe PacklinkLite::Client do + context 'when API key is not set' do + before do + PacklinkLite.config.api_key = nil + end + + it 'raises error' do + expect { subject.get('services') }.to raise_error(PacklinkLite::Error, /API key/) + end + end + context 'when API key is not passed' do it 'uses api key from configuration' do stub_request(:get, 'https://apisandbox.packlink.com/v1/services')