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.11'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_data, modifiers = {}) ⇒ 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



101
102
103
# File 'lib/json_data_extractor.rb', line 101

def configuration
  @configuration ||= Configuration.new
end

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

Yields:



105
106
107
# File 'lib/json_data_extractor.rb', line 105

def configure
  yield(configuration)
end

Instance Method Details

#add_modifier(modifier_name, &block) ⇒ Object



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



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
# File 'lib/json_data_extractor.rb', line 20

def extract(schema)
  results = {}
  schema.each do |key, val|
    if val.is_a?(Hash)
      val.transform_keys!(&:to_sym)
      path       = val[:path]
      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 = []
    end

    extracted_data = JsonPath.on(@data, path)

    if extracted_data.empty?
      results[key] = nil
    else
      results[key] = apply_modifiers(extracted_data, modifiers)

      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