Skip to content

Commit

Permalink
wire in /contacts.json endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
William Morgan committed Mar 10, 2012
1 parent 81e99dc commit cb45418
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
18 changes: 18 additions & 0 deletions bin/heliotrope-server
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class Set
def to_json(*a); to_a.to_json(*a) end
end

module Enumerable
def uniq_by; inject({}) { |h, e| h[yield(e)] ||= e; h }.values end
end

class HeliotropeServer < Sinatra::Base
API_VERSION = 0.1

Expand Down Expand Up @@ -421,6 +425,20 @@ class HeliotropeServer < Sinatra::Base
get_message_summary(message_id).merge(:response => :ok).to_json
end

get "/contacts.json" do
content_type :json

prefix = params["prefix"]
num = (params["num"] || 20).to_i

contacts = @metaindex.contacts(:prefix => prefix, :num => num).
sort_by { |c| -c[:timestamp] }.
uniq_by { |c| c[:email] }.
map { |c| { :name => c[:name], :email => c[:email] } }

{ :response => :ok, :contacts => contacts }.to_json
end

private

def parse_json_set val
Expand Down
1 change: 1 addition & 0 deletions lib/heliotrope-client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def raw_message message_id
def labels; get_json("labels")["labels"] end
def info; get_json("info") end
def size; get_json("size")["size"] end
def contacts prefix; get_json("contacts")["contacts"] end

def prune_labels!; post_json("labels/prune")["labels"] end

Expand Down

0 comments on commit cb45418

Please sign in to comment.