blob: 427ac92a85d58a6e0ea0b9086dcb1e465e04d47a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
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
if ok? && !empty?
key_response
else
not_found_response
end
end
protected
def key_response
format keys.merge(address: nick.to_s)
end
def not_found_response
format({})
end
def format(response)
response.to_json
end
def keys
rows.first['value'].map do |k,v|
if k == 'pgp'
# created through webapps deprecated API
['openpgp', v]
else
[k, v['value']]
end
end.to_h
end
def ok?
couch_status == 200
end
def empty?
rows.empty? || keys.empty?
end
def rows
json['rows']
end
attr_reader :couch_status, :json, :nick
end
end
|