slice_pizzabot 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6372fbbad64a043afa9e5a4931993877549c599183016444d538c9faa7efcf02
4
- data.tar.gz: 4a27d733824c379daf8ce1699b6185f6b1d69b2bf9d02349758744df483f7c42
3
+ metadata.gz: 96524b57c59a4e83a476b2c8ea5805bfe14a83b775be76379e45b63e462833bf
4
+ data.tar.gz: 7726b461068299e3a68c137e09e096661d2ebaf1708a980c340115830327a72a
5
5
  SHA512:
6
- metadata.gz: 7ea774e7828d2061b962375c7fac999e81604f027b3c0a7f65e8226a9c836b8369c482430ffae110e8f75d080adbf9924c2335b89217d2532e45096381e558ba
7
- data.tar.gz: fd348e305bc7c5e2cae757cf2d76f4436add489083f991ba954b8fe6a0f66dbc8803b10100e041f300c3b4f93aaab998acf316ae7161d08356d1b0037095b610
6
+ metadata.gz: cba7baac7f3ceef0122c8fdae9f2119879a8954b0cf649544a372cf04dcb0733385b325c55f0c25d9e48f9a6c14a93dd029651649804b84b3a5cf0c7c0a4e1fd
7
+ data.tar.gz: 33c7d8059a3944ca84891df680e298df1a6edf3411933fc01d591a615410cb71218be368638eaddbeef9c4708c67eae83c1c871790766afd0108763c2b1a4bbc
@@ -1,3 +1,3 @@
1
1
  require 'pizzabot'
2
2
 
3
- Pizzabot.new(ARGV[0]).deliver
3
+ Pizzabot.new(ARGV[0]).deliver
@@ -1,20 +1,22 @@
1
- require_relative 'pizzabot/route_service.rb'
1
+ require_relative 'pizzabot/route_service'
2
2
 
3
3
  class Pizzabot
4
+ NO_ARGUMENT_MESSAGE = 'You should provide delivery string to pizzabot,'\
5
+ ' example: pizzabot "2x2 (0,1) (0,2)"'
6
+ INITIAL_POSITION = [0, 0]
4
7
 
5
- NO_ARGUMENT_MESSAGE = 'You should provide delivery string to pizzabot, example: pizzabot "2x2 (0,1) (0,2)"'
8
+ attr_reader :route
9
+ attr_accessor :current_position, :delivery_process
6
10
 
7
- attr_reader :route
8
- attr_accessor :current_position, :delivery_process
9
-
10
- def initialize(delivery_string)
11
+ def initialize(delivery_string)
11
12
  raise ArgumentError, NO_ARGUMENT_MESSAGE unless delivery_string
12
- @current_position = [0, 0]
13
- @delivery_process = ''
14
- @route = RouteService.new(delivery_string).prepare_route
15
- end
16
13
 
17
- def deliver
14
+ @current_position = INITIAL_POSITION
15
+ @delivery_process = ''
16
+ @route = RouteService.new(delivery_string).prepare_route
17
+ end
18
+
19
+ def deliver
18
20
  route.each do |address|
19
21
  move_to(address)
20
22
  drop_pizza
@@ -27,43 +29,32 @@ class Pizzabot
27
29
  private
28
30
 
29
31
  def move_to(address)
30
- until current_position[0] == address[0] do
31
- if current_position[0] < address[0]
32
- move_east
33
- else
34
- move_west
35
- end
36
- end
37
- until current_position[1] == address[1] do
38
- if current_position[1] < address[1]
39
- move_north
40
- else
41
- move_south
42
- end
43
- end
32
+ current_position[0] < address[0] ? move_east : move_west until current_position[0] == address[0]
33
+
34
+ current_position[1] < address[1] ? move_north : move_south until current_position[1] == address[1]
44
35
  end
45
36
 
46
37
  def drop_pizza
47
- delivery_process << "D"
38
+ delivery_process << 'D'
48
39
  end
49
40
 
50
41
  def move_north
51
- current_position[1] += 1
52
- delivery_process << "N"
42
+ current_position[1] += 1
43
+ delivery_process << 'N'
53
44
  end
54
45
 
55
46
  def move_south
56
- current_position[1] -= 1
57
- delivery_process << "S"
47
+ current_position[1] -= 1
48
+ delivery_process << 'S'
58
49
  end
59
50
 
60
51
  def move_west
61
- current_position[0] -= 1
62
- delivery_process << "W"
52
+ current_position[0] -= 1
53
+ delivery_process << 'W'
63
54
  end
64
55
 
65
56
  def move_east
66
- current_position[0] += 1
67
- delivery_process << "E"
57
+ current_position[0] += 1
58
+ delivery_process << 'E'
68
59
  end
69
60
  end
@@ -2,4 +2,4 @@ class AddressError < StandardError
2
2
  def initialize(msg)
3
3
  super
4
4
  end
5
- end
5
+ end
@@ -1,5 +1,5 @@
1
1
  class GridError < StandardError
2
- def initialize(msg='Grid in wrong format')
2
+ def initialize(msg = 'Grid in wrong format')
3
3
  super
4
4
  end
5
- end
5
+ end
@@ -1,17 +1,19 @@
1
- require_relative 'grid_error.rb'
2
- require_relative 'address_error.rb'
1
+ require_relative 'grid_error'
2
+ require_relative 'address_error'
3
3
 
4
4
  class RouteService
5
-
6
- GRID_REGEX = /^\d+x\d+/
5
+ GRID_REGEX = /^\s*\d+x\d+/
7
6
  ADDRESSES_REGEX = /\((.*?)\)/
8
7
  DIGITS_EXTRACT_REGEX = /\d+/
9
8
 
10
- attr_reader :grid
9
+ NEGATIVE_NUMBERS_MESSAGE = 'Address should not have negative numbers'
10
+ NOT_IN_RANGE_MESSAGE = 'Address is not in delivery range'
11
+
12
+ attr_reader :grid
11
13
  attr_accessor :addresses
12
14
 
13
15
  def initialize(delivery_string)
14
- @grid = delivery_string[GRID_REGEX]
16
+ @grid = delivery_string[GRID_REGEX]&.split('x')&.map(&:to_i)
15
17
  @addresses = delivery_string.scan(ADDRESSES_REGEX).flatten
16
18
  end
17
19
 
@@ -23,15 +25,17 @@ class RouteService
23
25
  private
24
26
 
25
27
  def validate_route
26
- raise GridError unless grid
27
- grid_values = grid.split('x').map(&:to_i)
28
+ raise GridError unless grid
28
29
 
29
30
  addresses.map! do |address|
31
+ raise AddressError, NEGATIVE_NUMBERS_MESSAGE if address.include?('-')
32
+
30
33
  extracted_array = address.scan(DIGITS_EXTRACT_REGEX).map(&:to_i)
31
- raise AddressError.new("Address in wrong format: #{address}") unless extracted_array.length == 2
32
- raise AddressError.new("Address is not in delivery range") if extracted_array[0] > grid_values[0] ||
33
- extracted_array[1] > grid_values[1]
34
- extracted_array
34
+ raise AddressError, "Wrong format address: #{address}" unless extracted_array.length == 2
35
+ raise AddressError, NOT_IN_RANGE_MESSAGE if extracted_array[0] > grid[0] ||
36
+ extracted_array[1] > grid[1]
37
+
38
+ extracted_array
35
39
  end
36
40
  end
37
41
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slice_pizzabot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aliaksei Khalupau