Class: EdFi::Client::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/ed_fi/client/response.rb

Overview

Represents an API response. EdFi::Client::Response instances initialized from a Hash also allow for reference chaining.

Instance Method Summary collapse

Constructor Details

#initialize(response, client: nil) ⇒ Response

Returns a new instance of Response

Parameters:

  • response (Hash, Array)

    The value to encapsulate as an EdFi::Client::Response.

  • client (Crapi::Client)

    The client to use for request chaining.

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ed_fi/client/response.rb', line 18

def initialize(response, client: nil)
  @client = client

  case response
  when Hash
    @response = response.to_a.map do |tuple|
      (key, value) = tuple.dup
      key = key.to_s.underscore.to_sym
      value = EdFi::Client::Response.new(value, client: @client) if value.is_a?(Hash) || value.is_a?(Array)

      [key, value]
    end.to_h.with_indifferent_access

  when Array
    @response = response.dup.map do |i|
      i = EdFi::Client::Response.new(i, client: @client) if i.is_a?(Hash) || i.is_a?(Array)
      i
    end

  when nil, ''
    ## This can happen for non-body-returning calls.
    @response = {}.with_indifferent_access

  else
    raise EdFi::Client::ArgumentError, %(Unexpected "response" type: #{response.class})

  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ed_fi/client/response.rb', line 103

def method_missing(name, *args, &block)
  ## Note references are cached.
  ## To force a refresh on an already-cached reference,
  ## the method should be called with a single `true` parameter.
  ## (i.e. `#school` vs `#school(true)`)

  if @response.is_a? Hash
    ## Allow for acceess to response values via dot notation.
    return @response[name] if @response.key? name

    ## Allow for assignment to response values via dot notation.
    return @response.send(:'[]=', name[0...-1], *args, &block) if name.to_s.ends_with? '='

    ## Allow for simple access to referenced resources.
    if @client.present?
      @references ||= {}
      reference = @response["#{name}_reference"].link.href rescue nil

      if reference.present?
        @references.delete(reference) if args[0] == true
        return @references[reference] ||= @client.get(reference)
      end
    end
  end

  ## All other unaccounted-for method calls should be delegated to the response Hash/Array.
  @response.send(name, *args, &block)
end

Instance Method Details

#client=(client) ⇒ Object

Deep updates the associated Crapi::Client] for this and all descendant EdFi::Client::Response instances.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ed_fi/client/response.rb', line 50

def client=(client)
  @client = client

  case @response
  when Hash
    @response.values.each { |i| i.client = client if i.is_a? EdFi::Client::Response }

  when Array
    @response.each { |i| i.client = client if i.is_a? EdFi::Client::Response }

  end
end