diff options
Diffstat (limited to 'lib/nickserver')
-rw-r--r-- | lib/nickserver/hkp/fetch_key.rb | 29 | ||||
-rw-r--r-- | lib/nickserver/hkp/source.rb | 16 |
2 files changed, 24 insertions, 21 deletions
diff --git a/lib/nickserver/hkp/fetch_key.rb b/lib/nickserver/hkp/fetch_key.rb index fe00473..10c4926 100644 --- a/lib/nickserver/hkp/fetch_key.rb +++ b/lib/nickserver/hkp/fetch_key.rb @@ -1,4 +1,6 @@ require 'em-http' +require 'nickserver/response' +require 'nickserver/hkp/response' # # Fetch keys via HKP @@ -8,35 +10,36 @@ require 'em-http' module Nickserver; module Hkp class FetchKey - include EM::Deferrable - def get(uid) - FetchKeyInfo.new.search(uid).callback {|key_info_list| + def initialize(adapter) + @adapter = adapter + end + + def get(nick, &block) + FetchKeyInfo.new.search(nick).callback {|key_info_list| best = pick_best_key(key_info_list) - get_key_by_fingerprint(best.keyid) {|key| - self.succeed key - } + get_key_by_fingerprint(nick, best.keyid, &block) }.errback {|status, msg| - self.fail status, msg + yield Nickserver::Response.new(status, msg) } - self end # # fetches ascii armored OpenPGP public key from the keyserver # - def get_key_by_fingerprint(key_id) + def get_key_by_fingerprint(nick, key_id) params = {op: 'get', search: "0x" + key_id, exact: 'on', options: 'mr'} http = EventMachine::HttpRequest.new(Config.hkp_url).get(query: params) http.callback { - if http.response_header.status != 200 - self.fail http.response_header.status, "HKP Request failed" + status = http.response_header.status + if status != 200 + yield Nickserver::Response.new status, "HKP Request failed" else - yield http.response + yield Response.new nick, http.response end } http.errback { - self.fail 500, http.error + yield Nickserver::Response.new 500, http.error } end diff --git a/lib/nickserver/hkp/source.rb b/lib/nickserver/hkp/source.rb index 79333f0..f06a931 100644 --- a/lib/nickserver/hkp/source.rb +++ b/lib/nickserver/hkp/source.rb @@ -1,20 +1,20 @@ -require 'nickserver/hkp/response' module Nickserver::Hkp class Source def initialize(adapter) + @adapter = adapter end - def query(nick) - fetcher.get(nick).callback {|key| - yield Response.new(nick, key) - }.errback {|status, msg| - yield Nickserver::Response.new(status, msg) - } + def query(nick, &block) + fetcher.get(nick, &block) end + protected + + attr_reader :adapter + def fetcher - Nickserver::Hkp::FetchKey.new + Nickserver::Hkp::FetchKey.new(adapter) end end end |