Skip to content

Commit

Permalink
Returns JSON or XML output in preparation for return an atom feed of …
Browse files Browse the repository at this point in the history
…pingbacks.
  • Loading branch information
aaronpk committed Jan 28, 2013
1 parent dbea4e0 commit ba426fd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -22,6 +22,7 @@ gem 'omniauth-openid'

gem 'mechanize'
gem 'pingback'
gem 'xml-simple', :require => 'xmlsimple'

gem 'mysql2', '0.3.7'
gem 'dm-core'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -132,6 +132,7 @@ GEM
rack
raindrops (~> 0.7)
webrobots (0.0.13)
xml-simple (1.1.1)

PLATFORMS
ruby
Expand Down Expand Up @@ -165,3 +166,4 @@ DEPENDENCIES
sinatra-namespace
sinatra-support
thin
xml-simple
23 changes: 18 additions & 5 deletions controller.rb
Expand Up @@ -83,20 +83,33 @@ 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'
},
data.to_json
end

def xml_response(code, data)
xml = XmlSimple.xml_out(data, {'KeepRoot' => true})
halt code, xml
end

end
28 changes: 19 additions & 9 deletions controllers/api.rb
@@ -1,9 +1,14 @@
class Controller < Sinatra::Base

get "/api/links" do
get %r{/api/links(:?\.(?<format>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"
}
Expand All @@ -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"
}
Expand All @@ -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"
}
Expand All @@ -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"
}
Expand All @@ -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

0 comments on commit ba426fd

Please sign in to comment.