Class: Wsapi::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/wsapi/mapper.rb

Class Method Summary collapse

Class Method Details

.get_errors(json) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/wsapi/mapper.rb', line 10

def self.get_errors(json)
  if result = json["QueryResult"]
    result["Errors"]
  elsif result = json["OperationResult"]
    result["Errors"]
  else
    []
  end
end

.get_object(response) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/wsapi/mapper.rb', line 20

def self.get_object(response)
  json = MultiJson.load(response.body)
  if get_errors(json).empty? && json.size == 1
    Wsapi::Object.from_data(json.keys.first, json.values.first)
  else
    raise ApiError.new("Errors: #{get_errors(json).inspect}", response)
  end
rescue MultiJson::LoadError
  raise ApiError.new("Invalid JSON response from WSAPI: #{response.body}", response)
end

.get_objects(response) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/wsapi/mapper.rb', line 31

def self.get_objects(response)
  json = MultiJson.load(response.body)
  if get_errors(json).empty? && query_result = json["QueryResult"]
    query_result["Results"].map { |object| Wsapi::Object.from_data(object["_type"], object) }
  else
    raise ApiError.new("Errors: #{get_errors(json).inspect}", response)
  end
rescue MultiJson::LoadError
  raise ApiError.new("Invalid JSON response from WSAPI: #{response.body}", response)
end