summaryrefslogtreecommitdiff
path: root/lib/nickserver/couch_db
diff options
context:
space:
mode:
authorAzul <azul@riseup.net>2016-05-25 13:13:30 +0200
committerAzul <azul@riseup.net>2016-05-25 14:13:30 +0200
commit8a81429f0eb8aa5041d47557d0c5b5359bb959e6 (patch)
tree10f5d3db69883c685408edc3365d1e762f13e322 /lib/nickserver/couch_db
parent5c0fa0fb7b10820f2956807cb457421bf1e00708 (diff)
copy over all files from rewritten attempt
I started a nickserver from scratch to implement the things that are independent of our choice of stack (eventmachine or other). This commit copies them over and tests both things in parallel.
Diffstat (limited to 'lib/nickserver/couch_db')
-rw-r--r--lib/nickserver/couch_db/response.rb51
-rw-r--r--lib/nickserver/couch_db/source.rb34
2 files changed, 85 insertions, 0 deletions
diff --git a/lib/nickserver/couch_db/response.rb b/lib/nickserver/couch_db/response.rb
new file mode 100644
index 0000000..c6afe03
--- /dev/null
+++ b/lib/nickserver/couch_db/response.rb
@@ -0,0 +1,51 @@
+require 'nickserver/couch_db'
+require 'json'
+
+module Nickserver::CouchDB
+ class Response
+
+ def initialize(nick, couch_response = {})
+ @nick = nick
+ @couch_status = couch_response[:status]
+ @json = JSON.load(couch_response[:body]) if couch_status == 200
+ end
+
+ def status
+ if ok? && empty? then 404
+ else couch_status
+ end
+ end
+
+ def content
+ key_response if ok? && !empty?
+ end
+
+ protected
+
+ def key_response
+ format address: nick.to_s, openpgp: key
+ end
+
+ def format(response)
+ response.to_json
+ end
+
+ def key
+ rows.first["value"]
+ end
+
+ def ok?
+ couch_status == 200
+ end
+
+ def empty?
+ rows.empty?
+ end
+
+ def rows
+ json["rows"]
+ end
+
+ attr_reader :couch_status, :json, :nick
+ end
+end
diff --git a/lib/nickserver/couch_db/source.rb b/lib/nickserver/couch_db/source.rb
new file mode 100644
index 0000000..fffa76e
--- /dev/null
+++ b/lib/nickserver/couch_db/source.rb
@@ -0,0 +1,34 @@
+#
+# This class allows querying couch for public keys.
+#
+require 'nickserver/couch_db/response'
+
+module Nickserver::CouchDB
+ class Source
+
+ VIEW = '/_design/Identity/_view/pgp_key_by_email'
+
+ def initialize(adapter)
+ @adapter = adapter
+ end
+
+ def query(nick)
+ adapter.get VIEW, query: query_for(nick) do |status, body|
+ yield Response.new nick, status: status, body: body
+ end
+ end
+
+ protected
+
+ def query_for(nick)
+ { reduce: "false", key: "\"#{nick}\"" }
+ end
+
+ def adapter
+ @adapter
+ # Nickserver::Adapters::Http.new(config)
+ end
+
+ attr_reader :config
+ end
+end