class EdFi::Client::Response
Represents an API response. {EdFi::Client::Response EdFi::Client::Response} instances initialized from a Hash also allow for reference chaining.
Public Class Methods
new(response, client: nil)
click to toggle source
@param response [Hash, Array]
The value to encapsulate as an {EdFi::Client::Response EdFi::Client::Response}.
@param client [Crapi::Client]
The client to use for request chaining.
@raise [EdFi::Client::ArgumentError]
# 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
Public Instance Methods
as_json(*args)
click to toggle source
@private
# File lib/ed_fi/client/response.rb, line 82 def as_json(*args) eval(to_s).as_json(*args) end
client=(client)
click to toggle source
Deep updates the associated Crapi::Client] for this and all descendant {EdFi::Client::Response EdFi::Client::Response} instances.
# 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
inspect()
click to toggle source
@private
# File lib/ed_fi/client/response.rb, line 71 def inspect @response.inspect end
method_missing(name, *args, &block)
click to toggle source
@private
# 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
respond_to_missing?(name, include_private = false)
click to toggle source
@private
# File lib/ed_fi/client/response.rb, line 135 def respond_to_missing?(name, include_private = false) ( \ ( \ (@response.is_a? Hash) \ && \ ( \ @response.key?(name) \ || \ @response.key?("#{name}_reference".to_sym) \ ) \ ) \ || \ @response.respond_to?(name, include_private) \ ) end
to_json(*args)
click to toggle source
@private
# File lib/ed_fi/client/response.rb, line 94 def to_json(*args) eval(to_s).to_json(*args) end
to_s()
click to toggle source
@private
# File lib/ed_fi/client/response.rb, line 65 def to_s @response.to_s end