Class: Rapidomize::Payload

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Encoding::Json
Defined in:
lib/rapidomize/payload.rb

Overview

Payload objects are used to manage the payloads exchanged between Rapidomize Cloud and SDK. Payload objects can encode or decode their state to or from custom formats. (JSON, msgpack, …).

There are two kinds of payload objects:

Hash-like payloads collects payload information in key-value pairs.

Enum-like payloads collects information as an array of Hash-like payloads. Useful when sending multiple payloads in one request. This type of payloads are immutable.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Encoding::Json

#from_json, #to_json

Class Method Details

.create(obj) ⇒ Object

Factory method create payloads from other data types. This method will return corresponding Payload objects for Hashes, JSON strings. If the given object is already a Payload, it will be copied. It returns an empty Payload when obj is nil

Parameters:

  • obj (Hash, String, Payload, nil)

    A hash, possible JSON string or a Payload object

Returns:

  • a Payload object

Raises:

  • ArgumentError if obj is not a Hash, String, Payload or nil



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rapidomize/payload.rb', line 45

def self.create(obj)
  return Payload.new if obj.nil?

  case obj
  when Hash
    # noinspection RubyYardParamTypeMatch
    Payload.new.from_hash(obj)
  when String
    # noinspection RubyYardParamTypeMatch
    Payload.new.from_json(obj)
  when Payload
    obj.clone
  else
    raise ArgumentError, "Expected Hash, String or Payload; Given: #{obj.class}"
  end
end

Instance Method Details

#<<(payload) ⇒ Payload

Add a payload to the collection

Parameters:

  • payload (Payload)

    A payload to add

Returns:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rapidomize/payload.rb', line 94

def <<(payload)
  if payload.is_a? Array
    payload.each do |elem|
      raise InvalidPayloadTypeError, "Expected: Payload, Given: #{payload.class}" unless elem.is_a? Payload

      collection << elem
    end
  else
    raise InvalidPayloadTypeError, "Expected: Payload, Given: #{payload.class}" unless payload.is_a? Payload

    collection << payload
  end
  payload
end

#[](key) ⇒ Object

Get a value from the payload

Parameters:

  • key

    A key to search in the payload

Returns:

  • the value associated with payload key, nil if key is not in payload



65
66
67
# File 'lib/rapidomize/payload.rb', line 65

def [](key)
  hashmap[key.to_s]
end

#[]=(key, value) ⇒ Object

Set a value in the payload

Parameters:

  • key

    A key to search in the payload

  • value

    to be set for the key

Returns:

  • the value



73
74
75
# File 'lib/rapidomize/payload.rb', line 73

def []=(key, value)
  hashmap[key.to_s] = value
end

#dataObject



124
125
126
127
128
129
130
# File 'lib/rapidomize/payload.rb', line 124

def data
  if @type == :enum
    collection.clone
  else
    hashmap.clone
  end
end

#each(&block) ⇒ Object

Each method for enumerable features



116
117
118
119
120
121
122
# File 'lib/rapidomize/payload.rb', line 116

def each(&block)
  if @type == :enum
    collection.each { |payload| block.call(payload) }
  else
    hashmap.each { |key, value| block.call(key, value) }
  end
end

#from_hash(hash) ⇒ Object

Set values from a hash object

Parameters:

  • hash (Hash)

    Hash to include data from



86
87
88
89
# File 'lib/rapidomize/payload.rb', line 86

def from_hash(hash)
  hashmap.merge!(hash.transform_keys(&:to_s))
  self
end

#has?(key) ⇒ Boolean

Check if the key exists in the payload

Parameters:

  • key

    The key to search in the payload

Returns:

  • (Boolean)

    true if payload includes ‘key`



80
81
82
# File 'lib/rapidomize/payload.rb', line 80

def has?(key)
  hashmap.include? key.to_s
end

#sizeObject

Return the size of the payload collection

Returns:

  • the size of the payload collection



111
112
113
# File 'lib/rapidomize/payload.rb', line 111

def size
  data.size
end