Class: Xcal::Parktronic::GenericResponse

Inherits:
Object
  • Object
show all
Includes:
Exceptions, Routes::Nested::AlarmActions, Routes::Nested::Events, Routes::Nested::MetricValues, Routes::Nested::ValueGroups
Defined in:
lib/xcal/parktronic/generic_response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Routes::Nested::ValueGroups

#get_value_groups

Methods included from Routes::Nested::AlarmActions

#get_alarm_action, #get_alarm_actions, #post_alarm_action, #set_position, #update_alarm_action

Methods included from Routes::Nested::MetricValues

#get_paged_metric_values

Methods included from Routes::Nested::Events

#get_all_events, #get_paged_events, #post_event

Constructor Details

#initialize(raw_response = nil, client = nil) ⇒ GenericResponse

Generic Response initializations

Accepted attributes are:

+raw_response+ should be a hash or a valid raw JSON string


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/xcal/parktronic/generic_response.rb', line 19

def initialize(raw_response = nil, client = nil)
  # nil input
  raise InvalidResponseArgument.new('Cannot create response object without raw response') if raw_response.nil?

  if raw_response.is_a?(Hash)
    # hash input
    @source = raw_response
  elsif raw_response.is_a?(String)
    # json input
    parsed_json = JSON.parse(raw_response) rescue nil
    raise InvalidResponseArgument.new("Argument `#{raw_response}' should be a Hash or raw json String") unless parsed_json

    # for list routes render first Array object, for single item routes render object as is
    if parsed_json.is_a?(Array)
      if parsed_json.length > 1
        @source = { list: parsed_json }
      else
        @source = parsed_json.first
      end
    else
      @source = parsed_json
    end
  else
    # invalid input
    raise InvalidResponseArgument.new("Argument `#{raw_response}' should be a Hash or raw json String")
  end

  @source = Hashie::Mash.new(@source)
  @client = client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/xcal/parktronic/generic_response.rb', line 54

def method_missing(*args)
  # verify key in hashed self first
  if @source.has_key?(args.first)
    @source[args.first]
  # verify key in nested routes
  elsif respond_to?(args.first)
    send args.first
  else
    raise NoMethodError, "undefined method `#{args.first}' for #{self.class}"
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



13
14
15
# File 'lib/xcal/parktronic/generic_response.rb', line 13

def client
  @client
end

Instance Method Details

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/xcal/parktronic/generic_response.rb', line 50

def has_key?(key)
  @source.has_key?(key)
end