From 40ff8039a09ec77a1963773836f66d37480a56c8 Mon Sep 17 00:00:00 2001 From: Vlad Carballo Date: Thu, 27 Oct 2022 13:14:33 -0400 Subject: [PATCH 1/4] Add Gemfile --- Gemfile | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Gemfile diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..cd8aa9e --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gemspec \ No newline at end of file From d1c708d3f45c43c9a1e99a08176ecba23e5e2e18 Mon Sep 17 00:00:00 2001 From: Vlad Carballo Date: Thu, 27 Oct 2022 13:15:17 -0400 Subject: [PATCH 2/4] Upgrade Faraday and Json gems to v2 --- kickbox.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kickbox.gemspec b/kickbox.gemspec index f3a6f88..bddc3a9 100644 --- a/kickbox.gemspec +++ b/kickbox.gemspec @@ -15,6 +15,6 @@ Gem::Specification.new do |gem| gem.files = Dir["lib/**/*"] - gem.add_dependency "faraday", "~> 1.0" - gem.add_dependency "json", ">= 1.8" + gem.add_dependency "faraday", "~> 2.0" + gem.add_dependency "json", "~> 2.0" end From c0bb083fd1d9e7e04c95fd1529df43c9e0d4697c Mon Sep 17 00:00:00 2001 From: Vlad Carballo Date: Thu, 27 Oct 2022 13:29:02 -0400 Subject: [PATCH 3/4] Minor cleanup --- .gitignore | 1 + Gemfile | 2 +- lib/kickbox/http_client/error_handler.rb | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index d87d4be..2de3904 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ spec/reports test/tmp test/version_tmp tmp +.idea diff --git a/Gemfile b/Gemfile index cd8aa9e..fa75df1 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,3 @@ source 'https://rubygems.org' -gemspec \ No newline at end of file +gemspec diff --git a/lib/kickbox/http_client/error_handler.rb b/lib/kickbox/http_client/error_handler.rb index 0090d78..5a578bd 100644 --- a/lib/kickbox/http_client/error_handler.rb +++ b/lib/kickbox/http_client/error_handler.rb @@ -2,7 +2,7 @@ module Kickbox module HttpClient - # ErrorHanlder takes care of selecting the error message from response body + # ErrorHandler takes care of selecting the error message from response body class ErrorHandler < Faraday::Middleware def initialize(app) From 3f8e76e57c7cceb22c26e81ee8a6f5082af10719 Mon Sep 17 00:00:00 2001 From: Vlad Carballo Date: Thu, 27 Oct 2022 14:11:34 -0400 Subject: [PATCH 4/4] Refactor and Reformat code --- lib/kickbox/api/kickbox.rb | 2 +- lib/kickbox/client.rb | 2 +- lib/kickbox/http_client/auth_handler.rb | 33 +++++++-------------- lib/kickbox/http_client/response_handler.rb | 8 ++--- 4 files changed, 16 insertions(+), 29 deletions(-) diff --git a/lib/kickbox/api/kickbox.rb b/lib/kickbox/api/kickbox.rb index a0a3757..29d64d9 100644 --- a/lib/kickbox/api/kickbox.rb +++ b/lib/kickbox/api/kickbox.rb @@ -15,7 +15,7 @@ def initialize(client) # # email - Email address to verify def verify(email, options = {}) - body = options.fetch("query", {}) + body = options.fetch("query", {}) timeout = options.fetch("timeout", 6000) email = CGI::escape(email) diff --git a/lib/kickbox/client.rb b/lib/kickbox/client.rb index 0621778..4871196 100644 --- a/lib/kickbox/client.rb +++ b/lib/kickbox/client.rb @@ -12,7 +12,7 @@ def initialize(auth = {}, options = {}) end # - def kickbox() + def kickbox Kickbox::Api::Kickbox.new(@http_client) end diff --git a/lib/kickbox/http_client/auth_handler.rb b/lib/kickbox/http_client/auth_handler.rb index 6401c58..a89ecd3 100644 --- a/lib/kickbox/http_client/auth_handler.rb +++ b/lib/kickbox/http_client/auth_handler.rb @@ -15,40 +15,29 @@ def initialize(app, auth = {}, options = {}) end def call(env) - if !@auth.empty? - auth = get_auth_type - flag = false - - if auth == HTTP_HEADER - env = http_header(env) - flag = true - end - - if !flag - raise StandardError.new "Unable to calculate authorization method. Please check" - end - else - raise StandardError.new "Server requires authentication to proceed further. Please check" + raise StandardError.new("Server requires authentication to proceed further. Please check") if @auth.empty? + + if get_auth_type == HTTP_HEADER + env = http_header(env) end @app.call(env) end # Calculating the Authentication Type - def get_auth_type() - + def get_auth_type if @auth.has_key?(:http_header) - return HTTP_HEADER + HTTP_HEADER + else + raise StandardError.new("Unable to calculate authorization method. Please check") end - - return -1 end # Authorization with HTTP header def http_header(env) env[:request_headers]["Authorization"] = "token #{@auth[:http_header]}" - return env + env end def query_params(url) @@ -60,11 +49,11 @@ def query_params(url) end def merge_query(env, query) - query = query.update query_params(env[:url]) + query = query.update(query_params(env[:url])) env[:url].query = Faraday::Utils.build_query(query) - return env + env end end diff --git a/lib/kickbox/http_client/response_handler.rb b/lib/kickbox/http_client/response_handler.rb index ca445a7..2177148 100644 --- a/lib/kickbox/http_client/response_handler.rb +++ b/lib/kickbox/http_client/response_handler.rb @@ -7,14 +7,12 @@ class ResponseHandler def self.get_body(env) type = env.response_headers["content-type"] || '' - body = env.body # Response body is in JSON - if type.include?("json") - body = JSON.parse body - end + type.include?("json") ? + return JSON.parse(body) : + return env.body - return body end end