datacalc 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/datacalc.rb +63 -0
  2. metadata +46 -0
data/lib/datacalc.rb ADDED
@@ -0,0 +1,63 @@
1
+ require 'json'
2
+
3
+ class DataCalc
4
+ attr_accessor :length, :output, :input
5
+
6
+ def initialize(input)
7
+ @input = JSON.parse(File.read(input))
8
+ @length = @input.length
9
+ @output = nil
10
+ end
11
+
12
+ # What: Gets the sum of a column
13
+ # Input: Column to add up
14
+ # Output: Sum for column
15
+ def sum(col)
16
+ total = 0
17
+ (0..@length-1).each do |l|
18
+ total += (@input[l])[col].to_i
19
+ end
20
+ return total
21
+ end
22
+
23
+ # What: Gets list entries that include all values of arguments passed
24
+ # Input: Variable number of arrays with field name and value
25
+ # Output: Parsed JSON of entries that match all input arrays
26
+ def match(*matchnames)
27
+ savefile = "[]"
28
+ matchnames.each do |match|
29
+ tmpfile = Array.new
30
+ (0..@length-1).each do |l|
31
+ if match[1] == (@input[l])[match[0]]
32
+ tmpfile << @input[l]
33
+ savefile = tmpfile.to_json
34
+ end
35
+ end
36
+ @input = JSON.parse(savefile)
37
+ @length = @input.length
38
+ end
39
+ @output = @input
40
+ end
41
+
42
+ # What: Sorts in numerical order
43
+ # Input: Attribute to sort by
44
+ # Output: Sorted JSON
45
+ def sortnum(sattr)
46
+ @input.sort_by {|e| e[sattr].to_i}.to_json
47
+ end
48
+
49
+ # What: Sorts in alphabetical order
50
+ # Input: Attribute to sort by
51
+ # Output: Sorted JSON
52
+ def sortalpha(sattr)
53
+ @input.sort_by {|e| e[sattr]}.to_json
54
+ end
55
+
56
+ # What: Get average of a column
57
+ # Input: Column to get average of
58
+ # Output: Average
59
+ def mean(col)
60
+ mean = sum(col).to_f / @length.to_f
61
+ end
62
+ end
63
+
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: datacalc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - M. C. McGrath
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-30 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Some data calculation/manipulation for Transparency Toolkit.
15
+ email: shidash@shidash.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/datacalc.rb
21
+ homepage: http://transparencytoolkit.org
22
+ licenses:
23
+ - GPL
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.23
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Transparency Toolkit Data Calculation
46
+ test_files: []