Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
/.bundle
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.5.0
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.0'

gem 'sinatra', '~> 2.0.4'
gem 'grape', '~> 1.1.0'
gem 'puma', '~> 3.11'
gem 'http', '~> 3.3.0'
85 changes: 85 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
GEM
remote: https://rubygems.org/
specs:
activesupport (5.2.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
axiom-types (0.1.1)
descendants_tracker (~> 0.0.4)
ice_nine (~> 0.11.0)
thread_safe (~> 0.3, >= 0.3.1)
builder (3.2.3)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
concurrent-ruby (1.1.4)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
domain_name (0.5.20180417)
unf (>= 0.0.5, < 1.0.0)
equalizer (0.0.11)
grape (1.1.0)
activesupport
builder
mustermann-grape (~> 1.0.0)
rack (>= 1.3.0)
rack-accept
virtus (>= 1.0.0)
http (3.3.0)
addressable (~> 2.3)
http-cookie (~> 1.0)
http-form_data (~> 2.0)
http_parser.rb (~> 0.6.0)
http-cookie (1.0.3)
domain_name (~> 0.5)
http-form_data (2.1.1)
http_parser.rb (0.6.0)
i18n (1.5.3)
concurrent-ruby (~> 1.0)
ice_nine (0.11.2)
minitest (5.11.3)
mustermann (1.0.3)
mustermann-grape (1.0.0)
mustermann (~> 1.0.0)
public_suffix (3.0.3)
puma (3.12.0)
rack (2.0.6)
rack-accept (0.4.5)
rack (>= 0.4)
rack-protection (2.0.4)
rack
sinatra (2.0.4)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.4)
tilt (~> 2.0)
thread_safe (0.3.6)
tilt (2.0.9)
tzinfo (1.2.5)
thread_safe (~> 0.1)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.5)
virtus (1.0.5)
axiom-types (~> 0.1)
coercible (~> 1.0)
descendants_tracker (~> 0.0, >= 0.0.3)
equalizer (~> 0.0, >= 0.0.9)

PLATFORMS
ruby

DEPENDENCIES
grape (~> 1.1.0)
http (~> 3.3.0)
puma (~> 3.11)
sinatra (~> 2.0.4)

RUBY VERSION
ruby 2.5.0p0

BUNDLED WITH
1.16.5
71 changes: 69 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,69 @@
# rps
Rock-Paper-Scissors
# How to install

1. Pull the source code from repository.
2. Change current directory to project's root.
3. Install gems by executing command `bundle install`.
4. Please readme `How to test` section below to try it.

# How to test

NOTE that UI isn't implemented.
That's why it's possible to test only API.

**How to test:**

1. From source directory run application server with command `puma -p 3000`.
2. You can test `POST /play` endpoint with single parameter `choice` which accepts any string.
Please see the CURL examples below.

# CURL examples

Request:
```bash
curl -d "choice=rock" -X POST http://localhost:3000/play
```

Response:
```json
{
"user_choice":"rock",
"robot_choice":"paper",
"result":{
"winner":"robot",
"message":"paper beats rock"
}
}
```

Request:
```bash
curl -d "choice=hammer" -X POST http://localhost:3000/play
```

Response:
```json
{
"user_choice":"hammer",
"robot_choice":"rock",
"result":{
"winner":"user",
"message":"hammer beats rock"
}
}
```

Request:
```bash
curl -d "choice=scissors" -X POST http://localhost:3000/play
```

```json
{
"user_choice":"scissors",
"robot_choice":"scissors",
"result":{
"winner":"tie",
"message":"in this game scissors is the same as scissors"
}
}
```
3 changes: 3 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require_relative './init'

run Rack::Cascade.new [Web, Api]
19 changes: 19 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'bundler/setup'
Bundler.require(:default)

require_relative 'lib/choice'
require_relative 'lib/choice/beatable'
require_relative 'lib/choice/base'
require_relative 'lib/choice/rock'
require_relative 'lib/choice/paper'
require_relative 'lib/choice/scissors'
require_relative 'lib/choice/hammer'
require_relative 'lib/choice/unknown'

require_relative 'lib/robot_throw/base'
require_relative 'lib/robot_throw/curb_api'
require_relative 'lib/robot_throw/random'
require_relative 'lib/robot_throw'

require_relative 'lib/web'
require_relative 'lib/api'
28 changes: 28 additions & 0 deletions lib/api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Api < Grape::API
format :json

params do
requires :choice, type: Choice::Base, coerce_with: ->(value) { Choice.by(value).new(value) }
end

post :play do
user_choice = params[:choice]
robot_choice = RobotThrow.throw

if user_choice.class.beats?(robot_choice.class)
result = { winner: 'user', message: "#{user_choice} beats #{robot_choice}" }
elsif user_choice.class.beaten?(robot_choice.class)
result = { winner: 'robot', message: "#{robot_choice} beats #{user_choice}" }
elsif user_choice.class.tie?(robot_choice.class)
result = { winner: 'tie', message: "in this game #{robot_choice} is the same as #{user_choice}" }
else
raise StandardError, 'something went wrong, please let developers know :)'
end

present(
user_choice: user_choice.to_s,
robot_choice: robot_choice.to_s,
result: result
)
end
end
12 changes: 12 additions & 0 deletions lib/choice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Choice

def self.by(name)
class_name = name.to_s.downcase.strip.capitalize

if const_defined?(class_name)
const_get(class_name, false)
else
Unknown
end
end
end
13 changes: 13 additions & 0 deletions lib/choice/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Choice
class Base
extend Beatable

def initialize(original_name = nil)
@original_name = original_name || self.class.name.split('::').last.downcase
end

def to_s
@original_name.to_s
end
end
end
19 changes: 19 additions & 0 deletions lib/choice/beatable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Choice
module Beatable
def beats
raise NotImplementedError
end

def beats?(choice)
beats.include?(choice)
end

def beaten?(choice)
choice.beats?(self)
end

def tie?(choice)
!beats?(choice) && !beaten?(choice)
end
end
end
7 changes: 7 additions & 0 deletions lib/choice/hammer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Choice
class Hammer < Base
def self.beats
[Paper, Rock, Scissors]
end
end
end
7 changes: 7 additions & 0 deletions lib/choice/paper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Choice
class Paper < Base
def self.beats
[Rock]
end
end
end
7 changes: 7 additions & 0 deletions lib/choice/rock.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Choice
class Rock < Base
def self.beats
[Scissors]
end
end
end
7 changes: 7 additions & 0 deletions lib/choice/scissors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Choice
class Scissors < Base
def self.beats
[Paper]
end
end
end
8 changes: 8 additions & 0 deletions lib/choice/unknown.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Choice
class Unknown < Base

def self.beats
[]
end
end
end
16 changes: 16 additions & 0 deletions lib/robot_throw.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module RobotThrow

module Adapters
CURB_API = CurbApi
RANDOM = Random

ALL = [CURB_API, RANDOM].freeze
end

def self.throw
Adapters::ALL.find do |adapter|
choice = adapter.new.throw
return choice if choice
end
end
end
8 changes: 8 additions & 0 deletions lib/robot_throw/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module RobotThrow
class Base

def throw
raise NotImplementedError
end
end
end
27 changes: 27 additions & 0 deletions lib/robot_throw/curb_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module RobotThrow
class CurbApi < Base
ENDPOINT = 'https://5eddt4q9dk.execute-api.us-east-1.amazonaws.com/rps-stage/throw'
TIMEOUT = 3

def throw
response = HTTP.timeout(connect: TIMEOUT, read: TIMEOUT).get(ENDPOINT)
return unless success?(response)

curb_choice = response.parse.fetch('body')
curb_choice = curb_choice.delete('"')

Choice.by(curb_choice).new(curb_choice)

rescue StandardError
nil
end

private

def success?(response)
response.status.success? && response.parse['statusCode'] == 200
end
end
end
8 changes: 8 additions & 0 deletions lib/robot_throw/random.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module RobotThrow
class Random < Base

def throw
[Choice::Rock, Choice::Paper, Choice::Scissors].sample.new
end
end
end
5 changes: 5 additions & 0 deletions lib/web.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Web < Sinatra::Base
get '/' do
'Please read README.md to see instructions'
end
end