Class: DanarchyCouchDB::Connection

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

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Connection

Returns a new instance of Connection.



8
9
10
11
12
13
# File 'lib/danarchy_couchdb.rb', line 8

def initialize(connection)
  @connection = connection
  uri = 'http' if !@connection[:ssl]
  uri = 'https' if @connection[:ssl]
  @uri = "#{uri}://#{connection[:hostname]}:#{connection[:port]}"
end

Instance Method Details

#delete(*args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/danarchy_couchdb.rb', line 64

def delete(*args)
  database = args.shift
  document = args.shift
  revision = args.shift
  
  uri  = "#{@uri}/#{database}"
  uri += "/#{document}"      if document
  uri += "/?rev=#{revision}" if revision

  uri = URI(uri)
  JSON.parse(request(Net::HTTP::Delete.new(uri)), symbolize_names: true)
end

#delete_attachment(*args) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/danarchy_couchdb.rb', line 77

def delete_attachment(*args)
  database   = args.shift
  document   = args.shift
  revision   = args.shift
  attachment = args.shift
  
  uri = URI("#{@uri}/#{database}/#{document}/#{attachment}?rev=#{revision}")
  JSON.parse(request(Net::HTTP::Delete.new(uri)), symbolize_names: true)
end

#get(*args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/danarchy_couchdb.rb', line 15

def get(*args)
  database = args.shift
  document = args.shift
  revision = args.shift
  
  uri  = "#{@uri}/#{database}"
  uri += "/#{document}"        if document
  uri += "/?rev=#{revision}"   if revision
  uri = URI(uri)

  response = Net::HTTP::Get.new(uri)
  JSON.parse(request(response), symbolize_names: true)
end

#get_attachment(database, document, attachment) ⇒ Object



29
30
31
32
# File 'lib/danarchy_couchdb.rb', line 29

def get_attachment(database, document, attachment)
  uri = URI("#{@uri}/#{database}/#{document}/#{attachment}")
  request(Net::HTTP::Get.new(uri))
end

#put(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/danarchy_couchdb.rb', line 34

def put(*args)
  database = args.shift
  document = args.shift
  data     = args.shift
  
  uri  = "#{@uri}/#{database}"
  uri += "/#{document}" if document
  uri  = URI(uri)

  req = Net::HTTP::Put.new(uri)
  req["content-type"] = "application/json"
  req.body = data.to_json if data
  JSON.parse(request(req), symbolize_names: true)
end

#put_attachment(*args) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/danarchy_couchdb.rb', line 49

def put_attachment(*args)
  database   = args.shift
  document   = args.shift
  revision   = args.shift
  attachment = args.shift
  data       = args.shift
  type       = args.shift

  uri = URI("#{@uri}/#{database}/#{document}/#{attachment}?rev=#{revision}")
  req = Net::HTTP::Put.new(uri)
  req["content-type"] = "#{type}"
  req.body = data if data
  JSON.parse(request(req), symbolize_names: true)
end