Module: GromNative

Extended by:
FFI::Library
Defined in:
lib/grom_native.rb,
lib/grom_native/node.rb,
lib/grom_native/version.rb

Overview

Top level namespace for our gem

Defined Under Namespace

Classes: Node

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.build_nodes(data_struct, filter, decorators) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/grom_native.rb', line 38

def self.build_nodes(data_struct, filter, decorators)
  nodes = []
  nodes_by_subject = {}
  filtered_nodes = Array.new(filter.size) { [] }

  data_struct.fetch('statementsBySubject', []).each do |subject, statements|
    node = GromNative::Node.new(statements, decorators)

    nodes << node
    nodes_by_subject[subject] = node

    if !filter.empty? && node.respond_to?(:type)
      node_types = node.blank? ? Array(::Grom::Node::BLANK) : Array(node.type)
      indexes = node_types.reduce([]) do |memo, type|
        index = filter.index(type)
        memo << index if index

        memo
      end

      indexes.each { |index| filtered_nodes[index] << node }
    end
  end

  link_nodes(nodes_by_subject, data_struct)

  return filtered_nodes.first if filter && filter.size == 1
  return filtered_nodes       if filter

  nodes
end

.fetch(uri:, headers: {}, filter: [], decorators: nil) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/grom_native.rb', line 13

def self.fetch(uri:, headers: {}, filter: [], decorators: nil)
  input = { uri: uri, headers: headers, filter: filter }
  data_struct = JSON.parse(get(uri))

  handle_errors(data_struct)

  build_nodes(data_struct, filter, decorators)
end

.handle_errors(data_struct) ⇒ Object

Raises:

  • (StandardError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/grom_native.rb', line 22

def self.handle_errors(data_struct)
  error = nil
  status_code = data_struct.fetch('status_code', 0)

  if status_code >= 500
    error = "Server error: #{data_struct['error']}"
  elsif status_code >= 300 && status_code < 500
    error = "Client error: #{data_struct['error']}"
  end

  # require 'irb'; binding.irb
  error ||= data_struct['error']

  raise StandardError, error if error != "" && error != nil
end


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/grom_native.rb', line 70

def self.link_nodes(nodes_by_subject, data_struct)
  data_struct.fetch('edgesBySubject', {}).each do |subject, predicates|
    predicates.each do |predicate, object_uris|
      raise NamingError if predicate == 'type'

      current_node = nodes_by_subject[subject]
      next if current_node.nil?

      object_uris.each do |object_uri|
        predicate_name_symbol = "@#{predicate}".to_sym

        # Get the current value (if there is one)
        current_value = current_node.instance_variable_get(predicate_name_symbol)

        object = current_value

        # If we have stored a string, and there are objects to link, create an empty array
        current_value_is_string    = current_value.is_a?(String)
        object_is_array_of_strings = object.all? { |entry| entry.is_a?(String) } if object.is_a?(Array)
        object_by_uri              = nodes_by_subject[object_uri]

        object = [] if (current_value_is_string || object_is_array_of_strings) && object_by_uri

        # If the above is correct, and we have an array
        if object.is_a?(Array)
          # Insert the current value (only if this is a new array (prevents possible duplication),
          # the current value is a string, and there are no linked objects to insert)
          object << current_value if object.empty? && current_value_is_string && object_by_uri.nil?

          # Insert linked objects, if there are any
          object << object_by_uri if object_by_uri
        end

        current_node.instance_variable_set(predicate_name_symbol, object)
      end
    end
  end

  self
end