Class: Helium::Resource
- Inherits:
-
Object
- Object
- Helium::Resource
- Extended by:
- Utils
- Includes:
- Utils
- Defined in:
- lib/helium/resource.rb
Overview
Abstract base class for Helium Resources returned by the API
Direct Known Subclasses
Configuration, DataPoint, DeviceConfiguration, Element, Label, Organization, Sensor, User
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.all(opts = {}) ⇒ Array<Resource>
Returns all resources.
- .all_path ⇒ Object
-
.create(attributes, opts = {}) ⇒ Resource
Creates a new resource with given attributes.
-
.find(id, opts = {}) ⇒ Resource
Finds a single Resource by id.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Override equality to use id for comparisons.
-
#as_json ⇒ Hash
Inheriting resources should implement this with super.
-
#created_at ⇒ DateTime?
When the resource was created.
-
#destroy ⇒ Boolean
Deletes the Resource.
-
#eql?(other) ⇒ Boolean
Override equality to use id for comparisons.
-
#hash ⇒ Integer
Override equality to use id for comparisons.
-
#initialize(opts = {}) ⇒ Resource
constructor
A new instance of Resource.
-
#resource_path ⇒ String
Returns a path identifying the current resource.
-
#to_json(*options) ⇒ String
A JSON-encoded String representing the resource.
-
#update(attributes) ⇒ Resource
Updates a Resource.
-
#updated_at ⇒ DateTime?
When the resource was last updated.
Methods included from Utils
Constructor Details
#initialize(opts = {}) ⇒ Resource
Returns a new instance of Resource.
7 8 9 10 11 12 13 14 15 |
# File 'lib/helium/resource.rb', line 7 def initialize(opts = {}) @client = opts.fetch(:client) @params = opts.fetch(:params) @id = @params["id"] @type = @params.dig('type') @created_at = @params.dig('meta', 'created') @updated_at = @params.dig('meta', 'updated') end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
4 5 6 |
# File 'lib/helium/resource.rb', line 4 def id @id end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
4 5 6 |
# File 'lib/helium/resource.rb', line 4 def params @params end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
4 5 6 |
# File 'lib/helium/resource.rb', line 4 def type @type end |
Class Method Details
.all(opts = {}) ⇒ Array<Resource>
Returns all resources
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/helium/resource.rb', line 27 def all(opts = {}) client = opts.fetch(:client) response = client.get(all_path) resources_data = JSON.parse(response.body)["data"] resources = resources_data.map do |resource_data| self.new(client: client, params: resource_data) end return resources end |
.all_path ⇒ Object
40 41 42 |
# File 'lib/helium/resource.rb', line 40 def all_path "/#{resource_name}" end |
.create(attributes, opts = {}) ⇒ Resource
Creates a new resource with given attributes
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/helium/resource.rb', line 61 def create(attributes, opts = {}) client = opts.fetch(:client) path = "/#{resource_name}" body = { data: { attributes: attributes, type: resource_name } } response = client.post(path, body: body) resource_data = JSON.parse(response.body)["data"] return self.new(client: client, params: resource_data) end |
.find(id, opts = {}) ⇒ Resource
Finds a single Resource by id
48 49 50 51 52 53 54 55 |
# File 'lib/helium/resource.rb', line 48 def find(id, opts = {}) client = opts.fetch(:client) response = client.get("/#{resource_name}/#{id}") resource_data = JSON.parse(response.body)["data"] return self.new(client: client, params: resource_data) end |
Instance Method Details
#==(other) ⇒ Boolean
Override equality to use id for comparisons
120 121 122 |
# File 'lib/helium/resource.rb', line 120 def ==(other) self.id == other.id end |
#as_json ⇒ Hash
Inheriting resources should implement this with super
150 151 152 153 154 155 156 157 |
# File 'lib/helium/resource.rb', line 150 def as_json { id: id, type: type, created_at: created_at, updated_at: updated_at } end |
#created_at ⇒ DateTime?
Returns when the resource was created.
137 138 139 140 |
# File 'lib/helium/resource.rb', line 137 def created_at return nil if @created_at.nil? @_created_at ||= DateTime.parse(@created_at) end |
#destroy ⇒ Boolean
Deletes the Resource
113 114 115 116 |
# File 'lib/helium/resource.rb', line 113 def destroy path = "/#{resource_name}/#{self.id}" @client.delete(path) end |
#eql?(other) ⇒ Boolean
Override equality to use id for comparisons
126 127 128 |
# File 'lib/helium/resource.rb', line 126 def eql?(other) self == other end |
#hash ⇒ Integer
Override equality to use id for comparisons
132 133 134 |
# File 'lib/helium/resource.rb', line 132 def hash id.hash end |
#resource_path ⇒ String
Returns a path identifying the current resource. Can be overridden in child classes to handle non-standard resources (e.g. Organization)
89 90 91 |
# File 'lib/helium/resource.rb', line 89 def resource_path "/#{resource_name}/#{self.id}" end |
#to_json(*options) ⇒ String
Returns a JSON-encoded String representing the resource.
160 161 162 |
# File 'lib/helium/resource.rb', line 160 def to_json(*) as_json.to_json(*) end |
#update(attributes) ⇒ Resource
Updates a Resource
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/helium/resource.rb', line 96 def update(attributes) body = { data: { attributes: attributes, id: self.id, type: resource_name } } response = @client.patch(resource_path, body: body) resource_data = JSON.parse(response.body)["data"] return self.class.new(client: self, params: resource_data) end |
#updated_at ⇒ DateTime?
Returns when the resource was last updated.
143 144 145 146 |
# File 'lib/helium/resource.rb', line 143 def updated_at return nil if @updated_at.nil? @_updated_at ||= DateTime.parse(@updated_at) end |