From 28bdfce9f6707ba42da7d822c476f90c97bc4fbc Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 16 Mar 2017 17:30:39 -0500 Subject: [PATCH 1/2] /sources: add /sources.xml endpoint. --- app/controllers/sources_controller.rb | 9 +++------ app/logical/sources/site.rb | 4 ++-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/app/controllers/sources_controller.rb b/app/controllers/sources_controller.rb index c603f56c9..6e272e3da 100644 --- a/app/controllers/sources_controller.rb +++ b/app/controllers/sources_controller.rb @@ -1,15 +1,12 @@ class SourcesController < ApplicationController - # before_filter :member_only - respond_to :json + respond_to :json, :xml def show @source = Sources::Site.new(params[:url], :referer_url => params[:ref]) @source.get - respond_with(@source) do |format| - format.json do - render :json => @source.to_json - end + respond_with(@source.to_h) do |format| + format.xml { render xml: @source.to_h.to_xml(root: "source") } end end diff --git a/app/logical/sources/site.rb b/app/logical/sources/site.rb index 0c9f67601..d2cd7ba09 100644 --- a/app/logical/sources/site.rb +++ b/app/logical/sources/site.rb @@ -56,7 +56,7 @@ module Sources WikiPage.other_names_match(untranslated_tags).map{|wiki_page| [wiki_page.title, wiki_page.category_name]} end - def to_json + def to_h return { :artist_name => artist_name, :profile_url => profile_url, @@ -71,7 +71,7 @@ module Sources :title => artist_commentary_title, :description => artist_commentary_desc, } - }.to_json + } end def available? From 935cefd27e319311cf634d564a87dc60ab7dc6c1 Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 16 Mar 2017 17:44:53 -0500 Subject: [PATCH 2/2] API: on error, include backtrace in json response. Incidentally fixes issue with error responses inside /sources.json not including the usual `{"success": false}`. --- app/controllers/sources_controller.rb | 10 ---------- app/views/static/error.json.erb | 6 +++++- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/app/controllers/sources_controller.rb b/app/controllers/sources_controller.rb index 6e272e3da..6b86f08fa 100644 --- a/app/controllers/sources_controller.rb +++ b/app/controllers/sources_controller.rb @@ -9,14 +9,4 @@ class SourcesController < ApplicationController format.xml { render xml: @source.to_h.to_xml(root: "source") } end end - -private - - def rescue_exception(exception) - respond_with do |format| - format.json do - render :json => {:message => exception.to_s, :backtrace => exception.backtrace}, :status => :error - end - end - end end diff --git a/app/views/static/error.json.erb b/app/views/static/error.json.erb index 2bc0e5059..e2fa831d9 100644 --- a/app/views/static/error.json.erb +++ b/app/views/static/error.json.erb @@ -1,5 +1,9 @@ <% if @error_message %> {"success": false, "message": <%= raw @error_message.encode("utf-8", {:invalid => :replace, :undef => :replace, :replace => "?"}).to_json %>} <% else %> -{"success": false, "message": <%= raw @exception.to_s.encode("utf-8", {:invalid => :replace, :undef => :replace, :replace => "?"}).to_json %>} + { + "success": false, + "message": <%= raw @exception.to_s.encode("utf-8", {:invalid => :replace, :undef => :replace, :replace => "?"}).to_json %>, + "backtrace": <%= raw @exception.backtrace.to_json %> + } <% end %>