Class: Alula::SingletonDcpCommandResource

Inherits:
Object
  • Object
show all
Defined in:
lib/alula/singleton_dcp_command_resource.rb

Overview

Base class for DCP command resources

Direct Known Subclasses

Dcp::Config::Synchronize

Constant Summary collapse

BASE_PATH =
'/dcp/v2/helix'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_id, attributes = nil) ⇒ SingletonDcpCommandResource

Returns a new instance of SingletonDcpCommandResource.



10
11
12
13
# File 'lib/alula/singleton_dcp_command_resource.rb', line 10

def initialize(device_id, attributes = nil)
  @values = attributes || {}
  construct_new_resource(device_id)
end

Instance Attribute Details

#device_idObject

Returns the value of attribute device_id.



6
7
8
# File 'lib/alula/singleton_dcp_command_resource.rb', line 6

def device_id
  @device_id
end

#valuesObject

Returns the value of attribute values.



6
7
8
# File 'lib/alula/singleton_dcp_command_resource.rb', line 6

def values
  @values
end

Class Method Details

.payload_required?Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
# File 'lib/alula/singleton_dcp_command_resource.rb', line 93

def self.payload_required?
  # return false unless the method is overridden
  return false unless method_defined?(:payload_required?)

  payload_required?
end

.payload_to_camelcase(payload) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/alula/singleton_dcp_command_resource.rb', line 82

def self.payload_to_camelcase(payload)
  # recursively convert all keys to camelcase
  payload.each_with_object({}) do |(key, value), hash|
    hash[Util.camelize(key)] = if value.is_a?(Array)
                                 value.map { |v| v.is_a?(Hash) ? payload_to_camelcase(v) : v }
                               else
                                 value.is_a?(Hash) ? payload_to_camelcase(value) : value
                               end
  end
end

.request(device_id:, http_method:, path: get_resource_path, payload: {}, opts: {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/alula/singleton_dcp_command_resource.rb', line 19

def self.request(device_id:, http_method:, path: get_resource_path, payload: {}, opts: {})
  if self == Alula::SingletonDcpCommandResource
    raise NotImplementedError,
          'Alula::SingletonDcpCommandResource is an abstract class and cannot be instantiated.' \
          ' You should perform actions on its subclasses (Synchronize, etc.)'
  end
  if payload.empty? && payload_required?
    raise ArgumentError, 'Payload is empty. This will result in a useless request.'
  end

  path = "#{BASE_PATH}/#{device_id}/#{path}"
  payload = payload_to_camelcase(payload)
  response = Alula::Client.request(http_method, path, payload, opts)

  unless response.ok?
    error = Alula::AlulaError.for_response(response)

    #
    # Some error classifications are critical and should be raised for visibility
    # These errors do not contain meaningful data that an end-user can use to correct
    # the error.
    raise error if [Alula::RateLimitError, Alula::BadRequestError, Alula::ForbiddenError,
                    Alula::UnknownError, Alula::InsufficientScopeError].include?(error.class)

    return error
  end

  new(device_id, response.data)
end

Instance Method Details

#apply_attributes(attributes) ⇒ Object



72
73
74
75
76
# File 'lib/alula/singleton_dcp_command_resource.rb', line 72

def apply_attributes(attributes)
  attributes.each do |key, value|
    send("#{key}=", value)
  end
end

#callObject



78
79
80
# File 'lib/alula/singleton_dcp_command_resource.rb', line 78

def call
  self.class.call(device_id: device_id, payload: @values)
end

#construct_new_resource(device_id) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/alula/singleton_dcp_command_resource.rb', line 49

def construct_new_resource(device_id)
  self.device_id = device_id
  instance_eval do
    fields.each_key do |field|
      camel_key = Util.camelize(field)
      define_singleton_method(field) do
        if @values.key?(camel_key)
          @values[camel_key]
        else
          @values[field]
        end
      end
      define_singleton_method("#{field}=") do |value|
        if @values.key?(camel_key)
          @values[camel_key] = value
        else
          @values[field] = value
        end
      end
    end
  end
end

#model_nameObject



15
16
17
# File 'lib/alula/singleton_dcp_command_resource.rb', line 15

def model_name
  self.class
end