module XporterOnDemand::Utils
Public Instance Methods
assign_attributes(attributes)
click to toggle source
# File lib/xporter_on_demand/utils.rb, line 29 def assign_attributes(attributes) unless instance_variable_defined?("@attributes") instance_variable_set("@attributes", []) self.class.send(:attr_reader, :attributes) end XporterOnDemand::Result::Serialiser.serialise(attributes).each do |name, value| method_name = name.camelize.underscore if META_KEYS.include?(name.camelize) value = unwrap(value) else method_name = method_name.singularize if value.is_a?(Array) && value.length == 1 end instance_variable_set("@#{method_name}", value) self.class.send(:attr_reader, method_name) @attributes |= [method_name] end end
configure_request(*args)
click to toggle source
# File lib/xporter_on_demand/utils.rb, line 12 def configure_request(*args) request = HTTPI::Request.new(*args) request.headers["Content-Type"] = "application/json" request.headers["Authorization"] = "Idaas " + @token if @token request end
create_result(type, object)
click to toggle source
# File lib/xporter_on_demand/utils.rb, line 52 def create_result(type, object) Factory.create( META_KEYS.include?(type) ? type : type.singularize, XporterOnDemand::Result::Base, { namespace: XporterOnDemand::Result, result: object, } ) end
dont_raise_exception() { || ... }
click to toggle source
# File lib/xporter_on_demand/utils.rb, line 73 def dont_raise_exception @dont_raise_exception = true yield ensure @dont_raise_exception = false end
handle_exceptions(response)
click to toggle source
# File lib/xporter_on_demand/utils.rb, line 19 def handle_exceptions(response) response_body = JSON.parse(response.body || {}) raise ExceptionFactory.generate_exception(response_body) if response_body["ExceptionMessage"] && !@dont_raise_exception end
parameterize(sym)
click to toggle source
# File lib/xporter_on_demand/utils.rb, line 25 def parameterize(sym) sym.to_s.camelize(:lower) end
unwrap(enum)
click to toggle source
# File lib/xporter_on_demand/utils.rb, line 63 def unwrap(enum) if enum.is_a?(Array) enum.length <= 1 ? enum.first || [] : enum elsif enum.is_a?(Hash) enum.length == 1 ? unwrap(enum[enum.keys.first]) : enum else enum end end