Module: Fog::JSON

Defined in:
lib/fog/json.rb

Overview

The JSON module includes functionality that is common between APIs using JSON to send and receive data.

The intent is to provide common code for provider APIs using JSON but not require it for those using XML.

Defined Under Namespace

Classes: DecodeError, EncodeError

Class Method Summary collapse

Class Method Details

.decode(obj) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/fog/json.rb', line 37

def self.decode(obj)
  obj = obj.read if obj.respond_to?(:read)
  return if obj.nil? || obj.empty?
  ::JSON.parse(obj)
rescue => err
  raise DecodeError.slurp(err)
end

.encode(obj) ⇒ Object



31
32
33
34
35
# File 'lib/fog/json.rb', line 31

def self.encode(obj)
  ::JSON.dump(obj)
rescue => err
  raise EncodeError.slurp(err)
end

.sanitize(data) ⇒ Object

This cleans up Time objects to be ISO8601 format



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fog/json.rb', line 18

def self.sanitize(data)
  case data
  when Array
    data.map { |datum| sanitize(datum) }
  when Hash
    data.each { |key, value| data[key] = sanitize(value) }
  when ::Time
    data.strftime("%Y-%m-%dT%H:%M:%S%:z")
  else
    data
  end
end