Class: Helium::Resource
- Inherits:
-
Object
- Object
- Helium::Resource
- Defined in:
- lib/helium/resource.rb
Overview
Abstract base class for Helium Resources returned by the API
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
-
.all(opts = {}) ⇒ Array<Resource>
Returns all resources.
-
.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.
-
#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.
Constructor Details
#initialize(opts = {}) ⇒ Resource
Returns a new instance of Resource.
6 7 8 9 10 11 12 13 |
# File 'lib/helium/resource.rb', line 6 def initialize(opts = {}) @client = opts.fetch(:client) @params = opts.fetch(:params) @id = @params["id"] @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 |
Class Method Details
.all(opts = {}) ⇒ Array<Resource>
Returns all resources
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/helium/resource.rb', line 23 def all(opts = {}) client = opts.fetch(:client) response = client.get("/#{resource_name}") 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 |
.create(attributes, opts = {}) ⇒ Resource
Creates a new resource with given attributes
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/helium/resource.rb', line 53 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
40 41 42 43 44 45 46 47 |
# File 'lib/helium/resource.rb', line 40 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
107 108 109 |
# File 'lib/helium/resource.rb', line 107 def ==(other) self.id == other.id end |
#as_json ⇒ Hash
Inheriting resources should implement this with super
137 138 139 140 141 142 143 |
# File 'lib/helium/resource.rb', line 137 def as_json { id: id, created_at: created_at, updated_at: updated_at } end |
#created_at ⇒ DateTime?
Returns when the resource was created.
124 125 126 127 |
# File 'lib/helium/resource.rb', line 124 def created_at return nil if @created_at.nil? @_created_at ||= DateTime.parse(@created_at) end |
#destroy ⇒ Boolean
Deletes the Resource
100 101 102 103 |
# File 'lib/helium/resource.rb', line 100 def destroy path = "/#{resource_name}/#{self.id}" @client.delete(path) end |
#eql?(other) ⇒ Boolean
Override equality to use id for comparisons
113 114 115 |
# File 'lib/helium/resource.rb', line 113 def eql?(other) self == other end |
#hash ⇒ Integer
Override equality to use id for comparisons
119 120 121 |
# File 'lib/helium/resource.rb', line 119 def hash id.hash end |
#to_json(*options) ⇒ String
Returns a JSON-encoded String representing the resource.
146 147 148 |
# File 'lib/helium/resource.rb', line 146 def to_json(*) as_json.to_json(*) end |
#update(attributes) ⇒ Resource
Updates a Resource
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/helium/resource.rb', line 81 def update(attributes) path = "/#{resource_name}/#{self.id}" body = { data: { attributes: attributes, id: self.id, type: resource_name } } response = @client.patch(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.
130 131 132 133 |
# File 'lib/helium/resource.rb', line 130 def updated_at return nil if @updated_at.nil? @_updated_at ||= DateTime.parse(@updated_at) end |