Skip to content

Commit

Permalink
Honor requested response type
Browse files Browse the repository at this point in the history
  • Loading branch information
scottwater committed Feb 1, 2019
1 parent 09d70d9 commit 8a35779
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
31 changes: 26 additions & 5 deletions app/controllers/links_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/requests/links_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 8a35779

Please sign in to comment.