diff options
author | elijah <elijah@riseup.net> | 2013-05-15 22:03:08 -0700 |
---|---|---|
committer | elijah <elijah@riseup.net> | 2013-05-15 22:03:08 -0700 |
commit | 51dc03481a9be5148f16e8022a1b00b658739ff3 (patch) | |
tree | e370f488b6ce4b79c09778621d9f8fc2d95ee88d /lib/nickserver/couch | |
parent | b078659defeead5b68ca2f387d0e308fa7511eb4 (diff) |
add CouchDB support.
Diffstat (limited to 'lib/nickserver/couch')
-rw-r--r-- | lib/nickserver/couch/fetch_key.rb | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/lib/nickserver/couch/fetch_key.rb b/lib/nickserver/couch/fetch_key.rb new file mode 100644 index 0000000..d729ebc --- /dev/null +++ b/lib/nickserver/couch/fetch_key.rb @@ -0,0 +1,62 @@ +require 'em-http' +require 'json' + +module Nickserver; module Couch + class FetchKey + include EM::Deferrable + + def initialize(options={}) + @timeout = 5 + end + + def get(uid) + uid = uid.split('@').first # TEMPORARY HACK FOR NOW. in the future + # the database should be able to be searchable by full address + couch_request(uid) + self + end + + protected + + # + # curl http://localhost:5984/users/_design/User/_view/pgp_key_by_handle?key=%22bla%22\&reduce=false + # + def couch_request(uid) + query = {"reduce" => "false", "key" => "\"#{uid}\""} + request = EventMachine::HttpRequest.new("#{FetchKey.couch_url}/#{FetchKey.couch_view}").get(:timeout => @timeout, :query => query) + request.callback {|http| + if http.response_header.status != 200 + self.fail http.response_header.status, 'Unknown Error' + else + self.succeed parse_key_from_response(uid, http.response) + end + }.errback {|http| + self.fail 0, http.error + } + end + + def parse_key_from_response(uid, response) + json = JSON.load(response) + if json["offset"] == 0 + self.fail 404, "Not Found" + else + return json["rows"].first["value"] + end + rescue Exception + self.fail 0, "Error parsing CouchDB reply" + end + + def self.couch_view + "_design/User/_view/pgp_key_by_handle" + end + + def self.couch_url + if Config.couch_user + ['http://', Config.couch_user, ':', Config.couch_password, '@', Config.couch_host, ':', Config.couch_port, '/', Config.couch_database].join + else + ['http://', Config.couch_host, ':', Config.couch_port, '/', Config.couch_database].join + end + end + + end +end; end
\ No newline at end of file |