Class: Vng::Asset

Inherits:
Resource show all
Defined in:
lib/vng/asset.rb

Overview

Provides methods to interact with Vonigo assets.

Constant Summary collapse

PATH =
'/api/v1/data/Assets/'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, name: nil, weight: nil, breed_option_id: nil) ⇒ Asset

Returns a new instance of Asset.



10
11
12
13
14
15
# File 'lib/vng/asset.rb', line 10

def initialize(id:, name: nil, weight: nil, breed_option_id: nil)
  @id = id
  @name = name
  @weight = weight
  @breed_option_id = breed_option_id
end

Instance Attribute Details

#breed_option_idObject (readonly)

Returns the value of attribute breed_option_id.



8
9
10
# File 'lib/vng/asset.rb', line 8

def breed_option_id
  @breed_option_id
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/vng/asset.rb', line 8

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/vng/asset.rb', line 8

def name
  @name
end

#weightObject (readonly)

Returns the value of attribute weight.



8
9
10
# File 'lib/vng/asset.rb', line 8

def weight
  @weight
end

Class Method Details

.create(name:, weight:, breed_option_id:, client_id:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vng/asset.rb', line 17

def self.create(name:, weight:, breed_option_id:, client_id:)
  body = {
    method: '3',
    clientID: client_id,
    Fields: [
      { fieldID: 1013, fieldValue: name },
      { fieldID: 1017, fieldValue: weight },
      { fieldID: 1014, optionID: breed_option_id },
    ],
  }

  data = request path: PATH, body: body

  new id: data['Asset']['objectID']
end

.for_client_id(client_id) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vng/asset.rb', line 33

def self.for_client_id(client_id)
  body = { clientID: client_id, isCompleteObject: 'true' }

  data = request path: PATH, body: body

  data.fetch('Assets', []).map do |asset|
    next unless active?(asset)

    id = asset['objectID']
    name = asset['name']
    breed_option_id = option_for_field asset, 1014
    weight = value_for_field asset, 1017
    weight = (Integer weight unless weight.empty?)
    new id: id, name: name, weight: weight, breed_option_id: breed_option_id
  end
end

Instance Method Details

#destroyObject



50
51
52
53
54
55
56
57
# File 'lib/vng/asset.rb', line 50

def destroy
  body = {
    method: '4',
    objectID: id,
  }

  data = self.class.request path: PATH, body: body
end