Module: Enstratus::ResourceClassMethods

Defined in:
lib/enstratus/resource.rb

Instance Method Summary collapse

Instance Method Details

#[](id) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/enstratus/resource.rb', line 68

def [](id)
  s = self.new
  begin
    j = JSON.parse(self.connection({:id => id}).get)
    j[self.collection_name].first.each do |k,v|
      s.send("#{k.to_underscore}=", v)
    end
    return s
  rescue Exception => e
    puts e
  end
end

#all(filters = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/enstratus/resource.rb', line 51

def all(filters = {})
  all = []
  begin
    j = JSON.parse(self.connection(:filters => filters).get)
    j[self.collection_name].each do |x|
      s = self.new
      x.each do |k,v|
        s.send("#{k.to_underscore}=", v)
      end
      all << s
    end
    return all
  rescue Exception => e
    puts e
  end
end

#attribute(name, type = nil, read_only = true, required = true) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/enstratus/resource.rb', line 81

def attribute(name, type=nil, read_only=true, required=true)
  define_method(name) do
    @attributes[name]
  end

  define_method(:"#{name}=") do |val|
    @attributes[name] = val
  end
end

#collection_nameObject



31
32
33
34
# File 'lib/enstratus/resource.rb', line 31

def collection_name
  n = "#{self.name.split("::")[-1].downcase}s"
  @collection_name || n
end

#collection_name=(name) ⇒ Object



36
37
38
# File 'lib/enstratus/resource.rb', line 36

def collection_name=(name)
  @collection_name = name
end

#collects_as(name) ⇒ Object



40
41
42
# File 'lib/enstratus/resource.rb', line 40

def collects_as(name)
  self.collection_name = name.to_s
end

#connection(opts = {}) ⇒ Object



44
45
46
47
48
49
# File 'lib/enstratus/resource.rb', line 44

def connection(opts = {})
  opts[:id] ||= nil
  opts[:filters] ||= {}
  opts[:id].nil? ? path=url.dup : path="#{url.dup}/#{opts[:id]}"
  Enstratus::Request.new(path, opts[:filters])
end

#urlObject



26
27
28
29
# File 'lib/enstratus/resource.rb', line 26

def url
  top, parent, local = self.name.split("::")
  url = "#{parent.downcase}/#{local}"
end