Class: JsonTail::CouchServer

Inherits:
Object
  • Object
show all
Defined in:
lib/json_tail/couchdb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, database, username, password) ⇒ CouchServer

Returns a new instance of CouchServer.



7
8
9
10
11
12
13
# File 'lib/json_tail/couchdb.rb', line 7

def initialize(host, port, database, username, password)
  @host = host
  @port = port
  @database = database
  @username = username
  @password = password
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



5
6
7
# File 'lib/json_tail/couchdb.rb', line 5

def database
  @database
end

Instance Method Details

#delete(uri) ⇒ Object



15
16
17
# File 'lib/json_tail/couchdb.rb', line 15

def delete(uri)
  send_request(Net::HTTP::Delete.new(uri))
end

#get(uri) ⇒ Object



19
20
21
# File 'lib/json_tail/couchdb.rb', line 19

def get(uri)
  send_request(Net::HTTP::Get.new(uri))
end

#post(uri, json) ⇒ Object



30
31
32
33
34
35
# File 'lib/json_tail/couchdb.rb', line 30

def post(uri, json)
  req = Net::HTTP::Post.new(uri)
  req["content-type"] = "application/json"
  req.body = json
  send_request(req)
end

#put(uri, json) ⇒ Object



23
24
25
26
27
28
# File 'lib/json_tail/couchdb.rb', line 23

def put(uri, json)
  req = Net::HTTP::Put.new(uri)
  req["content-type"] = "application/json"
  req.body = json
  send_request(req)
end

#send_request(req) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/json_tail/couchdb.rb', line 37

def send_request(req)
  begin
    res = Net::HTTP.start(@host, @port) do |http|
      req.basic_auth @username, @password
      http.request(req)
    end
  rescue Errno::ECONNREFUSED => e
    puts "couchdb: " + e
    exit
  end
end