level_travel 0.1.0 → 0.2.0

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: 6463a5dd4e26e45033296ba9d8002e6a7171f2c8f48c47243a7496456b31a29b
4
- data.tar.gz: ce029033e6ab06c9375e6794c465a79b1de13aa933af84594ee99891f03f178e
3
+ metadata.gz: 130213408e85057cbe6f3d37751fa02729c24b9b2b6ec29d3370d32bc910d2cb
4
+ data.tar.gz: 8c70e07d8b93a2b72095c5f3c6563d714f5d80f2d620da2ec7b8b3a32e74470a
5
5
  SHA512:
6
- metadata.gz: 9c992179ae33396f1d544c04df20a2a0893a0b7af3516a2ecb9d4db5f88add232229f52e7c70a633d24b781e1f60ef9c3c86abb2a7ad4b53b81ea9465f4e30dc
7
- data.tar.gz: 57d108e758d90a6cd548422e79a3d55b2f8e315cba6b1303eab91643df2721a3a94cf3b639b646054927891c959927d9b399c923986439538147784d781fe07a
6
+ metadata.gz: 8609b7c56d0ea5dffe9a26349a27863678ef1aa7a2d62c023462b41ac730b2b225f6e6f9a0ea186201d1617bf10be576e4bc88fa6a4fa9e3eba1cde853e53419
7
+ data.tar.gz: 2a077800dc672071ad254f57410ea4722cf4e801f24dea32b9cbe13bac36288842e5329f7b94263dc40e469b9caca29b7ff2df27e9eba1ffb6713a20cfbb1c96
@@ -2,7 +2,6 @@
2
2
 
3
3
  require 'faraday'
4
4
  require 'oj'
5
- require 'dry-struct'
6
5
 
7
6
  module LevelTravel
8
7
  class Request
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LevelTravel
4
+ module Search
5
+ class Params < Dry::Struct
6
+ attribute :from_city, ::LevelTravel::Types::Strict::String
7
+ attribute :to_country, ::LevelTravel::Types::Strict::String
8
+ attribute? :to_city, ::LevelTravel::Types::Strict::String.optional
9
+ attribute? :hotel_ids, ::LevelTravel::Types.Array(::LevelTravel::Types::Strict::Integer).optional
10
+ attribute :nights, ::LevelTravel::Types::Strict::Integer
11
+ attribute :adults, ::LevelTravel::Types::Strict::Integer
12
+ attribute :start_date, ::LevelTravel::Types::Strict::Date
13
+ attribute? :kids, ::LevelTravel::Types::Strict::Integer.optional
14
+ attribute? :kids_ages, ::LevelTravel::Types.Array(::LevelTravel::Types::Strict::Integer).optional
15
+ attribute? :stars_from, ::LevelTravel::Types::Strict::Integer.optional
16
+ attribute? :stars_to, ::LevelTravel::Types::Strict::Integer.optional
17
+
18
+ def to_h
19
+ result = super.merge(start_date: start_date.strftime('%d.%m.%Y'))
20
+ result = result.merge(hotel_ids: hotel_ids.join(',')) if result[:hotel_ids]
21
+ result = result.merge(kids_ages: kids_ages.join(',')) if result[:kids_ages]
22
+
23
+ result
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LevelTravel
4
+ module Search
5
+ class ParamsContract < Dry::Validation::Contract
6
+ config.messages.default_locale = :en
7
+ config.messages.top_namespace = 'level_travel'
8
+ config.messages.load_paths << File.expand_path('../../../config/errors.yml', __dir__).freeze
9
+
10
+ RANGES = {
11
+ stars: (1..5).freeze,
12
+ kids_age: (0..12).freeze
13
+ }.freeze
14
+
15
+ # TODO: from_city, to_country, and to_city are also possible to be an ID
16
+ params do
17
+ required(:from_city).filled(:string)
18
+ required(:to_country).value(:string)
19
+ required(:nights).value(:integer)
20
+ required(:adults).value(:integer)
21
+ required(:start_date).value(:date)
22
+
23
+ optional(:to_city).value(:string)
24
+ optional(:hotel_ids).value(Types::ArrayOfIntegers)
25
+ optional(:kids).value(:integer)
26
+ optional(:kids_ages).value(Types::ArrayOfIntegers)
27
+ optional(:stars_from).value(:integer)
28
+ optional(:stars_to).value(:integer)
29
+ end
30
+
31
+ rule(:start_date) do
32
+ key.failure(:invalid) if value <= Date.today
33
+ end
34
+
35
+ rule(:stars_from) do
36
+ key.failure(:not_in_range, range: RANGES.fetch(:stars)) if key? && !RANGES.fetch(:stars).include?(value)
37
+ end
38
+
39
+ rule(:stars_to) do
40
+ key.failure(:not_in_range, range: RANGES.fetch(:stars)) if key? && !RANGES.fetch(:stars).include?(value)
41
+ end
42
+
43
+ rule(:kids_ages) do
44
+ key.failure(:required) if key? && values[:kids] && value.empty?
45
+
46
+ if key? && value.any? { |age| !RANGES.fetch(:kids_age).include?(age) }
47
+ key.failure(:not_in_range, range: RANGES.fetch(:kids_age))
48
+ end
49
+
50
+ if key? && !value.empty? && value.size != values[:kids].to_i
51
+ key.failure(:not_equal_to_kids, actual: value.size, needed: values[:kids].to_i)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LevelTravel
4
+ module Search
5
+ class Request
6
+ # @param search_params [LevelTravel::Search::Params] Request params for search
7
+ def self.enqueue(search_params)
8
+ ::LevelTravel::Request.get('/search/enqueue', search_params.to_h)
9
+ end
10
+
11
+ # @param request_id [String] Request ID from the `enqueue` request
12
+ def self.status(request_id)
13
+ ::LevelTravel::Request.get('/search/status', request_id: request_id)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -4,6 +4,9 @@ require 'dry-types'
4
4
 
5
5
  module LevelTravel
6
6
  module Types
7
- include Dry.Types(:params, :strict, default: :strict)
7
+ include Dry.Types()
8
+
9
+ StrippedString = Types::Strict::String.constructor(&:strip)
10
+ ArrayOfIntegers = Types.Array(Types::Coercible::Integer)
8
11
  end
9
12
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LevelTravel
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/level_travel.rb CHANGED
@@ -3,8 +3,16 @@
3
3
  module LevelTravel
4
4
  end
5
5
 
6
+ require 'date'
7
+ require 'dry-struct'
8
+ require 'dry-validation'
9
+
6
10
  require 'level_travel/version'
7
11
  require 'level_travel/types'
8
12
  require 'level_travel/settings'
9
13
  require 'level_travel/request'
10
14
  require 'level_travel/references'
15
+
16
+ require 'level_travel/search/params_contract'
17
+ require 'level_travel/search/params'
18
+ require 'level_travel/search/request'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: level_travel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Rodionov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-07 00:00:00.000000000 Z
11
+ date: 2020-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-struct
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: dry-validation
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.4'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.4'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: faraday
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -208,6 +222,9 @@ files:
208
222
  - lib/level_travel.rb
209
223
  - lib/level_travel/references.rb
210
224
  - lib/level_travel/request.rb
225
+ - lib/level_travel/search/params.rb
226
+ - lib/level_travel/search/params_contract.rb
227
+ - lib/level_travel/search/request.rb
211
228
  - lib/level_travel/settings.rb
212
229
  - lib/level_travel/types.rb
213
230
  - lib/level_travel/version.rb