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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Resource

Returns a new instance of Resource



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

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

#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 = {}) ⇒ Array<Resource>

Returns all resources

Parameters:

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

    a customizable set of options

Options Hash (opts):

  • :client (Client)

    A Helium::Client

Returns:

  • (Array<Resource>)

    an Array of all of the inheriting Resource



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/helium/resource.rb', line 24

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

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:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/helium/resource.rb', line 54

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:



41
42
43
44
45
46
47
48
# File 'lib/helium/resource.rb', line 41

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

Returns:

  • (Boolean)


108
109
110
# File 'lib/helium/resource.rb', line 108

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



138
139
140
141
142
143
144
145
# File 'lib/helium/resource.rb', line 138

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



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

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



101
102
103
104
# File 'lib/helium/resource.rb', line 101

def destroy
  path = "/#{resource_name}/#{self.id}"
  @client.delete(path)
end

#eql?(other) ⇒ Boolean

Override equality to use id for comparisons

Returns:

  • (Boolean)


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

def eql?(other)
  self == other
end

#hashInteger

Override equality to use id for comparisons

Returns:

  • (Integer)


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

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



148
149
150
# File 'lib/helium/resource.rb', line 148

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

#update(attributes) ⇒ Resource

Updates a Resource

Parameters:

  • attributes (Hash)

    The attributes to update

Returns:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/helium/resource.rb', line 82

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_atDateTime?

Returns when the resource was last updated

Returns:

  • (DateTime, nil)

    when the resource was last updated



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

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