Class: Moceansdk::Modules::ResponseFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/moceansdk/modules/response_factory.rb

Class Method Summary collapse

Class Method Details

.create_object(raw_response) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/moceansdk/modules/response_factory.rb', line 5

def self.create_object(raw_response)
  begin
    res = JSON.load(raw_response, proc {|obj|
      case obj
      when Hash
        obj.each {|k, v|
          obj[k] = if v.is_a? Float
                     # hardcoded float to 4 decimal string
                     sprintf('%.4f', v)
                   elsif v.is_a? Integer
                     v.to_s
                   else
                     v
                   end
        }
      when Array
        obj.map! {|v| v}
      end
    })
  rescue JSON::JSONError
    begin
      res = XmlSimple.xml_in(raw_response, 'ForceArray': false)
    rescue StandardError
      raise Moceansdk::Exceptions::MoceanError, 'unable to parse response, ' + raw_response
    end
  end

  hashed_res = HashExtended.new.merge(res)
  hashed_res.to_dot
end