class Helium::Resource
Abstract base class for Helium
Resources returned by the API
Attributes
Public Class Methods
# File lib/helium/resource.rb, line 26 def all(opts = {}) client = opts.fetch(:client) Collection.new(klass: self, client: client).all end
The resource's index API route @return [String] path to resource's index
# File lib/helium/resource.rb, line 22 def all_path "/#{resource_name}" end
Creates a new resource with given attributes @param attributes [Hash] The attributes for the new Resource
@option opts [Client] :client A Helium::Client
@return [Resource]
# File lib/helium/resource.rb, line 52 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
Finds a single Resource
by id @param id [String] An id to find the Resource
@option opts [Client] :client A Helium::Client
@return [Resource]
# File lib/helium/resource.rb, line 35 def find(id, opts = {}) client = opts.fetch(:client) initialize_from_path(path: "/#{resource_name}/#{id}", client: client) end
# File lib/helium/resource.rb, line 74 def initialize_from_path(opts = {}) client = opts.fetch(:client) path = opts.fetch(:path) response = client.get(path) resource_data = JSON.parse(response.body)["data"] return self.new(client: client, params: resource_data) end
# 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
# File lib/helium/resource.rb, line 70 def resource_name kebab_case(self.name.split('::').last) end
Fetches a singleton resource (e.g. organization, user) @option opts [Client] :client A Helium::Client
@return [Resource] A singleton resource
# File lib/helium/resource.rb, line 43 def singleton(opts = {}) client = opts.fetch(:client) initialize_from_path(path: all_path, client: client) end
Public Instance Methods
Override equality to use id for comparisons @return [Boolean]
# File lib/helium/resource.rb, line 121 def ==(other) self.id == other.id end
Inheriting resources should implement this with super @return [Hash] a Hash of the object's attributes for JSON
# File lib/helium/resource.rb, line 151 def as_json { id: id, type: type, created_at: created_at, updated_at: updated_at } end
@return [DateTime, nil] when the resource was created
# File lib/helium/resource.rb, line 138 def created_at return nil if @created_at.nil? @_created_at ||= DateTime.parse(@created_at) end
Deletes the Resource
@return [Boolean] Whether the operation was successful
# File lib/helium/resource.rb, line 111 def destroy @client.delete(resource_path) end
Override equality to use id for comparisons @return [Boolean]
# File lib/helium/resource.rb, line 127 def eql?(other) self == other end
Override equality to use id for comparisons @return [Integer]
# File lib/helium/resource.rb, line 133 def hash id.hash end
# File lib/helium/resource.rb, line 115 def metadata Metadata.new(client: @client, klass: self) end
# File lib/helium/resource.rb, line 165 def resource_name kebab_case(self.class.name.split('::').last) end
Returns a path identifying the current resource. Can be overridden in child classes to handle non-standard resources (e.g. Organization
) @return [String] path to resource
# File lib/helium/resource.rb, line 87 def resource_path "/#{resource_name}/#{self.id}" end
@return [String] a JSON-encoded String representing the resource
# File lib/helium/resource.rb, line 161 def to_json(*options) as_json.to_json(*options) end
Updates a Resource
@param attributes [Hash] The attributes to update @return [Resource] The updated resource
# File lib/helium/resource.rb, line 94 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: @client, params: resource_data) end
@return [DateTime, nil] when the resource was last updated
# File lib/helium/resource.rb, line 144 def updated_at return nil if @updated_at.nil? @_updated_at ||= DateTime.parse(@updated_at) end