diff --git a/Gemfile b/Gemfile index f54cec2..123a2d3 100644 --- a/Gemfile +++ b/Gemfile @@ -22,6 +22,7 @@ gem 'omniauth-openid' gem 'mechanize' gem 'pingback' +gem 'xml-simple', :require => 'xmlsimple' gem 'mysql2', '0.3.7' gem 'dm-core' diff --git a/Gemfile.lock b/Gemfile.lock index df44c1d..4c381a0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -132,6 +132,7 @@ GEM rack raindrops (~> 0.7) webrobots (0.0.13) + xml-simple (1.1.1) PLATFORMS ruby @@ -165,3 +166,4 @@ DEPENDENCIES sinatra-namespace sinatra-support thin + xml-simple diff --git a/controller.rb b/controller.rb index 6b10d56..36aae0c 100644 --- a/controller.rb +++ b/controller.rb @@ -83,15 +83,23 @@ def rpc_error(code, error, string) error code, XMLRPC::Marshal.dump_response(XMLRPC::FaultException.new(error.to_i, string)) end + def api_response(format, code, data) + if format == 'json' + json_response(code, data) + elsif format == 'xml' + xml_response(code, data) + end + end + def json_error(code, data) - halt code, { - 'Content-Type' => 'application/json;charset=UTF-8', - 'Cache-Control' => 'no-store' - }, - data.to_json + json_response(code, data) end def json_respond(code, data) + json_response(code, data) + end + + def json_response(code, data) halt code, { 'Content-Type' => 'application/json;charset=UTF-8', 'Cache-Control' => 'no-store' @@ -99,4 +107,9 @@ def json_respond(code, data) data.to_json end + def xml_response(code, data) + xml = XmlSimple.xml_out(data, {'KeepRoot' => true}) + halt code, xml + end + end diff --git a/controllers/api.rb b/controllers/api.rb index a6ae202..f812ac0 100644 --- a/controllers/api.rb +++ b/controllers/api.rb @@ -1,9 +1,14 @@ class Controller < Sinatra::Base - get "/api/links" do + get %r{/api/links(:?\.(?json|xml))?} do + + if params['format'].nil? + format = 'json' + end + format = params['format'] if params[:target].empty? and params[:access_token].empty? - json_error 400, { + api_response format, 400, { error: "invalid_input", error_description: "Either an access token or a target URI is required" } @@ -12,14 +17,14 @@ class Controller < Sinatra::Base if params[:access_token].empty? target = Page.first :href => params[:target] if target.nil? - json_error 404, { + api_response format, 404, { error: "not_found", error_description: "The specified link was not found" } end if !target.site.public_access - json_error 401, { + api_response format, 401, { error: "forbidden", error_description: "This site does not allow public access to its pingbacks" } @@ -30,7 +35,7 @@ class Controller < Sinatra::Base account = Account.first :token => params[:access_token] if account.nil? - json_error 401, { + api_response format, 401, { error: "forbidden", error_description: "Access token was not valid" } @@ -42,7 +47,7 @@ class Controller < Sinatra::Base page = account.sites.pages.first(:href => params[:target]) if page.nil? - json_error 404, { + api_response format, 404, { error: "not_found", error_description: "There are no links for the specified page" } @@ -62,9 +67,14 @@ class Controller < Sinatra::Base } end - json_respond 200, { - links: link_array - } + if format == 'json' + api_response format, 200, { + links: link_array + } + else + atom_feed = {links: link_array} + api_response format, 200, atom_feed + end end end