summaryrefslogtreecommitdiff
path: root/lib/nickserver/couch_db/response.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/nickserver/couch_db/response.rb')
-rw-r--r--lib/nickserver/couch_db/response.rb51
1 files changed, 51 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