class BusinessCentral::Response::ResponseHandler

Attributes

compiled_data[RW]
dataset[RW]

Public Class Methods

new(dataset) click to toggle source

Constructor

@param dataset [Hash | Array] the result from the API operation

# File lib/business_central/response/response_handler.rb, line 14
def initialize(dataset)
  @compiled_data = []
  @dataset = dataset
  process
end

Public Instance Methods

process() click to toggle source

Compiles the data in the @dataset to the supplied class

# File lib/business_central/response/response_handler.rb, line 22
def process
  @dataset.is_a?(Array) ? process_array : process_data(@dataset)
end
process_array() click to toggle source

If the supplied result from the API operation is an array, iterate over it and process each result into @compiled_data

# File lib/business_central/response/response_handler.rb, line 29
def process_array
  @dataset.each { |data| process_data(data) }
end
process_data(data) click to toggle source

Parse the JSON response from the API into an OpenStruct object. This will also parse any child objects, which allows the data to be accessed via regular chaining

eg

company.address.state

@param data [JSON]

# File lib/business_central/response/response_handler.rb, line 42
def process_data(data)
  new_record = JSON.parse(sanitize(data), object_class: OpenStruct)

  unless data["@odata.etag"].nil?
    new_record.etag = data["@odata.etag"]
  end
  @compiled_data << new_record
end
sanitize(data) click to toggle source

Ensures the data is in JSON format

# File lib/business_central/response/response_handler.rb, line 53
def sanitize(data)
  data.is_a?(Hash) ? data.to_json : data
end