Skip to content
Permalink
Browse files

Honor requested response type

  • Loading branch information...
scottwater committed Feb 1, 2019
1 parent 09d70d9 commit 8a35779146e9c1e63b753ff282844529801eab3f
Showing with 30 additions and 9 deletions.
  1. +26 −5 app/controllers/links_controller.rb
  2. +4 −4 spec/requests/links_spec.rb
@@ -2,16 +2,24 @@ class LinksController < ApplicationController
before_action :verify_access, only: [:create, :show]

def index
render json: {message: "Welcome to Shorti!"}
end
if is_json_request?
render json: {message: "Welcome to Shorti!"}
else
render plain: "Welcome to Shorti"
end
end

def create
link = Link.new
link.url = params[:url]
link.title = params[:title]
link.description = params[:description]
if link.save
render json: {id: link.share_id, url: link.share_url(base_share_url)}, status: :created
if is_json_request?
render json: {id: link.share_id, url: link.share_url(base_share_url)}, status: :created
else
render plain: link.share_url(base_share_url), status: :created
end
else
error = link.errors.any? ? link.errors.full_messages.join(", ") : "Unknown error"
render json: {error: error}, status: :unprocessable_entity
@@ -37,14 +45,27 @@ def show
description: link.description,
url: link.share_url(base_share_url)
}
render json: data
if is_json_request?
render json: data
else
render plain: data.map {|k,v| "#{k}: #{v}"}.join("\n")
end
else
render json: {error: "No URL is available for #{params[:share_id]}"}, status: :not_found
error_message = "No URL is available for #{params[:share_id]}"
if is_json_request?
render json: {error: error_message}, status: :not_found
else
render plain: error_message, status: :not_found
end
end
end

private

def is_json_request?
request.accept == "application/json"
end

def base_share_url
ENV.fetch("SHORTI_BASE_URL", request.url)
end
@@ -21,7 +21,7 @@

it "will add a new link with a custom URL" do
ENV["SHORTI_BASE_URL"] = "https://shorti.shorti/"
post "/", params: {url: "https://scottw.com"}
post "/", params: {url: "https://scottw.com"}, headers: {"HTTP_ACCEPT" => 'application/json'}
expect(json["url"]).to match("https://shorti.shorti/#{json["id"]}")
end

@@ -44,7 +44,7 @@

it "will return a clean url to share (no API KEY)" do
ENV["SHORTI_API_KEY"] = "ABC"
post "/?api_key=ABC", params: {url: "https://scottw.com"}
post "/?api_key=ABC", params: {url: "https://scottw.com"}, headers: {"HTTP_ACCEPT" => 'application/json'}

expect(json["url"]).to eql("http://www.example.com/#{json["id"]}")
end
@@ -72,7 +72,7 @@
end

it "will show the info about the link" do
get "/info/#{link.share_id}"
get "/info/#{link.share_id}", headers: {"HTTP_ACCEPT" => "application/json"}
expect(json["url"]).to eql("http://www.example.com/#{link.share_id}")
end

@@ -87,7 +87,7 @@
ENV["SHORTI_INFO_API_KEY"] = "Y"
ENV["SHORTI_API_KEY"] = "YES"
get "/info/#{link.share_id}", params: {api_key: "YES"}
expect(json["url"]).to eql("http://www.example.com/#{link.share_id}")
expect(response.body).to include("http://www.example.com/#{link.share_id}")
end
end

0 comments on commit 8a35779

Please sign in to comment.
You can’t perform that action at this time.