From 513e8ff68f6fb38e8a483f9f1441feb04bd60df1 Mon Sep 17 00:00:00 2001 From: Alex Klarfeld Date: Mon, 25 Sep 2023 11:41:29 -0700 Subject: [PATCH] Updated endpoint for server --- lib/supergood/api.rb | 4 ++-- lib/supergood/client.rb | 4 +++- spec/client_spec.rb | 42 ++++++++++++++++++++--------------------- spec/spec_helper.rb | 4 ++-- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/lib/supergood/api.rb b/lib/supergood/api.rb index e0936ce..833077d 100644 --- a/lib/supergood/api.rb +++ b/lib/supergood/api.rb @@ -29,7 +29,7 @@ def post_events(payload) if @local_only @log.debug(payload) else - uri = URI(@base_url + '/api/events') + uri = URI(@base_url + '/events') response = Net::HTTP.post(uri, payload.to_json, @header_options) if response.code == '200' return JSON.parse(response.body, symbolize_names: true) @@ -45,7 +45,7 @@ def post_errors(payload) if @local_only @log.debug(payload) else - uri = URI(@base_url + '/api/errors') + uri = URI(@base_url + '/errors') response = Net::HTTP.post(uri, payload.to_json, @header_options) if response.code == '200' return JSON.parse(response.body, symbolize_names: true) diff --git a/lib/supergood/client.rb b/lib/supergood/client.rb index f474149..e131b62 100644 --- a/lib/supergood/client.rb +++ b/lib/supergood/client.rb @@ -11,7 +11,7 @@ module Supergood - DEFAULT_SUPERGOOD_BASE_URL = 'https://dashboard.supergood.ai' + DEFAULT_SUPERGOOD_BASE_URL = 'https://api.supergood.ai' class << self def init(config={}) supergood_client_id = config[:client_id] || ENV['SUPERGOOD_CLIENT_ID'] @@ -120,11 +120,13 @@ def intercept(request) request_id = SecureRandom.uuid requested_at = Time.now if !ignored?(request[:domain]) + puts "Caching Request" cache_request(request_id, requested_at, request) end response = yield if !ignored?(request[:domain]) && defined?(response) + puts "Caching Response" cache_response(request_id, requested_at, response) end diff --git a/spec/client_spec.rb b/spec/client_spec.rb index c0f3b0b..ff6ac4e 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -62,7 +62,7 @@ def get_response_format(match_keys = {}) Faraday.get(OUTBOUND_URL) Supergood.close() - expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events'). + expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events'). with { |req| req.body = JSON.parse(req.body, symbolize_names: true) req.body[0][:request] != nil && @@ -82,7 +82,7 @@ def get_response_format(match_keys = {}) end Supergood.close() - expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events'). + expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events'). with { |req| req.body = JSON.parse(req.body, symbolize_names: true) req.body.length() == http_error_codes.length() && @@ -99,7 +99,7 @@ def get_response_format(match_keys = {}) conn = Faraday.new(url: OUTBOUND_URL) response = conn.post('/') Supergood.close() - expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events'). + expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events'). with { |req| req.body = JSON.parse(req.body, symbolize_names: true) req.body[0][:request] != nil && @@ -120,7 +120,7 @@ def get_response_format(match_keys = {}) Faraday.get(OUTBOUND_URL) Supergood.close() - expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events')). + expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events')). to have_not_been_made end end @@ -136,7 +136,7 @@ def get_response_format(match_keys = {}) Faraday.get(OUTBOUND_URL) Supergood.close() - expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events'). + expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events'). with { |req| req.body = JSON.parse(req.body, symbolize_names: true) req.body[0][:request] != nil && @@ -155,7 +155,7 @@ def get_response_format(match_keys = {}) Supergood.close() Supergood.close() - expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events'). + expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events'). with { |req| req.body = JSON.parse(req.body, symbolize_names: true) req.body[0][:request] != nil && @@ -179,7 +179,7 @@ def get_response_format(match_keys = {}) Faraday.get(OUTBOUND_URL) Faraday.get(OUTBOUND_URL) - expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events'). + expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events'). with { |req| req.body = JSON.parse(req.body, symbolize_names: true) req.body[0][:request] != nil && @@ -201,7 +201,7 @@ def get_response_format(match_keys = {}) end Supergood.close() - expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events'). + expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events'). with { |req| req.body = JSON.parse(req.body, symbolize_names: true) req.body[0][:request] != nil && @@ -214,13 +214,13 @@ def get_response_format(match_keys = {}) WebMock.reset! stub_request(:get, OUTBOUND_URL).to_return(status: 200, body: { message: 'success' }.to_json, headers: {}) - stub_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/errors').to_return(status: 200, body: { message: 'Success' }.to_json, headers: {}) - stub_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events').to_raise(SupergoodException.new ERRORS[:POSTING_EVENTS]) + stub_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/errors').to_return(status: 200, body: { message: 'Success' }.to_json, headers: {}) + stub_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events').to_raise(SupergoodException.new ERRORS[:POSTING_EVENTS]) Supergood.init() Faraday.get(OUTBOUND_URL) Supergood.close() - expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/errors'). + expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/errors'). with { | req | req.body = JSON.parse(req.body, symbolize_names: true) req.body[:error] != nil @@ -235,7 +235,7 @@ def get_response_format(match_keys = {}) Supergood.init(config={ keysToHash: ['response.body']}) Faraday.get(OUTBOUND_URL) Supergood.close() - expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events'). + expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events'). with { |req| req.body = JSON.parse(req.body, symbolize_names: true) req.body[0][:response][:body][:hashed] == 'ODFhZjA0MTdmOTY5ZjkzODQ4YjFjZjMwZmNlMWRiOTM4ODRmYWNjMQ==' @@ -249,7 +249,7 @@ def get_response_format(match_keys = {}) Supergood.init(config={ keysToHash: ['response.body.message'] }) Faraday.get(OUTBOUND_URL) Supergood.close() - expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events'). + expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events'). with { |req| req.body = JSON.parse(req.body, symbolize_names: true) req.body[0][:response][:body][:message] == 'MTFmMzc2NTRkYTJkNWM5MmMzODU2MjM4ZmJlYmNkZjY0NGQ3NjEwNw==' @@ -263,7 +263,7 @@ def get_response_format(match_keys = {}) Supergood.init(config={ ignoredDomains: ['example.com'] }) Faraday.get(OUTBOUND_URL) Supergood.close() - expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events')). + expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events')). to have_not_been_made end @@ -276,7 +276,7 @@ def get_response_format(match_keys = {}) Faraday.get(OUTBOUND_URL) Faraday.get(SECOND_OUTBOUND_URL) Supergood.close() - expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events'). + expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events'). with { |req| req.body = JSON.parse(req.body, symbolize_names: true) req.body.length == 1 && @@ -293,7 +293,7 @@ def get_response_format(match_keys = {}) Faraday.get(OUTBOUND_URL) Faraday.get(SECOND_OUTBOUND_URL) Supergood.close() - expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events'). + expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events'). with { |req| req.body = JSON.parse(req.body, symbolize_names: true) req.body.length == 1 && @@ -313,7 +313,7 @@ def get_response_format(match_keys = {}) Faraday.get(OUTBOUND_URL) Supergood.close() - expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events'). + expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events'). with { |req| req.body = JSON.parse(req.body, symbolize_names: true) req.body[0][:response][:body][:hashed] == 'ZTg2YjZhNjhjNTM5NGRmN2UyNGRhNGQzZjQxNzEyNmE2OTBlMDI3Nw==' && @@ -331,7 +331,7 @@ def get_response_format(match_keys = {}) Faraday.get(OUTBOUND_URL) Supergood.close() - expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events'). + expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events'). with { |req| req.body = JSON.parse(req.body, symbolize_names: true) req.body[0][:response][:body].to_json == payload && @@ -350,7 +350,7 @@ def get_response_format(match_keys = {}) RestClient.get(OUTBOUND_URL) Supergood.close() - expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events'). + expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events'). with { |req| req.body = JSON.parse(req.body, symbolize_names: true) req.body[0][:response][:body].to_json == payload && @@ -367,7 +367,7 @@ def get_response_format(match_keys = {}) HTTParty.get(OUTBOUND_URL, { :headers => { 'Accept': 'application/json' }}) Supergood.close() - expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events'). + expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events'). with { |req| req.body = JSON.parse(req.body, symbolize_names: true) req.body[0][:response][:body].to_json == payload && @@ -388,7 +388,7 @@ def get_response_format(match_keys = {}) Supergood.close() - expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events'). + expect(a_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events'). with { |req| req.body = JSON.parse(req.body, symbolize_names: true) req.body[0][:response][:body].to_json == payload && diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a977d2d..5fba379 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -6,10 +6,10 @@ RSpec.configure do |config| config.before(:each) do - stub_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/events'). + stub_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/events'). to_return(status: 200, body: { message: 'Success' }.to_json, headers: {}) - stub_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/api/errors'). + stub_request(:post, ENV['SUPERGOOD_BASE_URL'] + '/errors'). to_return(status: 200, body: { message: 'Success' }.to_json, headers: {}) end end