Compare commits

...

3 Commits

Author SHA1 Message Date
Adam Townsend ac3e9f14cd added links route for API interactions 2023-10-12 11:42:43 -07:00
Adam Townsend 5ee9351362 added db initialization to make sure the db exists and is up to date
before tests
2023-10-12 11:42:14 -07:00
Adam Townsend 2964e06639 added before and after actions
+ before initializes the db
+ after cleans the db
2023-10-12 11:40:15 -07:00
2 changed files with 33 additions and 2 deletions

23
app.rb
View File

@ -41,10 +41,29 @@ class App < Roda
code = links.filter(:url => url).first[:code]
@message ||= "Link exists"
@new_link = 'http://' + request.env['HTTP_HOST'] + '/' + code
if 'application/json' == r.headers['CONTENT_TYPE']
view :create
end
r.on "links" do
r.post do
if 'application/json' != r.headers['CONTENT_TYPE']
return {message: "Not a valid JSON request"}.to_json
end
url = r.params['url']
if url.nil? or url.empty?
response.status = 400
return {message: "Please enter a valid URL"}
end
if links.filter(:url => url).first.nil?
code = SecureRandom.urlsafe_base64 4
links.insert(url: url, code: code)
end
code = links.filter(:url => url).first[:code]
@new_link = 'http://' + request.env['HTTP_HOST'] + '/' + code
return {url: url, code: code, link: @new_link}.to_json
end
view :create
end
end
end

View File

@ -7,12 +7,24 @@ require 'sequel'
require 'rspec'
require 'rack/test'
# DB initialization
Sequel.extension :migration
Sequel.sqlite("db/#{ENV['DB_NAME']}") do |db|
Sequel::Migrator.apply(db, "db/migrations")
end
def app
App
end
describe "Submit API request to create new link" do
include Rack::Test::Methods
before :each do
@links = Sequel.sqlite("db/#{ENV['DB_NAME']}")[:links]
end
after :each do
@links.delete
end
it "should return link data in json format when a valid url is entered" do
data = {
url: 'http://google.com'