Class: Cushion::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/cushion/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server

Initializes a Cushion::Server.



8
9
10
11
# File 'lib/cushion/server.rb', line 8

def initialize(options = {})
  @uri = options[:uri] || DEFAULT_COUCH_HOST
  @uuid_batch_count = options[:uuid_batch_count] || 1000
end

Instance Attribute Details

#uriObject

Returns the value of attribute uri.



5
6
7
# File 'lib/cushion/server.rb', line 5

def uri
  @uri
end

#uuid_batch_countObject

Returns the value of attribute uuid_batch_count.



5
6
7
# File 'lib/cushion/server.rb', line 5

def uuid_batch_count
  @uuid_batch_count
end

Instance Method Details

#[](name) ⇒ Object

Returns a Cushion::Database identified by name. This method does not attempt to create the database if it does not already exist on the server.



76
77
78
# File 'lib/cushion/server.rb', line 76

def [](name)
  db(name)
end

#active_tasks(headers = {}) ⇒ Object

Retrieves a list of running tasks on the server.



19
20
21
# File 'lib/cushion/server.rb', line 19

def active_tasks(headers = {})
  get("_active_tasks", headers)
end

#all_dbs(headers = {}) ⇒ Object

Retrieves a list of all databases on the server.



24
25
26
# File 'lib/cushion/server.rb', line 24

def all_dbs(headers = {})
  get("_all_dbs", headers)
end

#config(headers = {}) ⇒ Object

Fetches the configuration information for this server.



29
30
31
# File 'lib/cushion/server.rb', line 29

def config(headers = {})
  get("_config", headers)
end

#copy(source, destination, headers = {}) ⇒ Object

Issues a COPY request to the CouchDB server, copying a document from source to destination. Returns a parsed response body if the accept option is set to ‘application/json’ (the default), otherwise returns the raw RestClient response body.



144
145
146
147
# File 'lib/cushion/server.rb', line 144

def copy(source, destination, headers = {})
  defaults = { :accept => "application/json", 'Destination' => destination }
  parse_response(RestClient.copy("#{@uri}/#{source}", defaults.merge(headers)))
end

#create(name, headers = {}) ⇒ Object

Creates a database on this server with the provided name.



56
57
58
# File 'lib/cushion/server.rb', line 56

def create(name, headers = {})
  db(name).create(headers)
end

#db(name) ⇒ Object Also known as: database

Returns a Cushion::Database identified by name. This method does not attempt to create the database if it does not already exist on the server.



83
84
85
# File 'lib/cushion/server.rb', line 83

def db(name)
  Cushion::Database.new(name, :server => self)
end

#db!(name) ⇒ Object Also known as: database!

Creates a database on this server with the provided name if it doesn’t already exist. Returns a Cushion::Database.



90
91
92
93
94
# File 'lib/cushion/server.rb', line 90

def db!(name)
  ndb = db(name)
  ndb.create rescue nil
  ndb
end

#delete(path, headers = {}) ⇒ Object

Issues a DELETE request to the CouchDB server. Returns a parsed response body if the accept option is set to ‘application/json’ (the default), otherwise returns the raw RestClient response body.



135
136
137
138
# File 'lib/cushion/server.rb', line 135

def delete(path, headers = {})
  defaults = { :accept => "application/json" }
  parse_response(RestClient.delete("#{@uri}/#{path}", defaults.merge(headers)))
end

#drop(name, headers = {}) ⇒ Object

Deletes the database identified by name from this server.



61
62
63
# File 'lib/cushion/server.rb', line 61

def drop(name, headers = {})
  db(name).drop(headers)
end

#get(path, headers = {}) ⇒ Object

Issues a GET request to the CouchDB server. Returns a parsed response body if the accept option is set to ‘application/json’ (the default), otherwise returns the raw RestClient response body.



105
106
107
108
# File 'lib/cushion/server.rb', line 105

def get(path, headers = {})
  defaults = { :accept => "application/json" }
  parse_response(RestClient.get("#{@uri}/#{path}", defaults.merge(headers)))
end

#head(path, headers = {}) ⇒ Object

Issues a HEAD request to the CouchDB server.



98
99
100
# File 'lib/cushion/server.rb', line 98

def head(path, headers = {})
  RestClient.head("#{@uri}/#{path}", headers).headers
end

#info(headers = {}) ⇒ Object

Retrieves the server’s welcome message.



14
15
16
# File 'lib/cushion/server.rb', line 14

def info(headers = {})
  get('', headers)
end

#move(source, destination, headers = {}) ⇒ Object

Issues a MOVE request to the CouchDB server, moving a document from source to destination. Returns a parsed response body if the accept option is set to ‘application/json’ (the default), otherwise returns the raw RestClient response body.



153
154
155
156
# File 'lib/cushion/server.rb', line 153

def move(source, destination, headers = {})
  defaults = { :accept => "application/json", 'Destination' => destination }
  parse_response(RestClient.move("#{@uri}/#{source}", defaults.merge(headers)))
end

#next_uuid(count = @uuid_batch_count) ⇒ Object

Retrives the next unused UUID from CouchDB. Provide a count to set the number of UUIDs cached by this server instance.



179
180
181
182
183
184
185
# File 'lib/cushion/server.rb', line 179

def next_uuid(count = @uuid_batch_count)
  @uuids ||= []
  if @uuids.empty?
    @uuids = get("_uuids?count=#{count}")["uuids"]
  end
  @uuids.pop
end

#open_attachment(path) ⇒ Object

Opens the attachment located at path. Returns an OpenURI IO object.



171
172
173
174
175
# File 'lib/cushion/server.rb', line 171

def open_attachment(path)
  open("#{@uri}/#{path}")
rescue OpenURI::HTTPError
  raise RestClient::ResourceNotFound
end

#post(path, body, headers = {}) ⇒ Object

Issues a POST request to the CouchDB server. Parses the request body if the content_type option is set to ‘application/json’ (the default). Returns a parsed response body if the accept option is set to ‘application/json’ (the default), otherwise returns the raw RestClient response body.



115
116
117
118
119
# File 'lib/cushion/server.rb', line 115

def post(path, body, headers = {})
  defaults = { :accept => "application/json", :content_type => "application/json" }
  opts = defaults.merge(headers)
  parse_response(RestClient.post("#{@uri}/#{path}", construct_payload(body, opts), opts))
end

#put(path, body, headers = {}) ⇒ Object

Issues a PUT request to the CouchDB server. Parses the request body if the content_type option is set to ‘application/json’ (the default). Returns a parsed response body if the accept option is set to ‘application/json’ (the default), otherwise returns the raw RestClient response body.



126
127
128
129
130
# File 'lib/cushion/server.rb', line 126

def put(path, body, headers = {})
  defaults = { :accept => "application/json", :content_type => "application/json" }
  opts = defaults.merge(headers)
  parse_response(RestClient.put("#{@uri}/#{path}", construct_payload(body, opts), opts))
end

#recreate(name) ⇒ Object

Deletes and re-creates the database identified by name. Returns a Cushion::Database.



67
68
69
70
71
# File 'lib/cushion/server.rb', line 67

def recreate(name)
  rdb = db(name)
  rdb.recreate
  rdb
end

#replicate(source, target, headers = {}) ⇒ Object

Replicates source database to target database. source and target may be either local database names (for local replication) or fully qualified urls (for remote replication).



41
42
43
# File 'lib/cushion/server.rb', line 41

def replicate(source, target, headers = {})
  post("_replicate", {:source => source.to_s, :target => target.to_s}, headers)
end

#request(verb, path, params = {}) ⇒ Object

Issues a generic request to the server path with the method set to verb. Accepts body and headers options.



160
161
162
163
164
165
166
167
# File 'lib/cushion/server.rb', line 160

def request(verb, path, params = {})
  RestClient::Request.execute(
    :method => verb,
    :url => "#{@uri}/#{path}",
    :payload => params[:body],
    :headers => params[:headers]
  )
end

#restart(headers = {}) ⇒ Object

Restarts the server.



46
47
48
# File 'lib/cushion/server.rb', line 46

def restart(headers = {})
  post("_restart", nil, headers)
end

#set_config(section, option, value) ⇒ Object

Sets a configuration option identified by section and option.



34
35
36
# File 'lib/cushion/server.rb', line 34

def set_config(section, option, value)
  put("_config/#{section}/#{option}", value, :accept => "text/plain")
end

#stats(headers = {}) ⇒ Object

Fetches stats information for this server.



51
52
53
# File 'lib/cushion/server.rb', line 51

def stats(headers = {})
  get("_stats", headers)
end