Class: Helium::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/helium/resource.rb

Overview

Abstract base class for Helium Resources returned by the API

Direct Known Subclasses

DataPoint, Element, Label, Organization, Sensor, User

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, params:) ⇒ Resource

Returns a new instance of Resource



6
7
8
9
10
11
# File 'lib/helium/resource.rb', line 6

def initialize(client:, params:)
  @client     = client
  @id         = params["id"]
  @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

Instance Method Details

#==(other) ⇒ Boolean

Override equality to use id for comparisons

Returns:

  • (Boolean)


15
16
17
# File 'lib/helium/resource.rb', line 15

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



45
46
47
48
49
50
51
# File 'lib/helium/resource.rb', line 45

def as_json
  {
    id: id,
    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



32
33
34
35
# File 'lib/helium/resource.rb', line 32

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

#eql?(other) ⇒ Boolean

Override equality to use id for comparisons

Returns:

  • (Boolean)


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

def eql?(other)
  self == other
end

#hashInteger

Override equality to use id for comparisons

Returns:

  • (Integer)


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

def hash
  id.hash
end

#to_json(*options) ⇒ String

Returns a JSON-encoded String representing the resource

Returns:

  • (String)

    a JSON-encoded String representing the resource



54
55
56
# File 'lib/helium/resource.rb', line 54

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

#updated_atDateTime?

Returns when the resource was last updated

Returns:

  • (DateTime, nil)

    when the resource was last updated



38
39
40
41
# File 'lib/helium/resource.rb', line 38

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