proto-MPS7 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 539138dfb892f130d887e3a887348fe200ec25de
4
- data.tar.gz: 94cc084c6ee9aac724773e6ae174f2490a788874
3
+ metadata.gz: 6aaf50f0ad955fc6b7bc6d8b54a1d00a6f81a451
4
+ data.tar.gz: 1209b7349bc6ed83973f203c4f71dcffb39f58a2
5
5
  SHA512:
6
- metadata.gz: ab5de42a351f85e10cd8c9decaeda3e1ccf18382a269ba85894c0d4dbd1b5edec64261fecd93c67e95277785617a67338af692a7db8ead4f82462abee5e58f95
7
- data.tar.gz: b2391a6e43811484c8c082d9ecafb4366ea34942ebce6d3823720b7919bfa510368b8e572f9e72d0088f8f150c16e266ab57dc4ea370687e0227bd3f64a61330
6
+ metadata.gz: 8c383708dc6e8276a8c309ae9eadabccb92cba84c42e0848d161aebd9be23d5943d52694461e6a0e32dffbcf090e89cc72d8991ede5705ec208e3157cff0fadf
7
+ data.tar.gz: f809ceb3af4532a7ddc0e48e2b062147903eed63b67d465b37190d3f475c0b96d76c3091e4b2a5c2d6ba163feec795abd36e47d898ea073c6b8f43954f3ad12f
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- proto-MPS7 (0.1.0)
4
+ proto-MPS7 (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Proto::MPS7
1
+ # Proto::MPS7 🛸
2
2
 
3
3
  Put your Ruby code in the file `lib/proto/MPS7`. To experiment with that code, run `bin/console` for an interactive prompt.
4
4
 
@@ -14,6 +14,10 @@ Or install it yourself as:
14
14
 
15
15
  $ proto_MPS7 txnlog.dat
16
16
 
17
+ ## Running Tests
18
+
19
+ $ bundle exec rspec spec
20
+
17
21
  ## Development
18
22
 
19
23
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -21,7 +25,6 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
21
25
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
22
26
 
23
27
 
24
-
25
28
  ## License
26
29
 
27
30
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/bin/setup CHANGED
@@ -4,5 +4,3 @@ IFS=$'\n\t'
4
4
  set -vx
5
5
 
6
6
  bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,4 +1,3 @@
1
- require "byebug"
2
1
  require_relative "parser"
3
2
 
4
3
  class Cli
@@ -6,5 +5,6 @@ require_relative "parser"
6
5
 
7
6
  puts Parser::BinaryProtocol.ascii
8
7
  @protocol = Parser::BinaryProtocol.new(ARGV)
9
- @protocol.open
8
+ calculations = @protocol.parse
9
+ puts Parser::BinaryProtocol.answers(calculations)
10
10
  end
@@ -1,7 +1,4 @@
1
- require 'csv'
2
1
  require "active_support/all"
3
- require 'active_support/time_with_zone'
4
- require 'awesome_print'
5
2
 
6
3
  module Parser
7
4
  class BinaryProtocol
@@ -13,64 +10,127 @@ module Parser
13
10
 
14
11
  def self.ascii
15
12
  # the artii gem creates ascii text
16
- # this method shells it out
17
13
  `artii proto-MPS7 Parser`
18
14
  end
19
15
 
20
- # byte_data = contents.bytes.slice(9..-1)
21
- # header = ["MPS7", 1, 1191182336]
22
- def open
23
- contents = File.open("#{binary_file}", 'rb') { |f| f.read }
16
+ def parse
17
+ begin
18
+ contents = File.open("#{binary_file}", 'rb') { |f| f.read }
19
+ rescue Errno::ENOENT => e
20
+ STDERR.puts "No binary file given as an argument. Please include binary file to continue."
21
+ exit
22
+ end
24
23
  header = contents.unpack("a4CL>")
24
+ # header = ["MPS7", 1, 1191182336]
25
25
 
26
- data = contents.bytes.slice!(9..-1).pack("C*")
27
26
  index = 0
28
- calculations = { :debit => 0,
29
- :credit => 0,
27
+ calculations = { :debit => BigDecimal.new(0),
28
+ :credit => BigDecimal.new(0),
30
29
  :started_count => 0,
31
30
  :ended_count => 0,
32
- '2456938384156277127' => 0 }
31
+ '2456938384156277127' => BigDecimal.new(0) }
32
+
33
+ data = contents.bytes.slice!(9..-1).pack("C*")
33
34
 
34
- while data[index].present?
35
+ while binary_data_present?(data, index)
35
36
  # debit
36
- if data[index].unpack("C")[0] == 0
37
- record = data[index..index + 21].unpack("CL>Q>G")
38
- puts record
37
+ if debit?(data, index)
38
+ record = unpack_four_fields(data, index)
39
39
  # total amount in dollars of debits
40
- calculations[:debit] += record[3].to_f
40
+ total_dollars(calculations, :debit, record)
41
41
  # for user ID 2456938384156277127,
42
- if record[2] == 2456938384156277127
42
+ if for_user_id?(record, 2456938384156277127)
43
43
  # sum the dollars of debits from the user's total balance
44
- calculations['2456938384156277127'] += record[3].to_f
44
+ user_balance(calculations, 'sum', record)
45
45
  end
46
46
  index += 21
47
47
  # credit
48
- elsif data[index].unpack("C")[0] == 1
49
- record = data[index..index + 21].unpack("CL>Q>G")
50
- puts record
48
+ elsif credit?(data, index)
49
+ record = unpack_four_fields(data, index)
51
50
  # total amount in dollars of credits
52
- calculations[:credit] += record[3].to_f
51
+ total_dollars(calculations, :credit, record)
53
52
  # for user ID 2456938384156277127,
54
- if record[2] == 2456938384156277127
53
+ if for_user_id?(record, 2456938384156277127)
55
54
  # subtract the dollars of credits from the user's total balance
56
- calculations['2456938384156277127'] -= record[3].to_f
55
+ user_balance(calculations, 'difference', record)
57
56
  end
58
57
  index += 21
59
- # StartAutopay
60
- elsif data[index].unpack("C")[0] == 2
61
- puts data[index..index + 13].unpack("CL>Q>")
58
+ # StartAutopay
59
+ elsif start_autopay?(data, index)
60
+ unpack_three_fields(data, index)
62
61
  # increase the total count of how many times Autopay has been started
63
62
  calculations[:started_count] += 1
64
63
  index += 13
65
- # EndAutopay
66
- elsif data[index].unpack("C")[0] == 3
67
- puts data[index..index + 13].unpack("CL>Q>")
68
- # increase the total count of how many times Autopay has been started
64
+ # EndAutopay
65
+ elsif end_autopay?(data, index)
66
+ unpack_three_fields(data, index)
67
+ # increase the total count of how many times Autopay has been ended
69
68
  calculations[:ended_count] += 1
70
69
  index += 13
70
+ else
71
+ STDERR.puts "Binary does not follow custom protocol."
72
+ exit
71
73
  end
72
74
  end
73
75
  calculations
74
76
  end
77
+
78
+ def binary_data_present?(data, index)
79
+ data[index].present?
80
+ end
81
+
82
+ def debit?(data, index)
83
+ data[index].unpack("C")[0] == 0
84
+ end
85
+
86
+ def credit?(data, index)
87
+ data[index].unpack("C")[0] == 1
88
+ end
89
+
90
+ def start_autopay?(data, index)
91
+ data[index].unpack("C")[0] == 2
92
+ end
93
+
94
+ def end_autopay?(data, index)
95
+ data[index].unpack("C")[0] == 3
96
+ end
97
+
98
+ def for_user_id?(record, id)
99
+ record[2] == id
100
+ end
101
+
102
+ def unpack_four_fields(data, index)
103
+ data[index..index + 21].unpack("CL>Q>G")
104
+ end
105
+
106
+ def unpack_three_fields(data, index)
107
+ data[index..index + 13].unpack("CL>Q>")
108
+ end
109
+
110
+ def total_dollars(calculations, account, record)
111
+ calculations[account] += record[3].to_d
112
+ end
113
+
114
+ def user_balance(calculations, operator, record)
115
+ if operator == 'sum'
116
+ calculations['2456938384156277127'] += record[3].to_d
117
+ elsif operator == 'difference'
118
+ calculations['2456938384156277127'] -= record[3].to_d
119
+ end
120
+ end
121
+
122
+ def self.monetize(money)
123
+ '%.2f' % money
124
+ end
125
+
126
+ def self.answers(calculations)
127
+ puts <<~CALCS.strip
128
+ $#{self.monetize(calculations[:debit])} is the total amount in dollars of debits,
129
+ $#{self.monetize(calculations[:credit])} is the total amount of dollars of credits,
130
+ #{calculations[:started_count]} autopays were started,
131
+ #{calculations[:ended_count]} autopays were ended,
132
+ user ID 2456938384156277127 has a balance of $#{self.monetize(calculations['2456938384156277127'])}.
133
+ CALCS
134
+ end
75
135
  end
76
136
  end
@@ -1,5 +1,5 @@
1
1
  module Proto
2
2
  module MPS7
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proto-MPS7
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patricia Arbona
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-10 00:00:00.000000000 Z
11
+ date: 2019-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler