Class: FbResource::AbstractResource

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

Direct Known Subclasses

Device, Plants, Schedules, Sequences

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ AbstractResource

Returns a new instance of AbstractResource.



16
17
18
# File 'lib/resources/abstract_resource.rb', line 16

def initialize(conf)
  @conf = conf
end

Class Attribute Details

.pathObject



8
9
10
11
# File 'lib/resources/abstract_resource.rb', line 8

def path
  @path or
  raise "Set a resource path on your resource class via self.path="
end

Instance Attribute Details

#confObject

Returns the value of attribute conf.



14
15
16
# File 'lib/resources/abstract_resource.rb', line 14

def conf
  @conf
end

Instance Method Details

#allObject



40
41
42
# File 'lib/resources/abstract_resource.rb', line 40

def all
  @all || fetch
end

#fetchObject



20
21
22
23
24
25
# File 'lib/resources/abstract_resource.rb', line 20

def fetch
    Http
      .get(conf.url + self.class.path, conf.http_headers)
      .no { |obj, req, res| fetch_no(obj, req, res) }
      .ok { |obj, req, res| fetch_ok(obj, req, res) }
end

#fetch_no(obj, req, res) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
# File 'lib/resources/abstract_resource.rb', line 27

def fetch_no(obj, req, res)
  raise FetchError, """\n
  Error Fetching Farmbot resource:\n
  #{obj || "\n"}\n
  #{req && req.url}\n
  #{res && res.message}\n
  #{res && res.class}\n"""
end

#fetch_ok(obj, request, response) ⇒ Object



36
37
38
# File 'lib/resources/abstract_resource.rb', line 36

def fetch_ok(obj, request, response)
  @all = obj
end