Module: Response

Instance Method Summary collapse

Instance Method Details

#bool_to_num(bool) ⇒ Object



3
4
5
6
# File 'lib/orthanc/response.rb', line 3

def bool_to_num(bool)
  return 0 if bool == false
  return 1 if bool == true
end

#handle_response(response) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/orthanc/response.rb', line 13

def handle_response(response)
  begin
    # Try to parse response
    parsed_response = JSON.parse(response)

    if parsed_response.class == Array
      # Normalize to an array inside a hash, so RecursiveOpenStruct can act
      data = {}
      data["response"] = parsed_response
      return RecursiveOpenStruct.new(data.to_snake_keys, recurse_over_arrays: true ).response

    elsif parsed_response.class == Hash
      return RecursiveOpenStruct.new(parsed_response.to_snake_keys, recurse_over_arrays: true )
    else
      return response
    end
  rescue JSON::ParserError => e # If JSON parse fails, return original response
    return response
  end
end

#num_to_bool(num) ⇒ Object



8
9
10
11
# File 'lib/orthanc/response.rb', line 8

def num_to_bool(num)
  return false if num == "0"
  return true if num == "1"
end