Class: Jerakia::Answer
- Inherits:
-
Object
- Object
- Jerakia::Answer
- Defined in:
- lib/jerakia/answer.rb
Instance Attribute Summary collapse
-
#datatype ⇒ Object
Returns the value of attribute datatype.
-
#lookup_type ⇒ Object
readonly
Returns the value of attribute lookup_type.
-
#merge_strategy ⇒ Object
readonly
Returns the value of attribute merge_strategy.
-
#payload ⇒ Object
Returns the value of attribute payload.
Instance Method Summary collapse
- #consolidate ⇒ Object
- #flatten_payload! ⇒ Object
-
#initialize(lookup_type = :first, merge_strategy = :array) ⇒ Answer
constructor
A new instance of Answer.
-
#merge_payload! ⇒ Object
TODO: consolidate this into less lines.
- #process_response(response_entries) ⇒ Object
- #set_data_type ⇒ Object
Constructor Details
#initialize(lookup_type = :first, merge_strategy = :array) ⇒ Answer
Returns a new instance of Answer.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/jerakia/answer.rb', line 10 def initialize(lookup_type = :first, merge_strategy = :array) @lookup_type = lookup_type @merge_strategy = merge_strategy case lookup_type when :first @payload = nil when :cascade @payload = [] @datatype = 'array' end end |
Instance Attribute Details
#datatype ⇒ Object
Returns the value of attribute datatype.
6 7 8 |
# File 'lib/jerakia/answer.rb', line 6 def datatype @datatype end |
#lookup_type ⇒ Object (readonly)
Returns the value of attribute lookup_type.
8 9 10 |
# File 'lib/jerakia/answer.rb', line 8 def lookup_type @lookup_type end |
#merge_strategy ⇒ Object (readonly)
Returns the value of attribute merge_strategy.
7 8 9 |
# File 'lib/jerakia/answer.rb', line 7 def merge_strategy @merge_strategy end |
#payload ⇒ Object
Returns the value of attribute payload.
5 6 7 |
# File 'lib/jerakia/answer.rb', line 5 def payload @payload end |
Instance Method Details
#consolidate ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/jerakia/answer.rb', line 37 def consolidate if lookup_type == :cascade && payload.is_a?(Array) case merge_strategy when :array flatten_payload! when :hash, :deep_hash merge_payload! end end end |
#flatten_payload! ⇒ Object
49 50 51 |
# File 'lib/jerakia/answer.rb', line 49 def flatten_payload! @payload.flatten! end |
#merge_payload! ⇒ Object
TODO: consolidate this into less lines
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/jerakia/answer.rb', line 55 def merge_payload! # rubocop:disable Metrics/MethodLength payload_hash = {} @payload.each do |p| next unless p.is_a?(Hash) case merge_strategy when :hash payload_hash = p.merge(payload_hash) when :deep_hash payload_hash = p.deep_merge!(payload_hash) end end @payload = payload_hash set_data_type end |
#process_response(response_entries) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/jerakia/answer.rb', line 22 def process_response(response_entries) response_entries.flatten.each do |res| case lookup_type when :first @payload = res[:value] @datatype = res[:datatype] Jerakia.log.debug("Registered answer as #{payload}") break when :cascade @payload << res[:value] end end consolidate end |
#set_data_type ⇒ Object
70 71 72 |
# File 'lib/jerakia/answer.rb', line 70 def set_data_type @data_type = @payload.class.to_s.downcase end |