Module: OSCRuby::NormalizeModule

Included in:
QueryModule
Defined in:
lib/osc_ruby/modules/normalize_module.rb

Class Method Summary collapse

Class Method Details

.nested_normalize(input, resource) ⇒ Object



64
65
66
67
68
69
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
# File 'lib/osc_ruby/modules/normalize_module.rb', line 64

def nested_normalize(input,resource)

  if input.code.to_i == 404

    input.body

  else

    json_input = JSON.parse(input.body)
    
    final_hash = []

    json_input['items'].each do |item|

      item_array = []

      item['rows'].each_with_index do |row,row_i|

        obj_hash = {}
        
        item['columnNames'].each_with_index do |column,i|
          obj_hash[column] = if !row[i].nil? && row[i].is_i? == true then row[i].to_i else row[i] end
        end

        item_array.push(obj_hash)

      end

      final_hash.push(item_array)

    end

    
    final_hash.to_json

  end

end

.normalize(input, resource) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
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
# File 'lib/osc_ruby/modules/normalize_module.rb', line 9

def normalize(input,resource)

  if input.code.to_i == 404

    input.body

  else

    json_input = JSON.parse(input.body)

    # initialize an array to hold all of the objects

    final_hash = []

    # loop through the items from the returned JSON response

    json_input['items'].each do |item|

        results_array = []

            # loop through rows

            item['rows'].each_with_index do |row,row_i|

                    # initialize a hash to create the object

                    obj_hash = {}
                    
                    # loop through the column names from the query

                    item['columnNames'].each_with_index do |column,i|

                            # set the object property to the value of the row

                            # where the index of the value within that row

                            # matches the index of the column name

                            obj_hash[column] = if !row[i].nil? && row[i].is_i? == true then row[i].to_i else row[i] end
                    end

              # push the hash into the array

              results_array.push(obj_hash)

            end

          # puts "this is an array:  #{final_hash}"

            final_hash.push(results_array)


    end

    if final_hash.size === 1
      final_hash = final_hash.flatten!
    end


    final_hash.to_json

  end

end