Module: XporterOnDemand::Utils

Included in:
Client, Endpoint, Registration, Result::Base, ResultSet
Defined in:
lib/xporter_on_demand/utils.rb

Instance Method Summary collapse

Instance Method Details

#assign_attributes(attributes) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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) ⇒ Object



12
13
14
15
16
17
# 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) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# 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_exceptionObject



73
74
75
76
77
78
# 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) ⇒ Object



19
20
21
22
23
# 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) ⇒ Object



25
26
27
# File 'lib/xporter_on_demand/utils.rb', line 25

def parameterize(sym)
  sym.to_s.camelize(:lower)
end

#unwrap(enum) ⇒ Object



63
64
65
66
67
68
69
70
71
# 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