Compare commits

..

No commits in common. "ac3e9f14cd5824f10750fd5a705beed3ef387663" and "d1a0fc02222a7c4099269d77faddd0c3845eeebd" have entirely different histories.

2 changed files with 2 additions and 33 deletions

23
app.rb
View File

@ -41,29 +41,10 @@ class App < Roda
code = links.filter(:url => url).first[:code] code = links.filter(:url => url).first[:code]
@message ||= "Link exists" @message ||= "Link exists"
@new_link = 'http://' + request.env['HTTP_HOST'] + '/' + code @new_link = 'http://' + request.env['HTTP_HOST'] + '/' + code
view :create if 'application/json' == r.headers['CONTENT_TYPE']
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 return {url: url, code: code, link: @new_link}.to_json
end end
view :create
end end
end end
end end

View File

@ -7,24 +7,12 @@ require 'sequel'
require 'rspec' require 'rspec'
require 'rack/test' 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 def app
App App
end end
describe "Submit API request to create new link" do describe "Submit API request to create new link" do
include Rack::Test::Methods 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 it "should return link data in json format when a valid url is entered" do
data = { data = {
url: 'http://google.com' url: 'http://google.com'