From eb12f39d343a4c317cd0f8a83eff90b4660920e7 Mon Sep 17 00:00:00 2001 From: Joshua Azemoh Date: Fri, 5 Jan 2024 19:24:50 -0500 Subject: [PATCH] Update configure method to accept headers parameter. Update gem version to 0.4.0 --- lib/fera/api.rb | 3 ++- lib/fera/api/version.rb | 2 +- spec/fera/api_spec.rb | 25 ++++++++++++++++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/fera/api.rb b/lib/fera/api.rb index 146f7c6..55486c3 100644 --- a/lib/fera/api.rb +++ b/lib/fera/api.rb @@ -19,7 +19,7 @@ class Error < StandardError; end ## # @param api_key [String] Public API key, Secret API key or Auth Token (if app) # @return [Object, ::Fera::API] Result of the block operation if given, otherwise self - def self.configure(api_key, api_url: nil, strict_mode: false, debug_mode: false, api_type: nil) + def self.configure(api_key, api_url: nil, strict_mode: false, debug_mode: false, api_type: nil, headers: {}) previous_base_site = Base.site.dup previous_base_headers = Base.headers.dup previous_debug_mode = @debug_mode @@ -30,6 +30,7 @@ def self.configure(api_key, api_url: nil, strict_mode: false, debug_mode: false, @debug_mode = debug_mode + headers.to_a.each { |k, v| Base.headers[k] = v if v.present? } Base.api_key = api_key Base.headers['Strict-Mode'] = strict_mode if strict_mode diff --git a/lib/fera/api/version.rb b/lib/fera/api/version.rb index cf51f51..e012fa5 100755 --- a/lib/fera/api/version.rb +++ b/lib/fera/api/version.rb @@ -2,6 +2,6 @@ module Fera module API - VERSION = "0.3.4" + VERSION = "0.4.0" end end diff --git a/spec/fera/api_spec.rb b/spec/fera/api_spec.rb index d456a5c..878a97a 100644 --- a/spec/fera/api_spec.rb +++ b/spec/fera/api_spec.rb @@ -5,9 +5,12 @@ context "with block" do context "with auth token" do it "sets the API key in the headers" do - described_class.configure("ExampleAuthToken") do + described_class.configure("ExampleAuthToken", headers: { + 'X-Example-Header' => 'ExampleHeaderValue' + }) do expect(Fera::Base.api_key).to eq("ExampleAuthToken") expect(Fera::Base.headers['Authorization']).to eq("Bearer ExampleAuthToken") + expect(Fera::Base.headers['X-Example-Header']).to eq("ExampleHeaderValue") end expect(Fera::Base.api_key).to be_nil @@ -24,5 +27,25 @@ end end end + + context "without block" do + context "with auth token" do + it "sets the API key in the headers" do + described_class.configure("ExampleAuthToken", headers: { 'X-Example-Header' => 'ExampleHeaderValue' }) + + expect(Fera::Base.api_key).to eq("ExampleAuthToken") + expect(Fera::Base.headers['Authorization']).to eq("Bearer ExampleAuthToken") + expect(Fera::Base.headers['X-Example-Header']).to eq("ExampleHeaderValue") + end + end + + context "with secret key" do + it "sets the API key in the headers" do + described_class.configure("sk_abcd123") + + expect(Fera::Base.api_key).to eq("sk_abcd123") + end + end + end end end