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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +5 -2
- data/bin/setup +0 -2
- data/lib/proto/MPS7/cli.rb +2 -2
- data/lib/proto/MPS7/parser.rb +92 -32
- data/lib/proto/MPS7/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6aaf50f0ad955fc6b7bc6d8b54a1d00a6f81a451
|
4
|
+
data.tar.gz: 1209b7349bc6ed83973f203c4f71dcffb39f58a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c383708dc6e8276a8c309ae9eadabccb92cba84c42e0848d161aebd9be23d5943d52694461e6a0e32dffbcf090e89cc72d8991ede5705ec208e3157cff0fadf
|
7
|
+
data.tar.gz: f809ceb3af4532a7ddc0e48e2b062147903eed63b67d465b37190d3f475c0b96d76c3091e4b2a5c2d6ba163feec795abd36e47d898ea073c6b8f43954f3ad12f
|
data/Gemfile.lock
CHANGED
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
data/lib/proto/MPS7/cli.rb
CHANGED
@@ -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.
|
8
|
+
calculations = @protocol.parse
|
9
|
+
puts Parser::BinaryProtocol.answers(calculations)
|
10
10
|
end
|
data/lib/proto/MPS7/parser.rb
CHANGED
@@ -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
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
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
|
35
|
+
while binary_data_present?(data, index)
|
35
36
|
# debit
|
36
|
-
if data
|
37
|
-
record = data
|
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
|
40
|
+
total_dollars(calculations, :debit, record)
|
41
41
|
# for user ID 2456938384156277127,
|
42
|
-
if record
|
42
|
+
if for_user_id?(record, 2456938384156277127)
|
43
43
|
# sum the dollars of debits from the user's total balance
|
44
|
-
calculations
|
44
|
+
user_balance(calculations, 'sum', record)
|
45
45
|
end
|
46
46
|
index += 21
|
47
47
|
# credit
|
48
|
-
elsif data
|
49
|
-
record = data
|
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
|
51
|
+
total_dollars(calculations, :credit, record)
|
53
52
|
# for user ID 2456938384156277127,
|
54
|
-
if record
|
53
|
+
if for_user_id?(record, 2456938384156277127)
|
55
54
|
# subtract the dollars of credits from the user's total balance
|
56
|
-
calculations
|
55
|
+
user_balance(calculations, 'difference', record)
|
57
56
|
end
|
58
57
|
index += 21
|
59
|
-
|
60
|
-
elsif data
|
61
|
-
|
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
|
-
|
66
|
-
elsif data
|
67
|
-
|
68
|
-
# increase the total count of how many times Autopay has been
|
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
|
data/lib/proto/MPS7/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2019-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|