class Helium::Resource

Abstract base class for Helium Resources returned by the API

Attributes

id[R]
params[R]
type[R]

Public Class Methods

all(opts = {}) click to toggle source
# File lib/helium/resource.rb, line 26
def all(opts = {})
  client = opts.fetch(:client)
  Collection.new(klass: self, client: client).all
end
all_path() click to toggle source

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
create(attributes, opts = {}) click to toggle source

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
find(id, opts = {}) click to toggle source

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
initialize_from_path(opts = {}) click to toggle source
# 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
new(opts = {}) click to toggle source
# 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
resource_name() click to toggle source
# File lib/helium/resource.rb, line 70
def resource_name
  kebab_case(self.name.split('::').last)
end
singleton(opts = {}) click to toggle source

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

==(other) click to toggle source

Override equality to use id for comparisons @return [Boolean]

# File lib/helium/resource.rb, line 121
def ==(other)
  self.id == other.id
end
as_json() click to toggle source

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
created_at() click to toggle source

@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
destroy() click to toggle source

Deletes the Resource @return [Boolean] Whether the operation was successful

# File lib/helium/resource.rb, line 111
def destroy
  @client.delete(resource_path)
end
eql?(other) click to toggle source

Override equality to use id for comparisons @return [Boolean]

# File lib/helium/resource.rb, line 127
def eql?(other)
  self == other
end
hash() click to toggle source

Override equality to use id for comparisons @return [Integer]

# File lib/helium/resource.rb, line 133
def hash
  id.hash
end
metadata() click to toggle source
# File lib/helium/resource.rb, line 115
def metadata
  Metadata.new(client: @client, klass: self)
end
resource_name() click to toggle source
# File lib/helium/resource.rb, line 165
def resource_name
  kebab_case(self.class.name.split('::').last)
end
resource_path() click to toggle source

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
to_json(*options) click to toggle source

@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
update(attributes) click to toggle source

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
updated_at() click to toggle source

@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