-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocument.rb
More file actions
66 lines (52 loc) · 1.38 KB
/
document.rb
File metadata and controls
66 lines (52 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require "#{File.dirname(__FILE__)}/connection.rb"
module OpenData
class Document
include OpenData::Connection
def initialize(document, collection, options={})
@database = options[:database] ||= nil
@collection = collection
@path = generate_path
build_document(document)
end
def save
document = build_attributes
options = {
path: @path,
body: document
}
request = post(options)
end
private
def build_document(document)
if document.is_a? Hash
document.each do |attribute, value|
self.class.send(:attr_accessor, attribute)
instance_variable_set("@#{attribute}", value)
end
else
raise "Requires a Hash"
end
end
def build_attributes
exclude_attribute = %w[database collection path]
attributes = {}
instance_variables.each do |variable|
name = variable.to_s
name = name[1..name.size]
value = instance_variable_get variable
attributes[name] = value
end
exclude_attribute.each do |attribute|
attributes.delete(attribute)
end
attributes
end
def generate_path
if @database
"databases/#{@database}/collections/#{@collection}/documents"
else
"collections/#{@collection}/documents"
end
end
end
end