Class: Rapid::Common::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/ury_rapid/common/payload.rb

Overview

A wrapper around a PUT/POST payload

Payloads come in multiple formats:

  • A URL payload, which specifies where the resource desired can be found in terms of a string locator;

  • A hash payload, which directly specifies the properties the new resource will have according to some payload 'type';

  • A hash mapping an ID to one of the above, thus directly specifying where in the model structure the new resource should go.

This object takes a raw payload from the server, alongside the privileges granted to the client sending it is; decides which form of payload it is; then spits out a normalised form of the payload so that the handling objects can interpret it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload, privilege_set, default_id = nil) ⇒ Payload

Returns a new instance of Payload


23
24
25
26
27
28
# File 'lib/ury_rapid/common/payload.rb', line 23

def initialize(payload, privilege_set, default_id = nil)
  @default_id = default_id
  @payload = payload
  @privilege_set = privilege_set
  @id, @body = flatten_payload
end

Instance Attribute Details

#idObject

Returns the value of attribute id


21
22
23
# File 'lib/ury_rapid/common/payload.rb', line 21

def id
  @id
end

#privilege_setObject

Returns the value of attribute privilege_set


20
21
22
# File 'lib/ury_rapid/common/payload.rb', line 20

def privilege_set
  @privilege_set
end

Instance Method Details

#flatten_payloadArray

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Flattens a payload into an item and target ID

Returns:

  • (Array)

    A tuple of the target ID and direct object.


44
45
46
# File 'lib/ury_rapid/common/payload.rb', line 44

def flatten_payload
  id_mapped_payload? ? flatten_payload! : [@default_id, @payload]
end

#flatten_payload!Object


48
49
50
51
# File 'lib/ury_rapid/common/payload.rb', line 48

def flatten_payload!
  id, payload = @payload.flatten
  [make_valid_id(id), payload]
end

#id_mapped_payload?Boolean

Determines whether a payload is a map from an ID to a payload body

Returns:

  • (Boolean)

    True if the payload is an ID-map, false otherwise.


67
68
69
# File 'lib/ury_rapid/common/payload.rb', line 67

def id_mapped_payload?
  @payload.is_a?(Hash) && @payload.size == 1
end

#make_valid_id(id) ⇒ Object

Coaxes a payload ID into a valid type

This can either be a Symbol or an Integer.


56
57
58
59
60
61
62
# File 'lib/ury_rapid/common/payload.rb', line 56

def make_valid_id(id)
  # This to_s allows IDs that have been symbolised earlier up to be
  # turned into Integers.
  Integer(id.to_s)
rescue TypeError, RangeError, ArgumentError
  id.to_sym
end

#process(receiver) ⇒ Object


35
36
37
# File 'lib/ury_rapid/common/payload.rb', line 35

def process(receiver)
  PayloadDispatch.dispatch(@body, receiver)
end

#with_body(body) ⇒ Object

Creates a new payload with this payload's privileges but a new body


31
32
33
# File 'lib/ury_rapid/common/payload.rb', line 31

def with_body(body)
  self.class.new(body, @privilege_set, @default_id)
end