Class: CouchProxy::Rack::DesignDoc

Inherits:
Base
  • Object
show all
Defined in:
lib/couchproxy/rack/design_doc.rb

Constant Summary collapse

QUERY =
/_view\/.+$/
INFO =
/\/_info$/
VIEW_NAME =
/_view\/(.*)$/
REDUCE_ERROR =
'{"error":"query_parse_error","reason":"Invalid URL parameter `reduce` for map view."}'.freeze

Constants inherited from Base

Base::APPLICATION_JSON, Base::DESIGN_ID, Base::INVALID_JSON, Base::METHODS, Base::SERVER_VERSION, Base::TEXT_PLAIN

Instance Attribute Summary

Attributes inherited from Base

#cluster, #request

Instance Method Summary collapse

Methods inherited from Base

#initialize, #method_missing, #proxy_to, #proxy_to_all_nodes, #proxy_to_all_partitions, #proxy_to_any_node, #proxy_to_any_partition, #replicate_to_all_partitions, #rewrite_location, #send_error_response, #send_response, #uuids

Constructor Details

This class inherits a constructor from CouchProxy::Rack::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class CouchProxy::Rack::Base

Instance Method Details

#deleteObject



61
62
63
64
65
66
67
68
69
# File 'lib/couchproxy/rack/design_doc.rb', line 61

def delete
  proxy_to_all_partitions do |responses|
    head = response_headers.tap do |h|
      h['ETag'] = responses.first.response_header.etag
    end
    send_response(responses.first.response_header.status,
      head, responses.first.response)
  end
end

#getObject



11
12
13
14
15
16
17
# File 'lib/couchproxy/rack/design_doc.rb', line 11

def get
  case request.path_info
    when QUERY then query
    when INFO  then info
    else proxy_to_any_partition
  end
end

#headObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/couchproxy/rack/design_doc.rb', line 19

def head
  case request.path_info
    when QUERY
      proxy_to_all_partitions do |responses|
        etags = responses.map {|r| r.response_header.etag }
        head = response_headers.tap do |h|
          h['ETag'] = etag(etags)
        end
        send_response(responses.first.response_header.status, head, [])
      end
    else proxy_to_any_partition
  end
end

#postObject



33
34
35
# File 'lib/couchproxy/rack/design_doc.rb', line 33

def post
  # FIXME same as get, but body can have keys in it
end

#putObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/couchproxy/rack/design_doc.rb', line 37

def put
  partition = cluster.any_partition
  request.rewrite_proxy_url!(partition.num)
  uri = "#{partition.node.uri}#{@request.fullpath}"
  http = EM::HttpRequest.new(uri).put(:head => proxy_headers,
    :body => request.content)
  http.callback do |res|
    head = response_headers
    sender = proc do
      send_response(res.response_header.status, head, [res.response])
    end
    if success?(res)
      head.tap do |h|
        h['ETag'] = res.response_header.etag
        h['Location'] = rewrite_location(res.response_header.location)
      end
      replicate_to_all_partitions(partition, request.doc_id, &sender)
    else
      sender.call
    end
  end
  http.errback { send_error_response }
end