From a89e2ba55399e6bddd9e052cf2064ed0056c958a Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 8 Jun 2016 12:44:38 +0200 Subject: refactor: use new couch_db classes from fetch_key This moves all the em_http related stuff into a single adapter. We're also not using callback and errback inside fetch_key or server for couch requests anymore. Changing the interface for hkp to do the same will follow. --- lib/nickserver/couch/fetch_key.rb | 56 +++++++-------------------------------- 1 file changed, 9 insertions(+), 47 deletions(-) (limited to 'lib/nickserver/couch') diff --git a/lib/nickserver/couch/fetch_key.rb b/lib/nickserver/couch/fetch_key.rb index 3fe2a63..2013bca 100644 --- a/lib/nickserver/couch/fetch_key.rb +++ b/lib/nickserver/couch/fetch_key.rb @@ -1,63 +1,25 @@ -require 'em-http' -require 'json' +require 'nickserver/couch_db/source' +require 'nickserver/adapters/em_http' module Nickserver; module Couch class FetchKey - include EM::Deferrable - - VIEW = "_design/Identity/_view/pgp_key_by_email" def initialize(options={}) - @timeout = 5 + @source = Nickserver::CouchDB::Source.new(adapter) end - def get(uid) - couch_request(uid) - self + def get(uid, &block) + source.query(uid, &block) end protected - # - # For example: - # curl "$COUCH/identities/_design/Identity/_view/pgp_key_by_email?key=\"test1@bitmask.net\"" - # - def couch_request(uid) - query = {"reduce" => "false", "key" => "\"#{uid}\""} - request = EventMachine::HttpRequest.new(FetchKey.couch_url).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 + attr_reader :source - def parse_key_from_response(uid, response) - json = JSON.load(response) - if json["rows"].empty? - self.fail 404, "Not Found" - else - return json["rows"].first["value"] - end - rescue Exception - self.fail 0, "Error parsing CouchDB reply" + def adapter + @adapter ||= Nickserver::Adapters::EmHttp.new end - def self.couch_url - @couch_url ||= begin - url = ['http://'] - if Config.couch_user - url.push Config.couch_user, ':', Config.couch_password, '@' - end - url.push Config.couch_host, ':', Config.couch_port, '/', Config.couch_database - url.push '/', VIEW - url.join - end - end end -end; end \ No newline at end of file +end; end -- cgit v1.2.3 From 1af940f49f9ba5507673c2131fcb228b778083fb Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 9 Jun 2016 11:10:35 +0200 Subject: refactor: remove middleman Couch::FetchKey --- lib/nickserver/couch/fetch_key.rb | 25 ------------------------- 1 file changed, 25 deletions(-) delete mode 100644 lib/nickserver/couch/fetch_key.rb (limited to 'lib/nickserver/couch') diff --git a/lib/nickserver/couch/fetch_key.rb b/lib/nickserver/couch/fetch_key.rb deleted file mode 100644 index 2013bca..0000000 --- a/lib/nickserver/couch/fetch_key.rb +++ /dev/null @@ -1,25 +0,0 @@ -require 'nickserver/couch_db/source' -require 'nickserver/adapters/em_http' - -module Nickserver; module Couch - class FetchKey - - def initialize(options={}) - @source = Nickserver::CouchDB::Source.new(adapter) - end - - def get(uid, &block) - source.query(uid, &block) - end - - protected - - attr_reader :source - - def adapter - @adapter ||= Nickserver::Adapters::EmHttp.new - end - - - end -end; end -- cgit v1.2.3