Skip to content

Commit

Permalink
support morse code encode and decode
Browse files Browse the repository at this point in the history
  • Loading branch information
YingRui Lu committed Oct 4, 2018
0 parents commit df6b35e
Show file tree
Hide file tree
Showing 18 changed files with 323 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
@@ -0,0 +1,10 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
tags
Gemfile.lock
1 change: 1 addition & 0 deletions .rubocop.yml
@@ -0,0 +1 @@
inherit_from: .rubocop_todo.yml
28 changes: 28 additions & 0 deletions .rubocop_todo.yml
@@ -0,0 +1,28 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-10-04 16:16:14 +0800 using RuboCop version 0.59.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
Style/Documentation:
Exclude:
- 'spec/**/*'
- 'test/**/*'
- 'lib/morse_code/encoder.rb'

# Offense count: 6
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 654

Metrics/BlockLength:
Exclude:
- 'morse_code.gemspec'

Layout/SpaceInsideArrayPercentLiteral:
Exclude:
- 'lib/morse_code.rb'
6 changes: 6 additions & 0 deletions Gemfile
@@ -0,0 +1,6 @@
source 'https://rubygems.org'

git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

# Specify your gem's dependencies in morse_code.gemspec
gemspec
42 changes: 42 additions & 0 deletions Guardfile
@@ -0,0 +1,42 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

## Uncomment and set this to only include directories you want to watch
# directories %w(app lib config test spec features) \
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}

## Note: if you are using the `directories` clause above and you are not
## watching the project directory ('.'), then you will want to move
## the Guardfile to a watched dir and symlink it back, e.g.
#
# $ mkdir config
# $ mv Guardfile config/
# $ ln -s config/Guardfile .
#
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"

guard :minitest do
# with Minitest::Unit
watch(%r{^test/(.*)\/?test_(.*)\.rb$})
watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/test_#{m[2]}.rb" }
watch(%r{^test/test_helper\.rb$}) { 'test' }

# with Minitest::Spec
# watch(%r{^spec/(.*)_spec\.rb$})
# watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
# watch(%r{^spec/spec_helper\.rb$}) { 'spec' }

# Rails 4
# watch(%r{^app/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
# watch(%r{^app/controllers/application_controller\.rb$}) { 'test/controllers' }
# watch(%r{^app/controllers/(.+)_controller\.rb$}) { |m| "test/integration/#{m[1]}_test.rb" }
# watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" }
# watch(%r{^lib/(.+)\.rb$}) { |m| "test/lib/#{m[1]}_test.rb" }
# watch(%r{^test/.+_test\.rb$})
# watch(%r{^test/test_helper\.rb$}) { 'test' }

# Rails < 4
# watch(%r{^app/controllers/(.*)\.rb$}) { |m| "test/functional/#{m[1]}_test.rb" }
# watch(%r{^app/helpers/(.*)\.rb$}) { |m| "test/helpers/#{m[1]}_test.rb" }
# watch(%r{^app/models/(.*)\.rb$}) { |m| "test/unit/#{m[1]}_test.rb" }
end
35 changes: 35 additions & 0 deletions README.md
@@ -0,0 +1,35 @@
# MorseCode

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/morse_code`. To experiment with that code, run `bin/console` for an interactive prompt.

TODO: Delete this and the text above, and describe your gem

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'morse_code'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install morse_code

## Usage

TODO: Write usage instructions here

## Development

After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

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).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/morse_code.
10 changes: 10 additions & 0 deletions Rakefile
@@ -0,0 +1,10 @@
require 'bundler/gem_tasks'
require 'rake/testtask'

Rake::TestTask.new(:test) do |t|
t.libs << 'test'
t.libs << 'lib'
t.test_files = FileList['test/**/test_*.rb']
end

task default: :test
11 changes: 11 additions & 0 deletions bin/console
@@ -0,0 +1,11 @@
#!/usr/bin/env ruby

require 'bundler/setup'
require 'morse_code'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
require "pry"
Pry.start
8 changes: 8 additions & 0 deletions bin/setup
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
37 changes: 37 additions & 0 deletions lib/morse_code.rb
@@ -0,0 +1,37 @@
require 'morse_code/version'
require 'morse_code/error'
require 'morse_code/encoder'
require 'morse_code/decoder'

module MorseCode
ENCODE_MAP = Hash[*%w/
A .- 1 .----
B -... 2 ..---
C -.-. 3 ...--
D -.. 4 ....-
E . 5 .....
F ..-. 6 -....
G --. 7 --...
H .... 8 ---..
I .. 9 ----.
J .--- 0 -----
K -.- . .-.-.-
L .-.. , --..--
M -- ? ..--..
N -. \/ -..-.
O --- : ---...
P .--. ; -.-.-.
Q --.- = -...-
R .-. + .-.-.
S ... - -....-
T - _ ..--.-
U ..- " .-..-.
V ...- $ ...-..-
W .-- @ .--.-.
X -..-
Y -.--
Z --..
/]

DECODE_MAP = ENCODE_MAP.invert
end
23 changes: 23 additions & 0 deletions lib/morse_code/decoder.rb
@@ -0,0 +1,23 @@
require 'morse_code/error'

module MorseCode
class Decoder
def initialize(content = '')
@content = content
end

def decode
[].tap do |decode_words|
@content.split(/\s+/).each do |word|
decode_words << (MorseCode::DECODE_MAP[word] || word)
end
end.join(' ')
end

def decode_with
@content.tap { |content| content.gsub!('DIT', '.'); content.gsub!('DAH', '-') }
decode
end
alias dit_dah_to decode_with
end
end
24 changes: 24 additions & 0 deletions lib/morse_code/encoder.rb
@@ -0,0 +1,24 @@
require 'morse_code/error'

module MorseCode
class Encoder
def initialize(content = '')
@content = content.upcase
end

def encode
[].tap do |encode_words|
@content.split(/\s+/).each do |word|
encode_word = []
word.each_char { |char| encode_word << (MorseCode::ENCODE_MAP[char] || char) }
encode_words.concat(encode_word)
end
end.join(' ')
end

def encode_with
encode.tap { |content| content.gsub!('.', 'DIT'); content.gsub!('-', 'DAH') }
end
alias dit_dah encode_with
end
end
5 changes: 5 additions & 0 deletions lib/morse_code/error.rb
@@ -0,0 +1,5 @@
module MorseCode
class Error < StandardError; end

class NotSupported < Error; end
end
3 changes: 3 additions & 0 deletions lib/morse_code/version.rb
@@ -0,0 +1,3 @@
module MorseCode
VERSION = '0.1.0'.freeze
end
39 changes: 39 additions & 0 deletions morse_code.gemspec
@@ -0,0 +1,39 @@
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'morse_code/version'

Gem::Specification.new do |spec|
spec.name = 'morse-code-rb'
spec.version = MorseCode::VERSION
spec.authors = ['YingRui Lu']
spec.email = ['luyingrui518@gmail.com']

spec.summary = 'Morse Code Tool'
spec.description = 'Morse Code Tool'
spec.homepage = 'https://github.com/superiorlu/morse_code'

# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
# to allow pushing to a single host or delete this section to allow pushing to any host.
if spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
else
raise 'RubyGems 2.0 or newer is required to protect against ' \
'public gem pushes.'
end

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir['lib/**/*.rb', 'README.md']
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ['lib']
spec.licenses = ['MIT']

spec.add_development_dependency 'bundler', '~> 1.16'
spec.add_development_dependency 'guard', '~> 2.14'
spec.add_development_dependency 'guard-minitest', '~> 2.4'
spec.add_development_dependency 'minitest', '~> 5.11'
spec.add_development_dependency 'minitest-reporters', '~> 1.3'
spec.add_development_dependency 'pry', '~> 0.11'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rubocop', '~> 0.59'
end
16 changes: 16 additions & 0 deletions test/test_decoder.rb
@@ -0,0 +1,16 @@
require 'test_helper'

class TestDecoder < Minitest::Test

def test_decode
content = '.. .- -- .-. --- -... --- - .-.-.-'
decode_content = MorseCode::Decoder.new(content).decode
assert_equal 'I A M R O B O T .', decode_content
end

def test_dit_dah_to
content = 'DITDIT DITDAH DAHDAH DITDAHDIT DAHDAHDAH DAHDITDITDIT DAHDAHDAH DAH DITDAHDITDAHDITDAH'
decode_content = MorseCode::Decoder.new(content).dit_dah_to
assert_equal 'I A M R O B O T .', decode_content
end
end
16 changes: 16 additions & 0 deletions test/test_encoder.rb
@@ -0,0 +1,16 @@
require 'test_helper'

class TestEncoder < Minitest::Test

def test_encode
content = 'I am Robot.'
encode_content = MorseCode::Encoder.new(content).encode
assert_equal '.. .- -- .-. --- -... --- - .-.-.-', encode_content
end

def test_dit_dah
content = 'I am Robot.'
encode_content = MorseCode::Encoder.new(content).dit_dah
assert_equal 'DITDIT DITDAH DAHDAH DITDAHDIT DAHDAHDAH DAHDITDITDIT DAHDAHDAH DAH DITDAHDITDAHDITDAH', encode_content
end
end
9 changes: 9 additions & 0 deletions test/test_helper.rb
@@ -0,0 +1,9 @@
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)

require 'pry'
require 'morse_code'

require 'minitest/autorun'

require 'minitest/reporters'
Minitest::Reporters.use! Minitest::Reporters::ProgressReporter.new

0 comments on commit df6b35e

Please sign in to comment.