Class: CouchClient::Design

Inherits:
Object
  • Object
show all
Defined in:
lib/couch-client/design.rb

Overview

Design is the interface used to interact with design documents in order make view, show, list and fulltext requests.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, connection) ⇒ Design

Design is constructed with an id of the design documemnt and a connection that is used to make HTTP requests to the server.



15
16
17
18
# File 'lib/couch-client/design.rb', line 15

def initialize(id, connection)
  @id = id
  @connection = connection
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



11
12
13
# File 'lib/couch-client/design.rb', line 11

def id
  @id
end

Instance Method Details

#fulltext(name, options = {}) ⇒ Object

Makes requests to the server that return lucene search results.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/couch-client/design.rb', line 70

def fulltext(name, options = {})
  path = ["_fti", "_design", id, name]
  verb = :get
  
  # Options may be a Hash or a String.  Hashes are used for fulltext queries,
  # while String are used for administration operations (such as optimizing).
  if [String, Symbol].include?(options.class)
    path << "_#{options}"
    verb =  :post
    options = {}
  end
  
  code, body = @connection.hookup.send(verb, path, options)

  case code
  when 200
    if body["rows"]
      # Return a serch result if a query was provided.
      Collection.new(code, body, self)
    else
      # Return a status hash if a query was not provided.
      body
    end
  when 202
    true # Return true when administration operations are successfully performed.
  else
    case body["reason"]
    when "no_such_view"
      # Raise an error if a fulltext function was not found.
      raise FullTextNotFound.new("could not find fulltext field '#{name}' for design '#{id}'")
    when "bad_request"
      # Raise an error if a request was not formated properly (i.e. is bad).
      raise FullTextRequestBad.new("bad request made for fulltext field '#{name}' for design '#{id}'")
    else
      # Also raise an error if something else happens 
      raise Error.new("code: #{code}, error: #{body["error"]}, reason: #{body["reason"]}")
    end
  end
end

#inspectObject



110
111
112
# File 'lib/couch-client/design.rb', line 110

def inspect
  "#<#{self.class}: id: #{@id}>"
end

#list(name, document_id, view_name, options = {}) ⇒ Object

Makes requests to the server that list objects.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/couch-client/design.rb', line 54

def list(name, document_id, view_name, options = {})
  code, body = @connection.hookup.get(["_design", id, "_list", name, document_id, view_name], options)

  case code
  when 200
    body.is_a?(Hash) ? ConsistentHash.new(body) : body
  when 404
    # Raise an error if nothing was found
    raise ViewNotFound.new("could not find list field '#{name}' for design '#{id}'")
  else
    # Also raise an error if something else happens
    raise Error.new("code: #{code}, error: #{body["error"]}, reason: #{body["reason"]}")
  end
end

#show(name, document_id, options = {}) ⇒ Object

Makes requests to the server that return show objects.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/couch-client/design.rb', line 38

def show(name, document_id, options = {})
  code, body = @connection.hookup.get(["_design", id, "_show", name, document_id], options, nil)

  case code
  when 200
    body.is_a?(Hash) ? ConsistentHash.new(body) : body
  when 404
    # Raise an error if nothing was found
    raise ViewNotFound.new("could not find show field '#{name}' for design '#{id}'")
  else
    # Also raise an error if something else happens
    raise Error.new("code: #{code}, error: #{body["error"]}, reason: #{body["reason"]}")
  end
end

#view(name, options = {}) ⇒ Object

Makes requests to the server that return mappped/reduced view collections.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/couch-client/design.rb', line 21

def view(name, options = {})
  code, body = @connection.hookup.get(["_design", id, "_view", name], options)

  case code
  when 200
    # Return a Collection if results were found
    Collection.new(code, body, @connection)
  when 404
    # Raise an error if nothing was found
    raise ViewNotFound.new("could not find view field '#{name}' for design '#{id}'")
  else
    # Also raise an error if something else happens
    raise Error.new("code: #{code}, error: #{body["error"]}, reason: #{body["reason"]}")
  end
end