Class: Rapidomize::Payload
- Inherits:
-
Object
- Object
- Rapidomize::Payload
- 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
-
.create(obj) ⇒ Object
Factory method create payloads from other data types.
Instance Method Summary collapse
-
#<<(payload) ⇒ Payload
Add a payload to the collection.
-
#[](key) ⇒ Object
Get a value from the payload.
-
#[]=(key, value) ⇒ Object
Set a value in the payload.
- #data ⇒ Object
-
#each(&block) ⇒ Object
Each method for enumerable features.
-
#from_hash(hash) ⇒ Object
Set values from a hash object.
-
#has?(key) ⇒ Boolean
Check if the key exists in the payload.
-
#size ⇒ Object
Return the size of the payload collection.
Methods included from Encoding::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
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
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
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
73 74 75 |
# File 'lib/rapidomize/payload.rb', line 73 def []=(key, value) hashmap[key.to_s] = value end |
#data ⇒ Object
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
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
80 81 82 |
# File 'lib/rapidomize/payload.rb', line 80 def has?(key) hashmap.include? key.to_s end |
#size ⇒ Object
Return the size of the payload collection
111 112 113 |
# File 'lib/rapidomize/payload.rb', line 111 def size data.size end |