Class: AsiBod::Bod

Inherits:
Object
  • Object
show all
Defined in:
lib/asi_bod/bod.rb

Overview

Handle reading in Grin Tech BOD Json file and doing operations on it

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = { bod_file: default_file_path }) ⇒ Bod

Returns a new instance of Bod.



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

def initialize(params = { bod_file: default_file_path })
  @json_data = JSON.parse(File.read(params[:bod_file]))
  @hash_data = json_data_to_hash(@json_data)
end

Instance Attribute Details

#hash_dataObject (readonly)

Returns the value of attribute hash_data.



7
8
9
# File 'lib/asi_bod/bod.rb', line 7

def hash_data
  @hash_data
end

Class Method Details

.default_file_pathObject

Returns the path to the default BODm.json file



10
11
12
# File 'lib/asi_bod/bod.rb', line 10

def self.default_file_path
  File.expand_path('../../../BODm.json', __FILE__)
end

Instance Method Details

#clean_dict(original_dict) ⇒ Hash<Integer>, <Hash>

Make the Dictionary an pleasant hash with Integer top keys (addresses) and symbols for other keys

Returns:

  • (Hash<Integer>, <Hash>)

    Hash of Hashes where the top key is the address



23
24
25
26
27
# File 'lib/asi_bod/bod.rb', line 23

def clean_dict(original_dict)
  original_dict.each_with_object({}) do |(k, v), memo|
    memo[k.to_i] = clean_node(v)
  end
end

#clean_node(node) ⇒ Object



35
36
37
38
39
# File 'lib/asi_bod/bod.rb', line 35

def clean_node(node)
  node.each_with_object({}) do |(k, v), memo|
    memo[k.to_sym] = v
  end
end

#json_data_to_hash(json_data) ⇒ Object



29
30
31
32
33
# File 'lib/asi_bod/bod.rb', line 29

def json_data_to_hash(json_data)
  json_data.each_with_object({}) do |(k, v), memo|
    memo[k.to_i] = clean_node(v)
  end
end