gravity_falls_message 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 51e68853e44988a10e325f0dbcae25d8af1d0cac
4
+ data.tar.gz: 609060a40297ae2f77602857f988e3924ad3c2b1
5
+ SHA512:
6
+ metadata.gz: 6f6bad51562f6e710e850380b21747f267b31e93fea56e27f5d591f3e24689b6c3a107fa62ef4e4a0a355436a2db3cbb4687036ab42ff676ca199745b1ee9f79
7
+ data.tar.gz: 9fc5a8aaaf6f7ad5914745a374e38b9b4cf96ca9386968c545ae321e8dcb9149b73ccf7eb45fd26e1f0fa3aa0b3c0d654cf61da644a014273b945dc7c64c71cb
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+
16
+ .ruby-version
17
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gravity_falls_message.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Brian Hall
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,123 @@
1
+ # Gravity Falls Message
2
+
3
+ A gem to encode and decode messages using the ciphers as presented on Disney XD's show Gravity Falls. For more detailed information about the ciphers and how they are used, visit [the Gravity Falls Wiki cryptograms page](http://gravityfalls.wikia.com/wiki/List_of_cryptograms "Gravity Falls Wiki - List of cryptograms")
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'gravity_falls'
11
+ ```
12
+
13
+ Run the following command to install it:
14
+
15
+ ```console
16
+ bundle install
17
+ ```
18
+
19
+ ## Supported Ciphers
20
+
21
+ This gem currently supports the following ciphers:
22
+
23
+ - A1Z26
24
+ - atbash
25
+ - binary
26
+ - caesar
27
+ - vigenere
28
+
29
+ ## Usage
30
+
31
+ **Gravity Falls Message** has two methods, `decode` and `encode`. `GravityFallsMessage::Cipher` is where the work gets done, and defaults to decoding.
32
+
33
+ The `decode` and `encode` methods take the `message` to be decoded or encoded as the first argument, followed by the `cipher` to be used, and finally an `options` hash.
34
+
35
+ The `options` hash is only used for the Caesar and Vigenère ciphers. Since a Vigenère cipher is just a Caesar cipher whose shift is calculated from a key, the Vigenère cipher calls the Caesar cipher, passing in its `options` hash.
36
+
37
+ The A1Z26 cipher expects encoded messages to have the integer representation for each letter delimited by a `-` character, i.e. `14-5-24-20 21-16` is the representation of `NEXT UP`, where `N = 14`, `5 = E`, `24 = X`, etc.
38
+
39
+ ### Valid Options for Caesar and Vigenère Ciphers
40
+
41
+ Options other than those listed here are ignored.
42
+
43
+ **Valid Options:**
44
+
45
+ - `encode:` When present or `true`, switches the Cipher so it encodes the message rather than decoding it.
46
+ - `key:` A `String`. Characters other than `/[a-zA-z]/` are ignored.
47
+ - `shift:` An `Integer` representing the number of characters to shift. Defaults to 3. Must be positive (for now).
48
+
49
+ ### Decoding Messages
50
+
51
+ ```console
52
+ # decode a message using the A1Z26 cipher
53
+ > message = "20-15 2-5 3-15-14-20-9-14-21-5-4..."
54
+ => "20-15 2-5 3-15-14-20-9-14-21-5-4..."
55
+ > GravityFallsMessage.decode(message, 'a1z26')
56
+ => "TO BE CONTINUED..."
57
+
58
+ # decode a message using the atbash cipher
59
+ > message = "MLG S.T. DVOOH ZKKILEVW."
60
+ => "MLG S.T. DVOOH ZKKILEVW."
61
+ > GravityFallsMessage.decode(message, 'atbash')
62
+ => "NOT H.G. WELLS APPROVED."
63
+
64
+ # decode a message using binary
65
+ > message = "0101001101001000010011110101001001010100"
66
+ => "0101001101001000010011110101001001010100"
67
+ > GravityFallsMessage.decode(message, 'binary')
68
+ => "SHORT"
69
+
70
+ # decode a message using the caesar cipher
71
+ > message = "ZHOFRPH WR JUDYLWB IDOOV."
72
+ => "ZHOFRPH WR JUDYLWB IDOOV."
73
+ > GravityFallsMessage.decode(message, 'caesar')
74
+ => "WELCOME TO GRAVITY FALLS."
75
+
76
+ # decode a message using the vigenere cipher
77
+ > message = "NLMXQWWN IIZ LZFNF"
78
+ => "NLMXQWWN IIZ LZFNF"
79
+ > GravityFallsMessage.decode(message, 'vigenere', key: 'WHATEVS')
80
+ => "REMEMBER BIG HENRY"
81
+ ```
82
+
83
+ ### Encoding Messages
84
+
85
+ ```console
86
+ # encode a message using the A1Z26 cipher
87
+ > message = "TO BE CONTINUED..."
88
+ => "TO BE CONTINUED..."
89
+ > GravityFallsMessage.encode(message, 'a1z26')
90
+ => "20-15 2-5 3-15-14-20-9-14-21-5-4..."
91
+
92
+ # encode a message using the atbash cipher
93
+ > message = "NOT H.G. WELLS APPROVED."
94
+ => "NOT H.G. WELLS APPROVED."
95
+ > GravityFallsMessage.encode(message, 'atbash')
96
+ => "MLG S.T. DVOOH ZKKILEVW."
97
+
98
+ # encode a message using binary
99
+ > message = "SHORT"
100
+ => "SHORT"
101
+ > GravityFallsMessage.encode(message, 'binary')
102
+ => "0101001101001000010011110101001001010100"
103
+
104
+ # encode a message using the caesar cipher
105
+ > message = "WELCOME TO GRAVITY FALLS."
106
+ => "WELCOME TO GRAVITY FALLS."
107
+ > GravityFallsMessage.encode(message, 'caesar')
108
+ => "ZHOFRPH WR JUDYLWB IDOOV."
109
+
110
+ # encode a message using the vigenere cipher
111
+ > message = "REMEMBER BIG HENRY"
112
+ => "REMEMBER BIG HENRY"
113
+ > GravityFallsMessage.encode(message, 'vigenere', key: 'WHATEVS')
114
+ => "NLMXQWWN IIZ LZFNF"
115
+ ```
116
+
117
+ ## Contributing
118
+
119
+ 1. Fork it ( https://github.com/[my-github-username]/gravity_falls_message/fork )
120
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
121
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
122
+ 4. Push to the branch (`git push origin my-new-feature`)
123
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs = ['lib', 'test']
7
+ t.test_files = FileList['test/test_helper.rb', 'test/*_test.rb']
8
+ end
9
+
10
+ task default: :test
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gravity_falls_message/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gravity_falls_message"
8
+ spec.version = GravityFallsMessage::VERSION
9
+ spec.authors = ["Brian Hall"]
10
+ spec.email = ["brian@brianhalldeveloper.com"]
11
+ spec.summary = %q{A gem to encode and decode messages using the ciphers as presented on Disney XD's show Gravity Falls.}
12
+ spec.description = %q{}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = '~> 2.1.2'
22
+ spec.test_files = Dir.glob("test/**/*")
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.7"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency 'minitest-reporters', '~> 1.0.6'
27
+ spec.add_development_dependency 'shoulda', '~> 3.5.0'
28
+ spec.add_development_dependency 'pry'
29
+ end
@@ -0,0 +1,32 @@
1
+ require "gravity_falls_message/version"
2
+ require 'gravity_falls_message/cipher'
3
+
4
+ module GravityFallsMessage
5
+
6
+ def self.decode message, cipher, options={}
7
+ if cipher != 'binary'
8
+ message = cipher == 'a1z26' ? message.split(/( |\W)/) : message.split('')
9
+ end
10
+ # raise something if cipher == 'vigenere' && options[:key].nil?
11
+ case cipher
12
+ when 'a1z26' then Cipher.a1z26(message)
13
+ when 'atbash' then Cipher.atbash(message)
14
+ when 'binary' then Cipher.binary(message)
15
+ when 'caesar' then Cipher.caesar(message, options)
16
+ when 'vigenere' then Cipher.vigenere(message, options)
17
+ end
18
+ end
19
+
20
+ def self.encode message, cipher, options={}
21
+ message = message.split(//) if cipher != 'binary'
22
+
23
+ case cipher
24
+ when 'a1z26' then Cipher.a1z26(message, true)
25
+ when 'atbash' then Cipher.atbash(message, true)
26
+ when 'binary' then Cipher.binary(message, true)
27
+ when 'caesar' then Cipher.caesar(message, options.merge(encode: true))
28
+ when 'vigenere' then Cipher.vigenere(message, options.merge(encode: true))
29
+ end
30
+ end
31
+
32
+ end
@@ -0,0 +1,66 @@
1
+ module GravityFallsMessage
2
+ class Cipher
3
+
4
+ def self.alphabet
5
+ ('A'..'Z').to_a
6
+ end
7
+
8
+ def self.a1z26 arr, encode=false
9
+ answer = arr.collect do |c|
10
+ if /-/ === c
11
+ ''
12
+ elsif /\W/ === c
13
+ c
14
+ elsif c != ''
15
+ encode ? alphabet.index(c.upcase) + 1 : alphabet[c.to_i - 1]
16
+ end
17
+ end
18
+ encode ? answer.join('-').gsub(/-?(\s|")-/, '\1').gsub(/-(\W)-?/,'\1') : answer.join('')
19
+ end
20
+
21
+ def self.atbash arr, encode=false
22
+ answer = arr.collect do |c|
23
+ if /\W/ === c
24
+ c
25
+ else
26
+ encode ? alphabet[alphabet.reverse.index(c.upcase)] : alphabet.reverse[alphabet.index(c.upcase)]
27
+ end
28
+ end
29
+ answer.join('')
30
+ end
31
+
32
+ def self.binary input, encode=false
33
+ if encode
34
+ answer = input.unpack('B*')[0]
35
+ else
36
+ answer = ''
37
+ (0..input.length).step(8).each {|i| answer << input[i,8].to_i(2).chr}
38
+ end
39
+ answer.delete("\0")
40
+ end
41
+
42
+ def self.caesar arr, options={}
43
+ encode = options[:encode]
44
+ default_shift = options[:shift] || 3
45
+ key = options[:key].gsub(/[^A-Z]/i,'').upcase.split(//) if options[:key]
46
+ answer = arr.collect do |c|
47
+ val = key ? alphabet.index(key[0]) : default_shift
48
+ shift_value = encode ? val : (val * -1)
49
+ if /\W/ === c
50
+ c
51
+ else
52
+ ind = alphabet.index(c.upcase) + shift_value
53
+ ind = ind >= 26 ? ind - 26 : ind
54
+ key.rotate! if key
55
+ alphabet[ind]
56
+ end
57
+ end
58
+ answer.join('').upcase
59
+ end
60
+
61
+ def self.vigenere arr, options={}
62
+ caesar(arr, options)
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module GravityFallsMessage
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,102 @@
1
+ class GravityFallsMessageTest < Minitest::Test
2
+
3
+ def setup
4
+ @a1z26_crypts = {
5
+ "14-5-24-20 21-16: \"6-15-15-20-2-15-20 20-23-15: 7-18-21-14-11-12-5'19 7-18-5-22-5-14-7-5.\"" => "NEXT UP: \"FOOTBOT TWO: GRUNKLE'S GREVENGE.\"",
6
+ "18-5-22-5-18-19-5 20-8-5 3-9-16-8-5-18-19" => "REVERSE THE CIPHERS",
7
+ "8-1-16-16-25 14-15-23, 1-18-9-5-12?" => "HAPPY NOW, ARIEL?"
8
+ }
9
+
10
+ @atbash_crypts = {
11
+ "MLG S.T. DVOOH ZKKILEVW." => "NOT H.G. WELLS APPROVED.",
12
+ "KFIV VMVITB, MLG HPRM ZMW YLMV" => "PURE ENERGY, NOT SKIN AND BONE",
13
+ "IRHRMT ORPV GSV HSVKZIW GLMV" => "RISING LIKE THE SHEPARD TONE"
14
+ }
15
+
16
+ @binary_crypts = {
17
+ "0101000001010101" => "PU", "01010000010101010101010000100000010000010100110001001100001000000101001101001001010110000010000001010000010010010100010101000011010001010101001100100000010101000100111101000111010001010101010001001000010001010101001000100001" => "PUT ALL SIX PIECES TOGETHER!"
18
+ }
19
+
20
+ @caesar_crypts = {
21
+ 'ZLGGOH' => 'WIDDLE',
22
+ 'VKLIWHU' => 'SHIFTER',
23
+ 'ZKDWHYV' => 'WHATEVS',
24
+ 'EHDUR' => 'BEARO'
25
+ }
26
+
27
+ @vigenere_crypts = [
28
+ {
29
+ key: 'SHIFTER',
30
+ cryptogram: 'OOIY DMEV VN IBWRKAMW BRUWLL',
31
+ solution: 'WHAT KIND OF DISASTER INDEED'
32
+ },
33
+ {
34
+ key: 'BEARO',
35
+ cryptogram: 'BRTYMEMNX QBR HRRQPEE',
36
+ solution: 'ANTHYDING CAN HADPLEN'
37
+ }
38
+ ]
39
+ end
40
+
41
+ should "decode a message using the a1z26 cipher" do
42
+ @a1z26_crypts.each_pair do |cryptogram, solution|
43
+ assert_equal solution, GravityFallsMessage.decode(cryptogram, 'a1z26')
44
+ end
45
+ end
46
+
47
+ should "decode a message using the atbash cipher" do
48
+ @atbash_crypts.each_pair do |cryptogram, solution|
49
+ assert_equal solution, GravityFallsMessage.decode(cryptogram, 'atbash')
50
+ end
51
+ end
52
+
53
+ should "decode a message using binary" do
54
+ @binary_crypts.each_pair do |cryptogram, solution|
55
+ assert_equal solution, GravityFallsMessage.decode(cryptogram, 'binary')
56
+ end
57
+ end
58
+
59
+ should "decode a message using the caesar cipher" do
60
+ @caesar_crypts.each_pair do |cryptogram, solution|
61
+ assert_equal solution, GravityFallsMessage.decode(cryptogram, 'caesar')
62
+ end
63
+ end
64
+
65
+ should "decode a message using the vigenere cipher" do
66
+ @vigenere_crypts.each do |crypt|
67
+ assert_equal crypt[:solution], GravityFallsMessage.decode(crypt[:cryptogram], 'vigenere', {key: crypt[:key]})
68
+ end
69
+ end
70
+
71
+
72
+ should "encode a message using the a1z26 cipher" do
73
+ @a1z26_crypts.each_pair do |cryptogram, solution|
74
+ assert_equal cryptogram, GravityFallsMessage.encode(solution, 'a1z26')
75
+ end
76
+ end
77
+
78
+ should "encode a message using the atbash cipher" do
79
+ @atbash_crypts.each_pair do |cryptogram, solution|
80
+ assert_equal cryptogram, GravityFallsMessage.encode(solution, 'atbash')
81
+ end
82
+ end
83
+
84
+ should "encode a message using binary" do
85
+ @binary_crypts.each_pair do |cryptogram, solution|
86
+ assert_equal cryptogram, GravityFallsMessage.encode(solution, 'binary')
87
+ end
88
+ end
89
+
90
+ should "encode a message using the caesar cipher" do
91
+ @caesar_crypts.each_pair do |cryptogram, solution|
92
+ assert_equal cryptogram, GravityFallsMessage.encode(solution, 'caesar')
93
+ end
94
+ end
95
+
96
+ should "encode a message using the vigenere cipher" do
97
+ @vigenere_crypts.each do |crypt|
98
+ assert_equal crypt[:cryptogram], GravityFallsMessage.encode(crypt[:solution], 'vigenere', {key: crypt[:key]})
99
+ end
100
+ end
101
+
102
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ require 'minitest/autorun'
4
+ require 'minitest/reporters'
5
+ require 'shoulda/context'
6
+ require 'shoulda/matchers'
7
+ require 'pry'
8
+
9
+ require 'gravity_falls_message'
10
+
11
+ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gravity_falls_message
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brian Hall
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest-reporters
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.6
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.6
55
+ - !ruby/object:Gem::Dependency
56
+ name: shoulda
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.5.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.5.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: ''
84
+ email:
85
+ - brian@brianhalldeveloper.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - gravity_falls_message.gemspec
96
+ - lib/gravity_falls_message.rb
97
+ - lib/gravity_falls_message/cipher.rb
98
+ - lib/gravity_falls_message/version.rb
99
+ - test/gravity_falls_message_test.rb
100
+ - test/test_helper.rb
101
+ homepage: ''
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - "~>"
112
+ - !ruby/object:Gem::Version
113
+ version: 2.1.2
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.2.2
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: A gem to encode and decode messages using the ciphers as presented on Disney
125
+ XD's show Gravity Falls.
126
+ test_files:
127
+ - test/gravity_falls_message_test.rb
128
+ - test/test_helper.rb