Class: JSONAPI::ResponseDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/response_document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ResponseDocument

Returns a new instance of ResponseDocument.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/jsonapi/response_document.rb', line 5

def initialize(options = {})
  @serialized_results = []
  @result_codes = []
  @error_results = []
  @global_errors = []

  @options = options

  @top_level_meta = @options.fetch(:base_meta, {})
  @top_level_links = @options.fetch(:base_links, {})

  @key_formatter = @options.fetch(:key_formatter, JSONAPI.configuration.key_formatter)
end

Instance Attribute Details

#serialized_resultsObject (readonly)

Returns the value of attribute serialized_results.



3
4
5
# File 'lib/jsonapi/response_document.rb', line 3

def serialized_results
  @serialized_results
end

Instance Method Details

#add_global_error(error) ⇒ Object



40
41
42
# File 'lib/jsonapi/response_document.rb', line 40

def add_global_error(error)
  @global_errors.push error
end

#add_result(result, operation) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/jsonapi/response_document.rb', line 23

def add_result(result, operation)
  if result.is_a?(JSONAPI::ErrorsOperationResult)
    # Clear any serialized results
    @serialized_results = []

    # In JSONAPI v1 we only have one operation so all errors can be kept together
    result.errors.each do |error|
      add_global_error(error)
    end
  else
    @serialized_results.push result.to_hash(operation.options[:serializer])
    @result_codes.push result.code.to_i
    update_links(operation.options[:serializer], result)
    update_meta(result)
  end
end

#contentsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jsonapi/response_document.rb', line 44

def contents
  if has_errors?
    return { 'errors' => @global_errors }
  else
    hash = @serialized_results[0]
    meta = top_level_meta
    hash.merge!('meta' => meta) unless meta.empty?

    links = top_level_links
    hash.merge!('links' => links) unless links.empty?

    return hash
  end
end

#has_errors?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/jsonapi/response_document.rb', line 19

def has_errors?
  @error_results.length > 0 || @global_errors.length > 0
end

#statusObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/jsonapi/response_document.rb', line 59

def status
  status_codes = if has_errors?
                   @global_errors.collect do |error|
                     error.status.to_i
                   end
                 else
                   @result_codes
                 end

  # Count the unique status codes
  counts = status_codes.each_with_object(Hash.new(0)) { |code, counts| counts[code] += 1 }

  # if there is only one status code we can return that
  return counts.keys[0].to_i if counts.length == 1

  # :nocov: not currently used

  # if there are many we should return the highest general code, 200, 400, 500 etc.
  max_status = 0
  status_codes.each do |status|
    code = status.to_i
    max_status = code if max_status < code
  end
  return (max_status / 100).floor * 100
  # :nocov:
end