morse_code 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 07dd340bee0ff99a5eec4fee622995983a77f999
4
+ data.tar.gz: a4e2bc8a25a04856811bc6d2520d82da33ab0059
5
+ SHA512:
6
+ metadata.gz: bf15419bc5d6400a1b0d4fd6886baaa8b4cd7897f7d3fe7f130c2ebb92ef866d2133d592f18450c1aac287f89b1c63b124b5bf9710ca65865e338f008232bf41
7
+ data.tar.gz: 9b354f2060303503a1b273d0bcc922005d2cb8809b40b9381033ba781089668da8119ba9cabc4672a5e890b8f0c922f1e31ea50b7aa6cc7269ff3355250874b8
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in morse_code.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Horst Mumpitz
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,53 @@
1
+ # MorseCode
2
+
3
+ MorseCode is a small Library which enables the conversion of morse codes back and forth. As a GeoCaching advocat it is sometimes pretty usefull to add morse code abilities Ruby and your IRB. Only latin letters and arabic numbers are supported. It might change in the future.
4
+
5
+ ## Dependencies
6
+ Ruby >= 2.1
7
+ As this Library is using refinements please make sure that you're using a current Ruby version.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'morse_code'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install morse_code
22
+
23
+ ## Usage
24
+
25
+ The usage is pretty easy. Please make sure that your String is encoded with . (dit) and - (dah).
26
+
27
+ ### Morse Code to String
28
+ morse_code = MorseCode::Converter.new("..-. --- --- -... .- .-.")
29
+ morse_code.convert
30
+
31
+ ### Check if String is a valid Morse Code
32
+ MorseCode::Converter.new("foobar").valid?
33
+
34
+ ### Filter only Morse Codes from a String
35
+ MorseCode::Converter.new("$..-. --- --- -... .- .-.$").clean_text
36
+
37
+ ### String to Morse Code
38
+ using MorseCode::Refinements
39
+ "foobar".to_morse_code
40
+
41
+ ## License
42
+ MIT
43
+
44
+ ## Trouble?
45
+ Just write an issue!
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'lib'
6
+ t.verbose = true
7
+ t.test_files = FileList['test/*_spec.rb']
8
+ end
@@ -0,0 +1,90 @@
1
+ module MorseCode::Constants
2
+
3
+ ALPHABET = {
4
+ ".-" => "A",
5
+ "-..." => "B",
6
+ "-.-." => "C",
7
+ "-.." => "D",
8
+ "." => "E",
9
+ "..-." => "F",
10
+ "--." => "G",
11
+ "...." => "H",
12
+ ".." => "I",
13
+ ".---" => "J",
14
+ "-.-" => "K",
15
+ ".-.." => "L",
16
+ "--" => "M",
17
+ "-." => "N",
18
+ "---" => "O",
19
+ ".--." => "P",
20
+ "--.-" => "Q",
21
+ ".-." => "R",
22
+ "..." => "S",
23
+ "-" => "T",
24
+ "..-" => "U",
25
+ "...-" => "V",
26
+ ".--" => "W",
27
+ "-..-" => "X",
28
+ "-.--" => "Y",
29
+ "--.." => "Z"
30
+ }
31
+
32
+ NUMBERS = {
33
+ "-----" => "0",
34
+ ".----" => "1",
35
+ "..---" => "2",
36
+ "...--" => "3",
37
+ "....-" => "4",
38
+ "....." => "5",
39
+ "-...." => "6",
40
+ "--..." => "7",
41
+ "---.." => "8",
42
+ "----." => "9"
43
+ }
44
+
45
+ FULL_CODE = ALPHABET.merge(NUMBERS)
46
+
47
+ LATIN_ALPHABET = {
48
+ "A" => ".-",
49
+ "B" => "-...",
50
+ "C" => "-.-.",
51
+ "D" => "-..",
52
+ "E" => ".",
53
+ "F" => "..-.",
54
+ "G" => "--.",
55
+ "H" => "....",
56
+ "I" => "..",
57
+ "J" => ".---",
58
+ "K" => "-.-",
59
+ "L" => ".-..",
60
+ "M" => "--",
61
+ "N" => "-.",
62
+ "O" => "---",
63
+ "P" => ".--.",
64
+ "Q" => "--.-",
65
+ "R" => ".-.",
66
+ "S" => "...",
67
+ "T" => "-",
68
+ "U" => "..-",
69
+ "V" => "...-",
70
+ "W" => ".--",
71
+ "X" => "-..-",
72
+ "Y" => "-.--",
73
+ "Z" => "--.."
74
+ }
75
+
76
+ ARABIC_NUMBERS = {
77
+ "0" => "-----",
78
+ "1" => ".----",
79
+ "2" => "..---",
80
+ "3" => "...--",
81
+ "4" => "....-",
82
+ "5" => ".....",
83
+ "6" => "-....",
84
+ "7" => "--...",
85
+ "8" => "---..",
86
+ "9" => "----."
87
+ }
88
+
89
+ FULL_ALPHA_NUMERIC_CODE = LATIN_ALPHABET.merge(ARABIC_NUMBERS)
90
+ end
@@ -0,0 +1,48 @@
1
+
2
+ class MorseCode::Converter
3
+
4
+ def initialize(text)
5
+ @text = text
6
+ end
7
+
8
+ def clean_text
9
+ cleaned_text = ""
10
+ @text.split(" ").each do |potential_morse_code|
11
+ latin_character = MorseCode::Constants::FULL_CODE[potential_morse_code]
12
+ cleaned_text << [potential_morse_code, " "].join if latin_character
13
+ end
14
+ cleaned_text.strip
15
+ end
16
+
17
+ def valid?
18
+ @text.split(" ").each do |potential_morse_code|
19
+ return false unless MorseCode::Constants::FULL_CODE[potential_morse_code]
20
+ end
21
+ true
22
+ end
23
+
24
+ def convert
25
+ translated_text = ""
26
+
27
+ if @text.lines.count > 1
28
+ @text.each_line do |line|
29
+ translated_text << convert_line(line)
30
+ translated_text << "\n"
31
+ end
32
+ else
33
+ translated_text << convert_line(@text)
34
+ end
35
+ translated_text
36
+ end
37
+
38
+ def convert_line(line)
39
+ converted_text = ""
40
+ line.strip.split(" ").each do |potential_morse_code|
41
+ latin_character = MorseCode::Constants::FULL_CODE[potential_morse_code]
42
+ converted_text << latin_character if latin_character
43
+ end
44
+ converted_text
45
+ end
46
+
47
+
48
+ end
@@ -0,0 +1,12 @@
1
+ module MorseCode::Refinements
2
+ refine String do
3
+ def to_morse_code
4
+ converted_text = ""
5
+ self.chars.each do |character|
6
+ converted_character = MorseCode::Constants::FULL_ALPHA_NUMERIC_CODE[character.capitalize]
7
+ converted_text << [converted_character, " "].join if converted_character
8
+ end
9
+ converted_text.strip
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module MorseCode
2
+ VERSION = "0.1"
3
+ end
data/lib/morse_code.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "morse_code/version"
2
+ require "morse_code/constants"
3
+ require "morse_code/converter"
4
+ require "morse_code/refinements/string"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'morse_code/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "morse_code"
8
+ spec.version = MorseCode::VERSION
9
+ spec.authors = ["Horst Mumpitz"]
10
+ spec.email = ["denniskluge@me.com"]
11
+ spec.description = %q{A small Library which converst morse codes back and forth}
12
+ spec.summary = %q{A small Library which converst morse codes back and forth}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+ end
@@ -0,0 +1,36 @@
1
+ require "minitest/autorun"
2
+ require "morse_code"
3
+
4
+ describe MorseCode::Constants do
5
+ it "includes a hash with the morse code as key and the latin letter as value" do
6
+ MorseCode::Constants::ALPHABET.must_be_kind_of Hash
7
+ MorseCode::Constants::ALPHABET[".-"].must_equal "A"
8
+ end
9
+
10
+ it "includes a hash with the morse code as key and the arabic number as value" do
11
+ MorseCode::Constants::NUMBERS.must_be_kind_of Hash
12
+ MorseCode::Constants::NUMBERS["-----"].must_equal "0"
13
+ end
14
+
15
+ it "includes a hash with merged alphabet and numbers" do
16
+ MorseCode::Constants::FULL_CODE.must_be_kind_of Hash
17
+ MorseCode::Constants::FULL_CODE[".-"].must_equal "A"
18
+ MorseCode::Constants::NUMBERS["-----"].must_equal "0"
19
+ end
20
+
21
+ it "includes a hash with the latin latter as key and the morse code equilavent as value" do
22
+ MorseCode::Constants::LATIN_ALPHABET.must_be_kind_of Hash
23
+ MorseCode::Constants::LATIN_ALPHABET["A"].must_equal ".-"
24
+ end
25
+
26
+ it "includes a hash with the arabic numbers as key and the morse code equilavent as value" do
27
+ MorseCode::Constants::ARABIC_NUMBERS.must_be_kind_of Hash
28
+ MorseCode::Constants::ARABIC_NUMBERS["0"].must_equal "-----"
29
+ end
30
+
31
+ it "includes a hash with merged latin characters and numbers" do
32
+ MorseCode::Constants::FULL_ALPHA_NUMERIC_CODE.must_be_kind_of Hash
33
+ MorseCode::Constants::FULL_ALPHA_NUMERIC_CODE["A"].must_equal ".-"
34
+ MorseCode::Constants::FULL_ALPHA_NUMERIC_CODE["0"].must_equal "-----"
35
+ end
36
+ end
@@ -0,0 +1,34 @@
1
+ require "minitest/autorun"
2
+ require "morse_code"
3
+
4
+ describe MorseCode::Converter do
5
+ it "cleans a string from invalid characters" do
6
+ unclean_morse_code = MorseCode::Converter.new(".- foobar $ # * % .... --. ")
7
+ unclean_morse_code.clean_text.must_equal ".- .... --."
8
+ end
9
+
10
+ it "checks if the given morse code is invalid" do
11
+ invalid_morse_code = MorseCode::Converter.new("foobar")
12
+ invalid_morse_code.valid?.must_equal false
13
+ end
14
+
15
+ it "checks if the given morse code is valid" do
16
+ invalid_morse_code = MorseCode::Converter.new(".- ---")
17
+ invalid_morse_code.valid?.must_equal true
18
+ end
19
+
20
+ it "converts a valid morse code to a readable text" do
21
+ morse_code = MorseCode::Converter.new(".... . .-.. .-.. --- .-- --- .-. .-.. -..")
22
+ morse_code.convert.must_equal "HELLOWORLD"
23
+ end
24
+
25
+ it "converts a valid multi line morse code to a readable text" do
26
+ morse_code = MorseCode::Converter.new(".- .- .- \n ----- ----- --... \n ----- ----- --...")
27
+ morse_code.convert.must_equal "AAA\n007\n007\n"
28
+ end
29
+
30
+ it "does not converts a valid morse code to a readable text" do
31
+ morse_code = MorseCode::Converter.new("foobar")
32
+ morse_code.convert.must_equal ""
33
+ end
34
+ end
@@ -0,0 +1,14 @@
1
+ require "minitest/autorun"
2
+ require "morse_code"
3
+
4
+ using MorseCode::Refinements
5
+
6
+ describe String do
7
+ it "converts a string to its morse code equilavent" do
8
+ "hello world".to_morse_code.must_equal ".... . .-.. .-.. --- .-- --- .-. .-.. -.."
9
+ end
10
+
11
+ it "eliminates non convertable characters" do
12
+ "´´hello world§§".to_morse_code.must_equal ".... . .-.. .-.. --- .-- --- .-. .-.. -.."
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: morse_code
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Horst Mumpitz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-07 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A small Library which converst morse codes back and forth
56
+ email:
57
+ - denniskluge@me.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/morse_code.rb
68
+ - lib/morse_code/constants.rb
69
+ - lib/morse_code/converter.rb
70
+ - lib/morse_code/refinements/string.rb
71
+ - lib/morse_code/version.rb
72
+ - morse_code.gemspec
73
+ - test/constants_spec.rb
74
+ - test/converter_spec.rb
75
+ - test/string_refinements_spec.rb
76
+ homepage: ''
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: A small Library which converst morse codes back and forth
100
+ test_files:
101
+ - test/constants_spec.rb
102
+ - test/converter_spec.rb
103
+ - test/string_refinements_spec.rb