Class: Helium::Resource

Inherits:
Object
  • Object
show all
Extended by:
Utils
Includes:
Utils
Defined in:
lib/helium/resource.rb

Overview

Abstract base class for Helium Resources returned by the API

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

datetime_to_iso, kebab_case

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

#idObject (readonly)

Returns the value of attribute id



4
5
6
# File 'lib/helium/resource.rb', line 4

def id
  @id
end

#paramsObject (readonly)

Returns the value of attribute params



4
5
6
# File 'lib/helium/resource.rb', line 4

def params
  @params
end

#typeObject (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 = {}) ⇒ Object



26
27
28
29
# File 'lib/helium/resource.rb', line 26

def all(opts = {})
  client = opts.fetch(:client)
  Collection.new(klass: self, client: client).all
end

.all_pathString

The resource's index API route

Returns:

  • (String)

    path to resource's index



22
23
24
# File 'lib/helium/resource.rb', line 22

def all_path
  "/#{resource_name}"
end

.create(attributes, opts = {}) ⇒ Resource

Creates a new resource with given attributes

Parameters:

  • attributes (Hash)

    The attributes for the new Resource

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :client (Client)

    A Helium::Client

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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 = {}) ⇒ Resource

Finds a single Resource by id

Parameters:

  • id (String)

    An id to find the Resource

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :client (Client)

    A Helium::Client

Returns:



35
36
37
38
# 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 = {}) ⇒ Object



74
75
76
77
78
79
80
81
# 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

.resource_nameObject



70
71
72
# File 'lib/helium/resource.rb', line 70

def resource_name
  kebab_case(self.name.split('::').last)
end

.singleton(opts = {}) ⇒ Resource

Fetches a singleton resource (e.g. organization, user)

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :client (Client)

    A Helium::Client

Returns:



43
44
45
46
# File 'lib/helium/resource.rb', line 43

def singleton(opts = {})
  client = opts.fetch(:client)
  initialize_from_path(path: all_path, client: client)
end

Instance Method Details

#==(other) ⇒ Boolean

Override equality to use id for comparisons

Returns:

  • (Boolean)


121
122
123
# File 'lib/helium/resource.rb', line 121

def ==(other)
  self.id == other.id
end

#as_jsonHash

Inheriting resources should implement this with super

Returns:

  • (Hash)

    a Hash of the object's attributes for JSON



151
152
153
154
155
156
157
158
# File 'lib/helium/resource.rb', line 151

def as_json
  {
    id: id,
    type: type,
    created_at: created_at,
    updated_at: updated_at
  }
end

#created_atDateTime?

Returns when the resource was created

Returns:

  • (DateTime, nil)

    when the resource was created



138
139
140
141
# File 'lib/helium/resource.rb', line 138

def created_at
  return nil if @created_at.nil?
  @_created_at ||= DateTime.parse(@created_at)
end

#destroyBoolean

Deletes the Resource

Returns:

  • (Boolean)

    Whether the operation was successful



111
112
113
# File 'lib/helium/resource.rb', line 111

def destroy
  @client.delete(resource_path)
end

#eql?(other) ⇒ Boolean

Override equality to use id for comparisons

Returns:

  • (Boolean)


127
128
129
# File 'lib/helium/resource.rb', line 127

def eql?(other)
  self == other
end

#hashInteger

Override equality to use id for comparisons

Returns:

  • (Integer)


133
134
135
# File 'lib/helium/resource.rb', line 133

def hash
  id.hash
end

#metadataObject



115
116
117
# File 'lib/helium/resource.rb', line 115

def 
  Metadata.new(client: @client, klass: self)
end

#resource_nameObject



165
166
167
# File 'lib/helium/resource.rb', line 165

def resource_name
  kebab_case(self.class.name.split('::').last)
end

#resource_pathString

Returns a path identifying the current resource. Can be overridden in child classes to handle non-standard resources (e.g. Organization)

Returns:

  • (String)

    path to resource



87
88
89
# File 'lib/helium/resource.rb', line 87

def resource_path
  "/#{resource_name}/#{self.id}"
end

#to_json(*options) ⇒ String

Returns a JSON-encoded String representing the resource

Returns:

  • (String)

    a JSON-encoded String representing the resource



161
162
163
# File 'lib/helium/resource.rb', line 161

def to_json(*options)
  as_json.to_json(*options)
end

#update(attributes) ⇒ Resource

Updates a Resource

Parameters:

  • attributes (Hash)

    The attributes to update

Returns:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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_atDateTime?

Returns when the resource was last updated

Returns:

  • (DateTime, nil)

    when the resource was last updated



144
145
146
147
# File 'lib/helium/resource.rb', line 144

def updated_at
  return nil if @updated_at.nil?
  @_updated_at ||= DateTime.parse(@updated_at)
end