Compare commits
No commits in common. "4a821745264afaf9c2c6c83fb301ff3565e19ed9" and "3421e18f21de0b231b9ac44b610cda18b913a6ce" have entirely different histories.
4a82174526
...
3421e18f21
10
app.rb
10
app.rb
|
|
@ -7,8 +7,6 @@ class App < Roda
|
||||||
plugin :sessions, secret: ENV.delete('APP_SESSION_SECRET')
|
plugin :sessions, secret: ENV.delete('APP_SESSION_SECRET')
|
||||||
plugin :render, escape: true
|
plugin :render, escape: true
|
||||||
plugin :flash
|
plugin :flash
|
||||||
plugin :json_parser
|
|
||||||
plugin :request_headers
|
|
||||||
|
|
||||||
DB = Sequel.sqlite("db/#{ENV['DB_NAME']}")
|
DB = Sequel.sqlite("db/#{ENV['DB_NAME']}")
|
||||||
links = DB[:links]
|
links = DB[:links]
|
||||||
|
|
@ -29,7 +27,7 @@ class App < Roda
|
||||||
|
|
||||||
r.post "create" do
|
r.post "create" do
|
||||||
url = r.params['url']
|
url = r.params['url']
|
||||||
if url.nil? or url.empty?
|
if url.empty?
|
||||||
flash['message'] = "Please enter a valid URL";
|
flash['message'] = "Please enter a valid URL";
|
||||||
r.redirect '/'
|
r.redirect '/'
|
||||||
end
|
end
|
||||||
|
|
@ -38,12 +36,8 @@ class App < Roda
|
||||||
links.insert(url: url, code: code)
|
links.insert(url: url, code: code)
|
||||||
@message = "Link created"
|
@message = "Link created"
|
||||||
end
|
end
|
||||||
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'] + '/' + links.filter(:url => url).first[:code]
|
||||||
if 'application/json' == r.headers['CONTENT_TYPE']
|
|
||||||
return {url: url, code: code, link: @new_link}.to_json
|
|
||||||
end
|
|
||||||
view :create
|
view :create
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -14,14 +14,8 @@ 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
|
||||||
it "should return link data in json format" do
|
it "should return link data in json format" do
|
||||||
data = {
|
post '/create'
|
||||||
url: 'http://google.com'
|
last_response.should be_ok
|
||||||
}
|
|
||||||
post('/create', data.to_json, "CONTENT_TYPE" => "application/json")
|
|
||||||
expect(last_response).to be_ok
|
|
||||||
response_json = JSON.parse(last_response.body)
|
|
||||||
expect(response_json['url']).to eq(data[:url])
|
|
||||||
expect(response_json['code']).not_to eq(nil)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue