Skip to content

Commit

Permalink
include aggregate counts by type in /api/count URL
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronpk committed Jul 6, 2017
1 parent 7baa8e1 commit 689de04
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 105 deletions.
122 changes: 18 additions & 104 deletions controllers/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,120 +3,34 @@ class Controller < Sinatra::Base
get %r{/api/count(:?\.(?<format>json))?} do
format = params['format'] || 'json'

if params[:base].empty? and !params[:targets].empty?
if params[:target].empty?
api_response format, 400, {
error: "invalid_input",
error_description: "Parameter \"base\" is required, example: http://example.com"
error_description: "A target URI is required"
}
end

if params[:base]
base = URI.parse params[:base]
if base.nil?
api_response format, 400, {
error: "invalid_input",
error_description: "Invalid parameter \"base\""
}
end

site = Site.first :domain => base.host
if site.nil?
api_response format, 404, {
error: "not_found",
error_description: "The site was not found"
}
end

if site.public_access == false
api_response format, 401, {
error: "forbidden",
error_description: "This site does not allow public access to its mentions"
}
end

if params[:targets].empty?
api_response format, 400, {
error: "invalid_input",
error_description: "Parameter \"target\" is required"
}
end

targets = Page.all :href => params[:targets].split(",").map{|t| "#{params[:base]}#{t}"}
if targets.nil?
api_response format, 404, {
error: "not_found",
error_description: "The specified link was not found"
}
end

counts = {}
targets.each do |t|
links = t.links.count(:verified => true)
counts[t.href] = links
end

if format == 'json'
api_response format, 200, {
count: counts
}
else
atom_feed = {links: link_array}
api_response format, 200, atom_feed
end
links_of_type = {}

targets = Page.all :href => params[:target]
if targets.nil?
links = 0
else

if params[:target].empty? and params[:access_token].empty?
api_response format, 400, {
error: "invalid_input",
error_description: "Either an access token or a target URI is required"
}
end

if params[:access_token].empty?
target = Page.first :href => params[:target]
if target.nil?
links = 0
else
if target.site.public_access == false
api_response format, 401, {
error: "forbidden",
error_description: "This site does not allow public access to its mentions"
}
end

links = target.links.count(:verified => true)
end
else

account = Account.first :token => params[:access_token]

if account.nil?
api_response format, 401, {
error: "forbidden",
error_description: "Access token was not valid"
}
end

target = account.sites.pages.first :href => params[:target]

if target.nil?
links = 0
else
links = target.links.count(:verified => true)
links = targets.links.all(:verified => true).each{|link| link.id}.length
types = repository(:default).adapter.select('SELECT type, COUNT(1) AS num FROM links
WHERE page_id IN ('+targets.map{|t| t.id}.join(',')+')
GROUP BY type')
types.each do |type|
if type.type
links_of_type[(type.type == "link" ? "mention" : type.type)] = type.num
end
end

if format == 'json'
api_response format, 200, {
count: links
}
else
atom_feed = {count: count}
api_response format, 200, atom_feed
end

end

api_response 'json', 200, {
count: links,
type: links_of_type
}
end

get %r{/api/(links|mentions)(:?\.(?<format>json|atom|jf2))?} do
Expand Down
15 changes: 14 additions & 1 deletion views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

<p><b>Getting the data manually</b></p>

<p>To retrieve the data manually using jQuery, you can make a request like the following. Be sure to add a parameter for your access token if your account is set to disable public access to the data.</p>
<p>To retrieve the data manually using jQuery, you can make a request like the following.</p>

<p><pre><code>$(function(){
$.getJSON("https://webmention.io/api/count?jsonp=?", {
Expand All @@ -61,6 +61,19 @@
console.log(data.count);
});
});</code></pre></p>

<p>This will return a list of total mentions of the URL as well as grouped by type.</p>

<p><pre><code>{
"count": 6,
"type": {
"bookmark": 1,
"mention": 2,
"rsvp-maybe": 1,
"rsvp-no": 1,
"rsvp-yes": 1
}
}</code></pre></p>

<p><b>Displaying counts automatically</b></p>

Expand Down

0 comments on commit 689de04

Please sign in to comment.