Skip to content

Commit

Permalink
Update configure method to accept headers parameter. Update gem versi…
Browse files Browse the repository at this point in the history
…on to 0.4.0
  • Loading branch information
azemoh committed Jan 6, 2024
1 parent 6efc4f0 commit eb12f39
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/fera/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/fera/api/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Fera
module API
VERSION = "0.3.4"
VERSION = "0.4.0"
end
end
25 changes: 24 additions & 1 deletion spec/fera/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit eb12f39

Please sign in to comment.