Class: JsonDataExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/src/version.rb,
lib/src/configuration.rb,
lib/json_data_extractor.rb

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

VERSION =
'0.0.14'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_data, modifiers = {}) ⇒ JsonDataExtractor

Returns a new instance of JsonDataExtractor.



8
9
10
11
# File 'lib/json_data_extractor.rb', line 8

def initialize(json_data, modifiers = {})
  @data      = json_data.is_a?(Hash) ? json_data.to_json : json_data # hopefully it's a string; maybe we'll add some validation here
  @modifiers = modifiers.transform_keys(&:to_sym) # todo address this later
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/json_data_extractor.rb', line 6

def data
  @data
end

#modifiersObject (readonly)

Returns the value of attribute modifiers.



6
7
8
# File 'lib/json_data_extractor.rb', line 6

def modifiers
  @modifiers
end

Class Method Details

.configurationObject



120
121
122
# File 'lib/json_data_extractor.rb', line 120

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



124
125
126
# File 'lib/json_data_extractor.rb', line 124

def configure
  yield(configuration)
end

Instance Method Details

#add_modifier(modifier_name, &block) ⇒ Object

Parameters:

  • modifier_name (String, Symbol)


14
15
16
17
# File 'lib/json_data_extractor.rb', line 14

def add_modifier(modifier_name, &block)
  modifier_name            = modifier_name.to_sym unless modifier_name.is_a?(Symbol)
  modifiers[modifier_name] = block
end

#extract(schema) ⇒ Object

Parameters:

  • schema (Hash)

    schema of the expected data mapping



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
69
70
71
72
73
74
75
76
77
# File 'lib/json_data_extractor.rb', line 20

def extract(schema)
  results = {}
  schema.each do |key, val|
    default_value = nil
    if val.is_a?(Hash)
      val.transform_keys!(&:to_sym)
      path       = val[:path]
      default_value = val[:default]
      maps       = Array([val[:maps] || val[:map]]).flatten.compact.map do |map|
        if map.is_a?(Hash)
          map
        else
          raise ArgumentError, "Invalid map: #{map.inspect}"
        end
      end
      modifiers  = Array(val[:modifiers] || val[:modifier]).map do |mod|
        case mod
        when Symbol, Proc
          mod
        when String
          mod.to_sym
        else
          raise ArgumentError, "Invalid modifier: #{mod.inspect}"
        end
      end
      array_type = 'array' == val[:type]
      nested     = val.dup.delete(:schema)
    else
      path      = val
      modifiers = []
      maps      = []
    end

    extracted_data = JsonPath.on(@data, path) if path

    if extracted_data.nil? || extracted_data.empty?
      results[key] = default_value.is_a?(Proc) ? default_value.call : (default_value || nil)
    else
      extracted_data.map! { |val| val.nil? ? default_value : val }
      transformed_data = apply_modifiers(extracted_data, modifiers)
      results[key]     = apply_maps(transformed_data, maps)

      if array_type && nested
        results[key] = extract_nested_data(results[key], nested)
      elsif !array_type && nested
        results[key] = extract_nested_data(results[key], nested).first
      elsif !array_type && 1 < results[key].size
        # TODO: handle case where results[key] has more than one item
        # do nothing for now
      elsif array_type && !nested
        # do nothing, it is already an array
      else
        results[key] = results[key].first
      end
    end
  end
  results
end