sitehub 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +17 -0
  3. data/Gemfile +0 -2
  4. data/Gemfile.lock +15 -1
  5. data/README.md +23 -0
  6. data/Rakefile +3 -9
  7. data/circle.yml +6 -0
  8. data/lib/sitehub/builder.rb +19 -19
  9. data/lib/sitehub/collection/route_collection.rb +7 -9
  10. data/lib/sitehub/collection/split_route_collection/split.rb +7 -5
  11. data/lib/sitehub/collection/split_route_collection.rb +13 -10
  12. data/lib/sitehub/collection.rb +26 -21
  13. data/lib/sitehub/constants/http_header_keys.rb +2 -6
  14. data/lib/sitehub/constants/rack_http_header_keys.rb +2 -2
  15. data/lib/sitehub/constants.rb +3 -3
  16. data/lib/sitehub/cookie/attribute.rb +4 -4
  17. data/lib/sitehub/cookie/flag.rb +4 -5
  18. data/lib/sitehub/cookie.rb +10 -14
  19. data/lib/sitehub/cookie_rewriting.rb +11 -13
  20. data/lib/sitehub/forward_proxies.rb +12 -13
  21. data/lib/sitehub/forward_proxy.rb +38 -24
  22. data/lib/sitehub/forward_proxy_builder.rb +38 -21
  23. data/lib/sitehub/http_headers.rb +39 -26
  24. data/lib/sitehub/logging/access_logger.rb +39 -35
  25. data/lib/sitehub/logging/error_logger.rb +7 -7
  26. data/lib/sitehub/logging/log_entry.rb +6 -5
  27. data/lib/sitehub/logging/log_stash.rb +2 -2
  28. data/lib/sitehub/logging/log_wrapper.rb +5 -5
  29. data/lib/sitehub/logging.rb +1 -1
  30. data/lib/sitehub/middleware.rb +2 -2
  31. data/lib/sitehub/path_directive.rb +10 -10
  32. data/lib/sitehub/path_directives.rb +5 -4
  33. data/lib/sitehub/request_mapping.rb +13 -11
  34. data/lib/sitehub/resolver.rb +5 -6
  35. data/lib/sitehub/reverse_proxy.rb +16 -12
  36. data/lib/sitehub/rules.rb +2 -2
  37. data/lib/sitehub/string_sanitiser.rb +1 -1
  38. data/lib/sitehub/transaction_id.rb +3 -3
  39. data/lib/sitehub/version.rb +1 -1
  40. data/lib/sitehub.rb +1 -1
  41. data/sitehub.gemspec +27 -29
  42. data/spec/sitehub/builder_spec.rb +18 -20
  43. data/spec/sitehub/collection/route_collection_spec.rb +16 -14
  44. data/spec/sitehub/collection/split_route_collection_spec.rb +8 -10
  45. data/spec/sitehub/collection_spec.rb +7 -7
  46. data/spec/sitehub/cookie/attribute_spec.rb +3 -3
  47. data/spec/sitehub/cookie/flag_spec.rb +2 -2
  48. data/spec/sitehub/cookie_rewriting_spec.rb +15 -12
  49. data/spec/sitehub/cookie_spec.rb +7 -18
  50. data/spec/sitehub/error_handling_spec.rb +2 -3
  51. data/spec/sitehub/forward_proxies_spec.rb +16 -12
  52. data/spec/sitehub/forward_proxy_builder_spec.rb +53 -30
  53. data/spec/sitehub/forward_proxy_spec.rb +26 -22
  54. data/spec/sitehub/http_headers_spec.rb +17 -18
  55. data/spec/sitehub/integration_spec.rb +4 -5
  56. data/spec/sitehub/logging/access_logger_spec.rb +25 -24
  57. data/spec/sitehub/logging/error_logger_spec.rb +5 -7
  58. data/spec/sitehub/logging/log_entry_spec.rb +2 -5
  59. data/spec/sitehub/logging/log_stash_spec.rb +1 -3
  60. data/spec/sitehub/logging/log_wrapper_spec.rb +0 -4
  61. data/spec/sitehub/middleware_spec.rb +1 -5
  62. data/spec/sitehub/path_directive_spec.rb +4 -7
  63. data/spec/sitehub/path_directives_spec.rb +6 -7
  64. data/spec/sitehub/request_mapping_spec.rb +2 -5
  65. data/spec/sitehub/resolver_spec.rb +1 -1
  66. data/spec/sitehub/reverse_proxy_spec.rb +37 -31
  67. data/spec/sitehub/transaction_id_spec.rb +3 -3
  68. data/spec/sitehub_spec.rb +2 -4
  69. data/spec/support/async/callback.rb +11 -0
  70. data/spec/support/async/middleware.rb +25 -0
  71. data/spec/support/async/response_handler.rb +16 -0
  72. data/spec/support/async.rb +4 -0
  73. data/spec/support/patch/rack/response.rb +13 -21
  74. data/spec/support/shared_contexts/async_context.rb +3 -58
  75. data/spec/support/shared_contexts/middleware_context.rb +15 -17
  76. data/spec/support/shared_contexts/rack_test_context.rb +3 -3
  77. data/spec/support/shared_contexts/sitehub_context.rb +9 -4
  78. data/spec/support/silent_warnings.rb +2 -3
  79. data/tasks/code_quality.rake +15 -0
  80. data/tasks/gem_tasks.rake +1 -0
  81. data/tasks/support/console.rb +7 -0
  82. data/tasks/testing.rake +4 -0
  83. data/tasks/util_tasks.rake +7 -0
  84. metadata +27 -3
  85. data/spec/basket_spec.rb +0 -30
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: af77a45a381dc3d5db1d15d6d4934dd97fcfcf14
4
- data.tar.gz: 365f724e5ecc280935665cadc36dbab16b45fe34
3
+ metadata.gz: 7a6e0a6953fd3b5d361c2e65e2b0b015a22a36b8
4
+ data.tar.gz: 76336ecd1253d8cf8e593e6bc67a1912b9fde469
5
5
  SHA512:
6
- metadata.gz: 5a263ad7fde6a5f68b9c3480ae0765b302918cdbd1eccd30bff05704ed9054c171b7c291932bbc5632b32b30162237c5c586e84e9616392a4604e35cff944e74
7
- data.tar.gz: 48358f5f31aae39ff3d70a3bc83e2760b53b37b46046c2cd28eddf53537aa3b607bcc22dce3143a7ac41acc387898660b55d87e51c99196532be4a38412df0f6
6
+ metadata.gz: ecd3cd69815ab6d8755e222e0fbd17ee4b92f09472f67272da3b5f407be8b4a9954009d6b5563e7320ffda0db6b7b5aa8dce8e71e4d39e72f108fa5c522687d0
7
+ data.tar.gz: e17c0f2deb78badac60445ac189103bb193687ed073202edcf1e12282aaff4e22e77c7bc74596133f3b8d069796bcefda52a01b0d1b19368c8ce966ec9095b07
data/.rubocop.yml ADDED
@@ -0,0 +1,17 @@
1
+ Metrics/LineLength:
2
+ Max: 120
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 2.1
6
+ Exclude:
7
+ - 'pkg/**/*'
8
+ - 'vendor/**/*'
9
+ - 'coverage/**/*'
10
+ - '.idea/**/*'
11
+ - '*.gemspec'
12
+
13
+ Style/Documentation:
14
+ Enabled: false
15
+
16
+ Metrics/MethodLength:
17
+ Max: 11
data/Gemfile CHANGED
@@ -2,5 +2,3 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in sitehub.gemspec
4
4
  gemspec
5
-
6
-
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sitehub (0.4.2)
4
+ sitehub (0.4.3)
5
5
  em-http-request
6
6
  em-synchrony
7
7
  faraday
@@ -15,6 +15,7 @@ GEM
15
15
  remote: https://rubygems.org/
16
16
  specs:
17
17
  addressable (2.3.8)
18
+ ast (2.2.0)
18
19
  builder (3.2.2)
19
20
  cookiejar (0.3.0)
20
21
  crack (0.4.3)
@@ -50,6 +51,9 @@ GEM
50
51
  memory_profiler (0.9.6)
51
52
  multipart-post (2.0.0)
52
53
  nesty (1.0.2)
54
+ parser (2.3.1.0)
55
+ ast (~> 2.2)
56
+ powerpack (0.1.1)
53
57
  rack (1.6.4)
54
58
  rack-fiber_pool (0.9.3)
55
59
  rack-protection (1.5.3)
@@ -57,6 +61,7 @@ GEM
57
61
  rack-ssl-enforcer (0.2.9)
58
62
  rack-test (0.6.3)
59
63
  rack (>= 1.0)
64
+ rainbow (2.1.0)
60
65
  rake (10.4.2)
61
66
  rspec (3.2.0)
62
67
  rspec-core (~> 3.2.0)
@@ -71,7 +76,14 @@ GEM
71
76
  diff-lcs (>= 1.2.0, < 2.0)
72
77
  rspec-support (~> 3.2.0)
73
78
  rspec-support (3.2.2)
79
+ rubocop (0.40.0)
80
+ parser (>= 2.3.1.0, < 3.0)
81
+ powerpack (~> 0.1)
82
+ rainbow (>= 1.99.1, < 3.0)
83
+ ruby-progressbar (~> 1.7)
84
+ unicode-display_width (~> 1.0, >= 1.0.1)
74
85
  ruby-prof (0.15.9)
86
+ ruby-progressbar (1.8.1)
75
87
  safe_yaml (1.0.4)
76
88
  simplecov (0.11.1)
77
89
  docile (~> 1.1.0)
@@ -88,6 +100,7 @@ GEM
88
100
  eventmachine (~> 1.0, >= 1.0.4)
89
101
  rack (~> 1.0)
90
102
  tilt (2.0.2)
103
+ unicode-display_width (1.0.5)
91
104
  uuid (2.3.8)
92
105
  macaddr (~> 1.0)
93
106
  webmock (1.22.5)
@@ -105,6 +118,7 @@ DEPENDENCIES
105
118
  rack-test
106
119
  rake
107
120
  rspec (~> 3.2.0)
121
+ rubocop
108
122
  ruby-prof
109
123
  simplecov
110
124
  sitehub!
data/README.md CHANGED
@@ -132,6 +132,29 @@ sitehub = SiteHub.build do
132
132
  end
133
133
  end
134
134
  ```
135
+ ### Nesting routes and splits
136
+ Routes and Splits can themselves contain further route or split definitions
137
+ ```ruby
138
+ sitehub = SiteHub.build do
139
+ proxy '/catalogue' do
140
+ # Experiment 1
141
+ split(precentage: 30) do
142
+ # 30% of overall traffic is split between 2 different prototypes
143
+ split percentage: 50, url: 'http://version1.com', label: :prototype_1
144
+ split percentage: 50, url: 'http://version2.com', label: :prototype_2
145
+ end
146
+
147
+ # Experiment 2
148
+ split(precentage: 30) do
149
+ # 30% of overall traffic is split between 2 different prototypes
150
+ split percentage: 50, url: 'http://version3.com', label: :prototype_3
151
+ split percentage: 50, url: 'http://version4.com', label: :prototype_4
152
+ end
153
+
154
+ default url: 'http://current_catalogue.com'
155
+ end
156
+ end
157
+ ```
135
158
  ### Labels
136
159
  Splits and Routes must be defined with a label. Within a proxy defintion, this label must be unique. This is the value that SiteHub will use to identify the version of a downstream url that a user should stick to once it has been selected.
137
160
 
data/Rakefile CHANGED
@@ -1,11 +1,5 @@
1
- require 'bundler'
2
- require "bundler/gem_tasks"
3
-
4
- require 'rspec/core/rake_task'
5
-
6
-
7
- RSpec::Core::RakeTask.new(:spec) do
8
- ENV['coverage'] = 'true'
1
+ Dir["#{__dir__}/tasks/*.rake"].each do |tasks|
2
+ import tasks
9
3
  end
10
4
 
11
- task :default => :spec
5
+ task default: [:clean, :spec, :coverage_check, 'rubocop:auto_correct', 'rubocop']
data/circle.yml ADDED
@@ -0,0 +1,6 @@
1
+ machine:
2
+ ruby:
3
+ version: '2.1'
4
+ test:
5
+ override:
6
+ - bundle exec rake
@@ -8,18 +8,15 @@ require 'sitehub/logging'
8
8
  require 'rack/fiber_pool'
9
9
  require 'logger'
10
10
 
11
-
12
11
  class SiteHub
13
-
14
- class InvalidProxyDefinitionException < Exception;
12
+ class InvalidProxyDefinitionException < Exception
15
13
  end
16
14
  class Builder
17
-
18
15
  attr_reader :sitehub, :forward_proxies, :reverse_proxies
19
16
 
20
17
  include Middleware
21
18
 
22
- def force_ssl except: []
19
+ def force_ssl(except: [])
23
20
  @force_ssl = true
24
21
  @ssl_exclusions = except
25
22
  end
@@ -27,20 +24,20 @@ class SiteHub
27
24
  def initialize(&block)
28
25
  @forward_proxies = ForwardProxies.new
29
26
  @reverse_proxies = {}
30
- instance_eval &block if block
27
+ instance_eval(&block) if block
31
28
  end
32
29
 
33
- def access_logger logger=nil
30
+ def access_logger(logger = nil)
34
31
  return @access_logger unless logger
35
32
  @access_logger = logger
36
33
  end
37
34
 
38
- def error_logger logger=nil
35
+ def error_logger(logger = nil)
39
36
  return @error_logger unless logger
40
37
  @error_logger = logger
41
38
  end
42
39
 
43
- def sitehub_cookie_name name=nil
40
+ def sitehub_cookie_name(name = nil)
44
41
  @sitehub_cookie_name ||= RECORDED_ROUTES_COOKIE
45
42
 
46
43
  return @sitehub_cookie_name unless name
@@ -49,34 +46,37 @@ class SiteHub
49
46
 
50
47
  def build
51
48
  forward_proxies.init
49
+ add_default_middleware
50
+ middlewares.reverse!
51
+ apply_middleware(forward_proxies)
52
+ end
53
+
54
+ def add_default_middleware
52
55
  use ReverseProxy, reverse_proxies
53
56
  use TransactionId
54
57
  use Logging::AccessLogger, access_logger || ::Logger.new(STDOUT)
55
58
  use Logging::ErrorLogger, error_logger || ::Logger.new(STDERR)
56
59
  use Rack::FiberPool
57
60
  use Rack::SslEnforcer, except: @ssl_exclusions if @force_ssl
58
- middlewares.reverse!
59
-
60
- apply_middleware(forward_proxies)
61
61
  end
62
62
 
63
- def proxy opts={}, &block
64
- args = {sitehub_cookie_name: sitehub_cookie_name}
63
+ def proxy(opts = {}, &block)
64
+ args = { sitehub_cookie_name: sitehub_cookie_name }
65
65
 
66
66
  if opts.is_a?(Hash)
67
67
  mapped_path = opts.keys.first
68
68
  url = opts.values.first
69
- args.merge!(url: url, mapped_path: mapped_path)
69
+ args[:url] = url
70
+ args[:mapped_path] = mapped_path
70
71
  else
71
- args.merge!(mapped_path: opts)
72
+ args[:mapped_path] = opts
72
73
  end
73
74
 
74
75
  forward_proxies << ForwardProxyBuilder.new(args, &block)
75
76
  end
76
77
 
77
- def reverse_proxy hash
78
+ def reverse_proxy(hash)
78
79
  reverse_proxies.merge!(hash)
79
80
  end
80
81
  end
81
-
82
- end
82
+ end
@@ -2,27 +2,25 @@ require_relative '../collection'
2
2
  class SiteHub
3
3
  class Collection < Hash
4
4
  class RouteCollection < Collection
5
-
6
- def add id, route, *opts
5
+ def add(id, route, *_opts)
7
6
  self[id] = route
8
7
  end
9
8
 
10
9
  def valid?
11
- !self.empty?
10
+ !empty?
12
11
  end
13
12
 
14
13
  def resolve(env: nil)
15
- return self.values.first unless self.values.find { |route| route.rule }
16
- result = self.values.find { |route| route.applies?(env) }
14
+ return values.first unless values.find(&:rule)
15
+ result = values.find { |route| route.applies?(env) }
17
16
  result && result.resolve(env: env)
18
17
  end
19
18
 
20
- def transform &block
21
- each do |id, value|
22
- self[id] = block.call(value)
19
+ def transform
20
+ each do |id, value|
21
+ self[id] = yield(value)
23
22
  end
24
23
  end
25
-
26
24
  end
27
25
  end
28
26
  end
@@ -5,14 +5,16 @@ class SiteHub
5
5
  attr_reader :upper, :lower
6
6
  attr_accessor :value
7
7
 
8
- def initialize lower, upper, value
9
- @upper, @lower, @value = upper, lower, value
8
+ def initialize(lower, upper, value)
9
+ @upper = upper
10
+ @lower = lower
11
+ @value = value
10
12
  end
11
13
 
12
- def == other
13
- other.is_a?(Split) && other.lower == self.lower && other.upper == self.upper
14
+ def ==(other)
15
+ other.is_a?(Split) && other.lower == lower && other.upper == upper
14
16
  end
15
17
  end
16
18
  end
17
19
  end
18
- end
20
+ end
@@ -3,38 +3,41 @@ require_relative 'split_route_collection/split'
3
3
  class SiteHub
4
4
  class Collection < Hash
5
5
  class SplitRouteCollection < Collection
6
- class InvalidSplitException < Exception;
6
+ class InvalidSplitException < Exception
7
7
  end
8
8
 
9
- def initialize(hash={})
9
+ FIXNUM_ERR_MSG = 'splits must be a Fixnum'.freeze
10
+ SPLIT_ERR_MSG = 'total split percentages can not be greater than 100%'.freeze
11
+
12
+ def initialize(hash = {})
10
13
  hash.each do |value, percentage|
11
14
  add(value.id, value, percentage)
12
15
  end
13
16
  end
14
17
 
15
- def add id, value, percentage
16
- raise InvalidSplitException, 'splits must be a Fixnum' unless percentage.is_a?(Fixnum)
18
+ def add(id, value, percentage)
19
+ raise InvalidSplitException, FIXNUM_ERR_MSG unless percentage.is_a?(Fixnum)
17
20
  lower = values.last ? values.last.upper : 0
18
21
  upper = lower + percentage
19
22
 
20
- raise InvalidSplitException, 'total split percentages can not be greater than 100%' if upper > 100
23
+ raise InvalidSplitException, SPLIT_ERR_MSG if upper > 100
21
24
  self[id] = Split.new(lower, upper, value)
22
25
  end
23
26
 
24
27
  def resolve(*args)
25
28
  random = rand(100)
26
29
  result = values.find { |split| random >= split.lower && random < split.upper }
27
- result ? result.value : nil
30
+ result ? result.value.resolve(*args) : nil
28
31
  end
29
32
 
30
- def transform &block
33
+ def transform
31
34
  values.each do |split|
32
- split.value = block.call(split.value)
35
+ split.value = yield(split.value)
33
36
  end
34
37
  end
35
38
 
36
- def [] key
37
- result = super
39
+ def [](key)
40
+ result = super(key)
38
41
  result && result.value
39
42
  end
40
43
 
@@ -1,35 +1,40 @@
1
1
  class SiteHub
2
2
  class Collection < Hash
3
- class DuplicateVersionException < Exception;
3
+ class DuplicateVersionException < Exception
4
+ end
5
+
6
+ module ClassMethods
7
+ def overrides
8
+ @overrides ||= []
9
+ end
10
+
11
+ UNIQUE_LABELS_MSG = 'supply unique labels'.freeze
12
+
13
+ def method_added(name)
14
+ if name == :add && !overrides.include?(name)
15
+ overrides << name
16
+ alias_method :add_backup, :add
17
+
18
+ send(:define_method, :add) do |id, value, *args|
19
+ raise DuplicateVersionException, UNIQUE_LABELS_MSG if self[id]
20
+ add_backup id, value, *args
21
+ end
22
+ end
23
+ end
4
24
  end
5
25
 
6
26
  def valid?
7
- raise "implement me"
27
+ raise 'implement me'
8
28
  end
9
29
 
10
30
  def resolve
11
- raise "implement me"
31
+ raise 'implement me'
12
32
  end
13
33
 
14
34
  class << self
15
- def inherited clazz
16
-
17
- def clazz.overrides
18
- @overrides ||=[]
19
- end
20
-
21
- def clazz.method_added(name)
22
- if name == :add && !overrides.include?(name)
23
- overrides << name
24
- alias_method :add_backup, :add
25
-
26
- self.send(:define_method, :add) do |id, value, *args|
27
- raise DuplicateVersionException, 'supply unique labels' if self[id]
28
- add_backup id, value, *args
29
- end
30
- end
31
- end
35
+ def inherited(clazz)
36
+ clazz.extend(ClassMethods)
32
37
  end
33
38
  end
34
39
  end
35
- end
40
+ end
@@ -1,9 +1,7 @@
1
1
  class SiteHub
2
2
  module Constants
3
3
  module HttpHeaderKeys
4
-
5
- ShouldNotTransfer = %w(proxy-connection).freeze
6
-
4
+ PROXY_CONNECTION = 'proxy-connection'.freeze
7
5
  LOCATION_HEADER = 'location'.freeze
8
6
  HOST_HEADER = 'host'.freeze
9
7
  X_FORWARDED_HOST_HEADER = 'x-forwarded-host'.freeze
@@ -18,8 +16,6 @@ class SiteHub
18
16
  CONTENT_ENCODING = 'content-encoding'.freeze
19
17
  SET_COOKIE = 'Set-Cookie'.freeze
20
18
  CONTENT_LENGTH = 'Content-Length'.freeze
21
-
22
- HopByHop = [CONNECTION_HEADER, KEEP_ALIVE, PROXY_AUTHENTICATE, PROXY_AUTHORIZATION, TE, TRAILERS, TRANSFER_ENCODING, CONTENT_ENCODING].freeze
23
19
  end
24
20
  end
25
- end
21
+ end
@@ -9,9 +9,9 @@ class SiteHub
9
9
  REMOTE_ADDR = 'REMOTE_ADDR'.freeze
10
10
  REMOTE_USER = 'REMOTE_USER'.freeze
11
11
  HTTP_VERSION = 'HTTP_VERSION'.freeze
12
- REMOTE_ADDRESS_ENV_KEY = "REMOTE_ADDR".freeze
12
+ REMOTE_ADDRESS_ENV_KEY = 'REMOTE_ADDR'.freeze
13
13
  HTTP_HEADER_FILTER_EXCEPTIONS = %w(CONTENT_TYPE CONTENT_LENGTH).freeze
14
14
  TRANSACTION_ID = 'HTTP_SITEHUB_TRANSACTION_ID'.freeze
15
15
  end
16
16
  end
17
- end
17
+ end
@@ -7,17 +7,17 @@ class SiteHub
7
7
  ASYNC_CALLBACK = 'async.callback'.freeze
8
8
  RECORDED_ROUTES_COOKIE = 'sitehub.recorded_route'.freeze
9
9
  ERRORS = 'sitehub.errors'.freeze
10
- TIME_STAMP_FORMAT = "%d/%b/%Y:%H:%M:%S %z".freeze
10
+ TIME_STAMP_FORMAT = '%d/%b/%Y:%H:%M:%S %z'.freeze
11
11
  EMPTY_STRING = ''.freeze
12
12
  UNDERSCORE = '_'.freeze
13
13
  SEMICOLON = ';'.freeze
14
14
  SPACE = ' '.freeze
15
15
  SEMICOLON_WITH_SPACE = "#{SEMICOLON}#{SPACE}".freeze
16
- COMMA_WITH_SPACE = ", "
16
+ COMMA_WITH_SPACE = ', '.freeze
17
17
 
18
18
  HYPHEN = '-'.freeze
19
19
  QUESTION_MARK = '?'.freeze
20
20
  EQUALS_SIGN = '='.freeze
21
21
  FULL_STOP = '.'.freeze
22
22
  NEW_LINE = "\n".freeze
23
- end
23
+ end
@@ -5,7 +5,7 @@ class SiteHub
5
5
  include StringSanitiser
6
6
  attr_accessor :name, :value
7
7
 
8
- def initialize name, value
8
+ def initialize(name, value)
9
9
  @name = sanitise(name).to_s.to_sym
10
10
  @value = sanitise(value || EMPTY_STRING)
11
11
  end
@@ -14,9 +14,9 @@ class SiteHub
14
14
  "#{name}=#{value}"
15
15
  end
16
16
 
17
- def == other
18
- other.is_a?(Attribute) && self.name == other.name && self.value == other.value
17
+ def ==(other)
18
+ other.is_a?(Attribute) && name == other.name && value == other.value
19
19
  end
20
20
  end
21
21
  end
22
- end
22
+ end
@@ -5,7 +5,7 @@ class SiteHub
5
5
  include StringSanitiser
6
6
  attr_accessor :name
7
7
 
8
- def initialize flag
8
+ def initialize(flag)
9
9
  @name = sanitise(flag).to_sym
10
10
  end
11
11
 
@@ -13,10 +13,9 @@ class SiteHub
13
13
  name.to_s
14
14
  end
15
15
 
16
- def == other
17
- other.is_a?(Flag) && self.name == other.name
16
+ def ==(other)
17
+ other.is_a?(Flag) && name == other.name
18
18
  end
19
19
  end
20
-
21
20
  end
22
- end
21
+ end
@@ -8,8 +8,7 @@ class SiteHub
8
8
 
9
9
  FIRST = 0
10
10
 
11
- def initialize cookie_string
12
- @attributes_and_flags =[]
11
+ def initialize(cookie_string)
13
12
  pairs = cookie_string.split(SEMICOLON).map do |entry|
14
13
  if entry.include?(EQUALS_SIGN)
15
14
  Cookie::Attribute.new(*entry.split(EQUALS_SIGN))
@@ -19,9 +18,8 @@ class SiteHub
19
18
  end
20
19
 
21
20
  name_attribute = pairs.delete_at(FIRST)
22
- attributes_and_flags.concat(pairs)
21
+ @attributes_and_flags = pairs
23
22
  @name_attribute = Cookie::Attribute.new(name_attribute.name.to_s, name_attribute.value)
24
-
25
23
  end
26
24
 
27
25
  def name
@@ -32,23 +30,21 @@ class SiteHub
32
30
  name_attribute.value
33
31
  end
34
32
 
35
- def find name
36
- attributes_and_flags.find{|entry| entry.name == name}
33
+ def find(name)
34
+ attributes_and_flags.find { |entry| entry.name == name }
37
35
  end
38
36
 
39
- def delete name
40
- entry = find(name)
41
- @attributes_and_flags = attributes_and_flags.delete_if{|e| e.name == name}
42
- entry
37
+ def ==(other)
38
+ other.is_a?(self.class) &&
39
+ sorted_attributes_and_flags(attributes_and_flags) == sorted_attributes_and_flags(other.attributes_and_flags)
43
40
  end
44
41
 
45
- def == other
46
- other.is_a?(self.class) && self.attributes_and_flags.sort{|entry1, entry2| entry1.name <=> entry2.name} == other.attributes_and_flags.sort{|entry1, entry2| entry1.name <=> entry2.name}
42
+ def sorted_attributes_and_flags(attributes_and_flags)
43
+ attributes_and_flags.sort { |entry1, entry2| entry1.name <=> entry2.name }
47
44
  end
48
45
 
49
46
  def to_s
50
47
  [name_attribute].concat(attributes_and_flags).join(SEMICOLON_WITH_SPACE)
51
48
  end
52
-
53
49
  end
54
- end
50
+ end
@@ -1,35 +1,33 @@
1
1
  require 'sitehub/cookie'
2
2
  require 'sitehub/constants'
3
3
  class SiteHub
4
-
5
4
  module CookieRewriting
6
-
7
5
  ENDING_WITH_NEWLINE = /#{NEW_LINE}$/
8
6
 
9
7
  def rewrite_cookies(headers, substitute_domain:)
10
8
  cookies_hash = cookies_string_as_hash(headers[Constants::HttpHeaderKeys::SET_COOKIE])
11
9
 
12
10
  cookies_hash.values.each do |cookie|
13
- if domain_attribute = cookie.find(:domain)
14
- value = domain_attribute.value
15
- domain_attribute.value = substitute_domain.dup
16
- domain_attribute.value.prepend(FULL_STOP) if value.start_with?(FULL_STOP)
17
- end
11
+ domain_attribute = cookie.find(:domain) || next
12
+ value = domain_attribute.value
13
+ domain_attribute.value = substitute_domain.dup
14
+ domain_attribute.value.prepend(FULL_STOP) if value.start_with?(FULL_STOP)
18
15
  end
19
16
  headers[HttpHeaders::SET_COOKIE] = cookies_hash_to_string(cookies_hash)
20
17
  end
21
18
 
22
- def cookies_hash_to_string cookies_hash
19
+ def cookies_hash_to_string(cookies_hash)
23
20
  cookies_hash.values.inject(EMPTY_STRING.dup) do |cookie_string, cookie|
24
- cookie_string << "#{cookie.to_s}#{NEW_LINE}"
21
+ cookie_string << "#{cookie}#{NEW_LINE}"
25
22
  end.sub(ENDING_WITH_NEWLINE, EMPTY_STRING)
26
23
  end
27
24
 
28
- def cookies_string_as_hash cookie_string
29
- cookie_string.lines.inject({}) do |cookies, cookie_line|
25
+ def cookies_string_as_hash(cookie_string)
26
+ cookie_string.lines.each_with_object({}) do |cookie_line, cookies|
30
27
  cookie = SiteHub::Cookie.new(cookie_line)
31
- cookies[cookie.name] = cookie; cookies
28
+ cookies[cookie.name] = cookie
29
+ cookies
32
30
  end
33
31
  end
34
32
  end
35
- end
33
+ end