diff options
author | Azul <azul@riseup.net> | 2016-06-08 12:44:38 +0200 |
---|---|---|
committer | Azul <azul@riseup.net> | 2016-06-08 12:54:23 +0200 |
commit | a89e2ba55399e6bddd9e052cf2064ed0056c958a (patch) | |
tree | c28ec332f9763f395a88db991defcae59fa46806 /lib | |
parent | 712c1d062c08a58ca8772aafcdc39d0281959b4a (diff) |
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.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/nickserver/adapters/couch_db.rb | 12 | ||||
-rw-r--r-- | lib/nickserver/adapters/em_http.rb | 24 | ||||
-rw-r--r-- | lib/nickserver/config.rb | 15 | ||||
-rw-r--r-- | lib/nickserver/couch/fetch_key.rb | 56 | ||||
-rw-r--r-- | lib/nickserver/couch_db/source.rb | 9 | ||||
-rw-r--r-- | lib/nickserver/server.rb | 27 |
6 files changed, 70 insertions, 73 deletions
diff --git a/lib/nickserver/adapters/couch_db.rb b/lib/nickserver/adapters/couch_db.rb deleted file mode 100644 index e8a23ad..0000000 --- a/lib/nickserver/adapters/couch_db.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'nickserver/adapters' - -class Nickserver::Adapters::CouchDB - - - protected - - def query_couch(nick) - yield 404, "{}" - end - -end diff --git a/lib/nickserver/adapters/em_http.rb b/lib/nickserver/adapters/em_http.rb new file mode 100644 index 0000000..16db5ae --- /dev/null +++ b/lib/nickserver/adapters/em_http.rb @@ -0,0 +1,24 @@ +require 'nickserver/adapters' +require 'em-http' + +module Nickserver::Adapters + class EmHttp + + def initialize + @timeout = 5 + end + + def get(url, options = {}) + get_request(url, options).callback {|http| + yield http.response_header.status, http.response + }.errback {|http| + yield 0, http.error + } + end + + def get_request(url, options = {}) + @request = EventMachine::HttpRequest.new(url) + @request.get timeout: @timeout, query: options[:query] + end + end +end diff --git a/lib/nickserver/config.rb b/lib/nickserver/config.rb index 13e284b..b1def7c 100644 --- a/lib/nickserver/config.rb +++ b/lib/nickserver/config.rb @@ -36,6 +36,21 @@ module Nickserver self.validate end + def self.couch_url + [ 'http://', + couch_auth, + couch_host, + ':', + couch_port, + '/', + couch_database + ].join + end + + def self.couch_auth + "#{couch_user}:#{couch_password}@" if couch_user + end + private def self.validate 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 diff --git a/lib/nickserver/couch_db/source.rb b/lib/nickserver/couch_db/source.rb index fffa76e..874fe4f 100644 --- a/lib/nickserver/couch_db/source.rb +++ b/lib/nickserver/couch_db/source.rb @@ -2,6 +2,7 @@ # This class allows querying couch for public keys. # require 'nickserver/couch_db/response' +require 'nickserver/config' module Nickserver::CouchDB class Source @@ -13,13 +14,17 @@ module Nickserver::CouchDB end def query(nick) - adapter.get VIEW, query: query_for(nick) do |status, body| - yield Response.new nick, status: status, body: body + adapter.get url, query: query_for(nick) do |status, body| + yield Response.new(nick, status: status, body: body) end end protected + def url + Nickserver::Config.couch_url + VIEW + end + def query_for(nick) { reduce: "false", key: "\"#{nick}\"" } end diff --git a/lib/nickserver/server.rb b/lib/nickserver/server.rb index cc6e3dd..9e1551b 100644 --- a/lib/nickserver/server.rb +++ b/lib/nickserver/server.rb @@ -92,20 +92,23 @@ module Nickserver end def get_key_from_uid(uid) - fetcher = if local_address?(uid) - Nickserver::Couch::FetchKey.new + if local_address?(uid) + @fetcher = Nickserver::Couch::FetchKey.new + @fetcher.get(uid) do |response| + send_response(status: response.status, content: response.content) + end else - Nickserver::Hkp::FetchKey.new + @fetcher = Nickserver::Hkp::FetchKey.new + @fetcher.get(uid).callback {|key| + yield key + }.errback {|status, msg| + if status == 404 + send_not_found + else + send_response(status: status, content: msg) + end + } end - fetcher.get(uid).callback {|key| - yield key - }.errback {|status, msg| - if status == 404 - send_not_found - else - send_response(status: status, content: msg) - end - } end def format_response(map) |