Skip to content

Commit

Permalink
Updated endpoint for server
Browse files Browse the repository at this point in the history
  • Loading branch information
aklarfeld committed Sep 25, 2023
1 parent da29ba4 commit 513e8ff
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
4 changes: 2 additions & 2 deletions lib/supergood/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion lib/supergood/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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

Expand Down
42 changes: 21 additions & 21 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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() &&
Expand All @@ -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 &&
Expand All @@ -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
Expand All @@ -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 &&
Expand All @@ -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 &&
Expand All @@ -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 &&
Expand All @@ -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 &&
Expand All @@ -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
Expand All @@ -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=='
Expand All @@ -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=='
Expand All @@ -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

Expand All @@ -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 &&
Expand All @@ -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 &&
Expand All @@ -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==' &&
Expand All @@ -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 &&
Expand All @@ -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 &&
Expand All @@ -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 &&
Expand All @@ -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 &&
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 513e8ff

Please sign in to comment.