summaryrefslogtreecommitdiff
path: root/lib/nickserver/couch
diff options
context:
space:
mode:
authorazul <azul@riseup.net>2016-06-15 14:22:08 +0200
committerGitHub <noreply@github.com>2016-06-15 14:22:08 +0200
commit3c6dc3d7b902b46b82a3d8cd38bd3fa896024beb (patch)
tree559b3998465248138b6f24a76b6b3e55e60f3986 /lib/nickserver/couch
parent5cad637a4a2a3de6b95ff1204fc29174e18b3124 (diff)
parent93258bd6fe6247e7af67f423243eba9808e920ee (diff)
Merge pull request #3 from azul/refactor/transport-adapters
Refactor em specifics into http adapter
Diffstat (limited to 'lib/nickserver/couch')
-rw-r--r--lib/nickserver/couch/fetch_key.rb63
1 files changed, 0 insertions, 63 deletions
diff --git a/lib/nickserver/couch/fetch_key.rb b/lib/nickserver/couch/fetch_key.rb
deleted file mode 100644
index 3fe2a63..0000000
--- a/lib/nickserver/couch/fetch_key.rb
+++ /dev/null
@@ -1,63 +0,0 @@
-require 'em-http'
-require 'json'
-
-module Nickserver; module Couch
- class FetchKey
- include EM::Deferrable
-
- VIEW = "_design/Identity/_view/pgp_key_by_email"
-
- def initialize(options={})
- @timeout = 5
- end
-
- def get(uid)
- couch_request(uid)
- self
- 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
-
- 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"
- 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