gillbus-v2 0.1.2 → 0.1.3

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: e492282b609d3c3b20ad09f90a14def0027ace86577f4fc5f38152f7aef7646e
4
- data.tar.gz: 4a0ab148b572b5dbb982a73d6462371070a1fec9ca8e9d83d4446bd599e338c3
3
+ metadata.gz: 43f5f66f49c4de6dce6487cd0cf702d49073ca1a870d1097599d5caaeef798c9
4
+ data.tar.gz: b413b3fb9ad9e346742bbf2566ffd6b81a2cc9f4e189b60a03d4f106eb954739
5
5
  SHA512:
6
- metadata.gz: c59e9c4d3f612f2e5f01d865fabc123a20ca0d6ec7d0f77eaee5c57a6ee08efd42fbb5f7a123d6a7657600d3c54b920295d4f29a13130ebb22b2029b11685944
7
- data.tar.gz: f646e149e2dda7b7f82dc0e9cf7f886aeefdc69c2e2133302ead3fa50bd01fc381dff176a6d0082353edfa22be53b509983693dd4457fc177112490ad014c9b1
6
+ metadata.gz: 1aad4fa134e4e69d6d3e52d9d725768c80e5abeab5055347043ad2eeb488b9506fca7d8b6da7e3b891b1d1398a9e1f098e946804ef0699b2901d8e903e8181b8
7
+ data.tar.gz: 18c6a257567c09351c22698c97c7376e3b4a9a5d142eeb9caa6c1547de9438f6dc502582048803f5e4cf1234c8d1cb747318ac113d74f3b19549f9193cace3da
data/README.md CHANGED
@@ -150,6 +150,44 @@ Each location contains fields:
150
150
  - `modified_at` - last modification time
151
151
  - `deleted` - if true then location has been removed
152
152
 
153
+ ### Search trips
154
+
155
+ Search of trips works in the same way as `get_locations`:
156
+
157
+ - `search_trips` for first page
158
+ - `search_trips_page` for other
159
+
160
+ ```ruby
161
+ response = client.search_trips(
162
+ from_id: 111,
163
+ to_id: 222,
164
+ date: Date.today + 1,
165
+ back_date: Date.today + 5,
166
+ passengers_count: 1,
167
+ limit_for_page: 20,
168
+ )
169
+ response.trips
170
+
171
+ while response.pagination.next_page
172
+ response = client.search_trips_page(
173
+ pagination_uuid: response.pagination.uuid,
174
+ page_number: response.pagination.next_page,
175
+ )
176
+ response.trips
177
+ end
178
+ ```
179
+
180
+ Every response contains trips and dictionaries for them:
181
+
182
+ ```ruby
183
+ response.trips
184
+
185
+ # dictionaries
186
+ response.carriers
187
+ response.vehicles
188
+ response.points
189
+ ```
190
+
153
191
  ## Development
154
192
 
155
193
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/gillbus-v2.gemspec CHANGED
@@ -21,7 +21,8 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
23
 
24
- spec.add_dependency 'faraday'
24
+ spec.add_dependency "faraday"
25
+ spec.add_dependency "money"
25
26
 
26
27
  spec.add_development_dependency "bundler"
27
28
  spec.add_development_dependency "rake", "~> 10.0"
@@ -62,6 +62,34 @@ module Gillbus::V2
62
62
  )
63
63
  end
64
64
 
65
+ # search_mode:
66
+ # - full - искать все рейсы (по умолчанию)
67
+ # - direct - искать только прямые рейсы
68
+ def search_trips(from_id:, to_id:, date:, back_date: nil, passengers_count: 1, search_mode: "full", limit_for_page: 20)
69
+ params = {
70
+ from_id: from_id,
71
+ to_id: to_id,
72
+ date: date.iso8601,
73
+ back_date: back_date&.iso8601,
74
+ pass_count: passengers_count,
75
+ search_mode: search_mode,
76
+ limit: limit_for_page,
77
+ }
78
+ call_api(:get, "/search/v2/trips", params,
79
+ response_class: Responses::SearchTrips,
80
+ )
81
+ end
82
+
83
+ def search_trips_page(pagination_uuid:, page_number:)
84
+ params = {
85
+ page_uuid: pagination_uuid,
86
+ number_page: page_number,
87
+ }
88
+ call_api(:get, "/search/v2/trips/page", params,
89
+ response_class: Responses::SearchTrips,
90
+ )
91
+ end
92
+
65
93
  private
66
94
 
67
95
  def call_api(http_method, url, params, auth_required: true, response_class: Responses::Base)
@@ -9,7 +9,17 @@ module Gillbus::V2
9
9
  next
10
10
  end
11
11
 
12
- raw_value = raw_data[field[:from]]
12
+ raw_value =
13
+ if field[:from].is_a?(Hash)
14
+ field[:from].map { |key, value| [key, raw_data[value]] }.to_h
15
+ else
16
+ raw_data[field[:from]]
17
+ end
18
+
19
+ if field[:enrich_with]
20
+ raw_value = enrich_data(raw_value, raw_data, field[:enrich_with])
21
+ end
22
+
13
23
  result[field[:name]] =
14
24
  if raw_value.nil?
15
25
  field[:default]
@@ -22,6 +32,23 @@ module Gillbus::V2
22
32
 
23
33
  private
24
34
 
35
+ def enrich_data(data, from_data, attrs_list)
36
+ enrich_with_data =
37
+ attrs_list.each_with_object({}) do |attr_name, result|
38
+ result[attr_name] = from_data[attr_name]
39
+ end
40
+
41
+ if data.is_a?(Hash)
42
+ data.merge(enrich_with_data)
43
+ elsif data.is_a?(Array) && data.all? { |item| item.is_a?(Hash) }
44
+ data.map do |item|
45
+ item.merge(enrich_with_data)
46
+ end
47
+ else
48
+ data
49
+ end
50
+ end
51
+
25
52
  def coerce_value(value, type)
26
53
  case type
27
54
  when :boolean
@@ -32,15 +59,42 @@ module Gillbus::V2
32
59
  value.to_i
33
60
  when :float
34
61
  value.to_f
35
- when :date_time
62
+ when :date
63
+ Date.parse(value)
64
+ when :date_time_hh_mm
65
+ DateTime.strptime(value, '%Y-%m-%d %H:%M')
66
+ when :date_time_rfc3339
36
67
  DateTime.rfc3339(value)
37
- when :date_time_from_timestamp
68
+ when :date_time_timestamp
38
69
  DateTime.strptime(value.to_s, "%s")
70
+ when :time_interval_from_minutes
71
+ value.to_i * 60
39
72
  when :translations_hash
40
73
  value.to_h
41
- else
42
- raise "Type #{type} not supported"
74
+ when Array
75
+ inner_type = type.first
76
+ if value.is_a?(Array)
77
+ value.map { |item| coerce_value(item, inner_type) }
78
+ else
79
+ raise "Invalid value for #{type}: #{value.class}"
80
+ end
81
+ when Structs::Base::Enum
82
+ type.include?(value) ? value : nil
83
+ when Class
84
+ if type < Structs::Base
85
+ type.from_raw_data(value)
86
+ elsif type <= Money
87
+ if value[:amount] && value[:currency]
88
+ Money.from_amount(value[:amount], value[:currency])
89
+ end
90
+ else
91
+ raise "Type #{type} not supported"
92
+ end
43
93
  end
94
+ rescue StandardError
95
+ # Временно игнорируем все ошибки в данных.
96
+ # TODO: подумать над валидацией объектов.
97
+ nil
44
98
  end
45
99
 
46
100
  def fetch_from_translations(translations, field_name)
@@ -6,9 +6,11 @@ module Gillbus::V2
6
6
  end
7
7
 
8
8
  def access_token
9
- if json_body["access_token"]
10
- Structs::AccessToken.from_raw_data(json_body)
11
- end
9
+ return @access_token if defined?(@access_token)
10
+ @access_token =
11
+ if json_body["access_token"]
12
+ Structs::AccessToken.from_raw_data(json_body)
13
+ end
12
14
  end
13
15
  end
14
16
  end
@@ -0,0 +1,47 @@
1
+ module Gillbus::V2
2
+ module Responses
3
+ class SearchTrips < Responses::Base
4
+ def trips
5
+ @trips ||= (json_body["trips"] || []).map do |item|
6
+ trip_data = item.dup
7
+ trip_data["direct_trip"].map! { |id| segment_data(id) }
8
+ trip_data["back_trip"].map! { |id| segment_data(id) }
9
+ Structs::Trip.from_raw_data(trip_data)
10
+ end
11
+ end
12
+
13
+ def carriers
14
+ @carriers ||= (json_body["carriers"] || []).map do |item|
15
+ Structs::Carrier.from_raw_data(item)
16
+ end
17
+ end
18
+
19
+ def vehicles
20
+ @vehicles ||= (json_body["vehicles"] || []).map do |item|
21
+ Structs::Vehicle.from_raw_data(item)
22
+ end
23
+ end
24
+
25
+ def points
26
+ @points ||= (json_body["points"] || []).map do |item|
27
+ Structs::Point.from_raw_data(item)
28
+ end
29
+ end
30
+
31
+ def pagination
32
+ @pagination ||=
33
+ if json_body["pages_info"]
34
+ Structs::Pagination.from_raw_data(json_body["pages_info"])
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def segment_data(segment_id)
41
+ @segments_data_by_ids ||=
42
+ (json_body["segments"] || []).group_by { |item| item["id"] }
43
+ @segments_data_by_ids[segment_id]&.first
44
+ end
45
+ end
46
+ end
47
+ end
@@ -3,7 +3,7 @@ module Gillbus::V2
3
3
  field :access_token, :string
4
4
  field :token_type, :string, default: "Bearer"
5
5
  field :expires_in, :integer
6
- field :expires_on, :date_time_from_timestamp
6
+ field :expires_on, :date_time_timestamp
7
7
 
8
8
  def self.from_token_string(token)
9
9
  raw_data = { "access_token" => token }
@@ -3,6 +3,13 @@ module Gillbus::V2
3
3
  class Base
4
4
  attr_reader :raw_data
5
5
 
6
+ # field :my_field, Enum.("val1", "val2")
7
+ class Enum < Set
8
+ def self.call(*elements)
9
+ new(elements)
10
+ end
11
+ end
12
+
6
13
  def initialize(raw_data:, **fields)
7
14
  @raw_data = raw_data
8
15
  fields.each do |field_name, field_value|
@@ -10,14 +17,15 @@ module Gillbus::V2
10
17
  end
11
18
  end
12
19
 
13
- def self.field(name, type, from: name, default: nil)
20
+ def self.field(name, type, from: name.to_s, default: nil, enrich_with: nil)
14
21
  attr_reader name
15
22
  @fields_settings ||= []
16
23
  @fields_settings << {
17
24
  name: name,
18
25
  type: type,
19
- from: from.to_s,
26
+ from: from,
20
27
  default: default,
28
+ enrich_with: enrich_with,
21
29
  }
22
30
  end
23
31
 
@@ -0,0 +1,8 @@
1
+ module Gillbus::V2
2
+ class Structs::Carrier < Structs::Base
3
+ field :id, :string
4
+ field :name, :string
5
+ field :trade_mark, :string
6
+ field :logo, :string
7
+ end
8
+ end
@@ -6,7 +6,7 @@ module Gillbus::V2
6
6
  field :type_id, :integer
7
7
  field :sub_type_id, :integer, from: "subtype_id"
8
8
 
9
- field :modified_at, :date_time, from: "date_modified"
9
+ field :modified_at, :date_time_rfc3339, from: "date_modified"
10
10
  field :deleted, :boolean
11
11
 
12
12
  field :latitude, :float
@@ -0,0 +1,9 @@
1
+ module Gillbus::V2
2
+ class Structs::Point < Structs::Base
3
+ field :id, :string
4
+ field :parent_id, :string
5
+
6
+ field :name, :string
7
+ field :address, :string
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ module Gillbus::V2
2
+ class Structs::RefundConditions < Structs::Base
3
+ field :time_from, :time_interval_from_minutes
4
+ field :time_till, :time_interval_from_minutes
5
+
6
+ field :return_percent, :float
7
+ field :return_amount, Money,
8
+ from: { amount: "return_amount", currency: "currency" }
9
+
10
+ field :description, :string, from: "condition_description"
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ module Gillbus::V2
2
+ class Structs::RoutePoint < Structs::Base
3
+ field :point_id, :string
4
+ field :vehicle_id, :string
5
+
6
+ field :departure_platform, :string, from: "platform"
7
+
8
+ field :departure_date_time, :date_time_hh_mm, from: "departureDateTime"
9
+ field :arrival_date_time, :date_time_hh_mm, from: "arrivalDateTime"
10
+
11
+ field :check_point, :boolean
12
+ field :transfer_point, :boolean, from: "is_hub"
13
+
14
+ field :distance_from_start, :integer, from: "distance"
15
+ end
16
+ end
@@ -0,0 +1,26 @@
1
+ module Gillbus::V2
2
+ # TODO: use in seatmap response or remove
3
+ class Structs::Seat < Structs::Base
4
+ field :id, :string
5
+ field :number, :string
6
+
7
+ field :floor, :integer
8
+ field :col, :integer
9
+ field :row, :integer
10
+
11
+ field :seat_class, :string, from: "class"
12
+
13
+ field :status, Enum.(
14
+ "EMPTY",
15
+ "ORDERED",
16
+ "BLOCKED",
17
+ "FREE",
18
+ "BOOKED",
19
+ "SALED",
20
+ "SPESIAL",
21
+ "REGISTERED",
22
+ "TECHNICAL",
23
+ "PRIORITY",
24
+ )
25
+ end
26
+ end
@@ -0,0 +1,48 @@
1
+ module Gillbus::V2
2
+ class Structs::Segment < Structs::Base
3
+ field :id, :string
4
+ field :carrier_id, :string
5
+ field :vehicle_id, :string
6
+ field :resource_id, :string
7
+
8
+ field :price, Structs::SegmentPrice
9
+
10
+ field :travel_time, :string
11
+
12
+ field :trip_number, :string, from: "number"
13
+ field :trip_name, :string, from: "name"
14
+ field :trip_type, Enum.("international", "internal")
15
+
16
+ field :departure_point_id, :string
17
+ field :arrival_point_id, :string
18
+
19
+ field :departure_date_time, :date_time_hh_mm, from: "departureDateTime"
20
+ field :arrival_date_time, :date_time_hh_mm, from: "arrivalDateTime"
21
+
22
+ field :departure_platform, :string, from: "platform"
23
+
24
+ field :reservation_enabled, :boolean, from: "reservation_enable"
25
+ field :reservation_lifetime, :time_interval_from_minutes
26
+
27
+ # время до отправления рейса, после которого запрещено бронирование
28
+ field :stop_reservation_time, :time_interval_from_minutes
29
+
30
+ # наличие пересадки (смены автобуса)
31
+ field :has_transfer, :boolean, from: "has_hub"
32
+
33
+ # стоимость может измениться при продаже в случае автоматического применения льготных тарифов
34
+ field :can_discount, :boolean
35
+
36
+ # возможность добровольного возврата
37
+ field :voluntary_return_enabled, :boolean, from: "return_enable"
38
+
39
+ # Если sale_enabled == false, то необходим редирект на redirect_url
40
+ # (на этот рейс возможна продажа только на сайте владельца)
41
+ field :sale_enabled, :boolean, from: "sale_enable"
42
+ field :redirect_url, :string
43
+
44
+ field :options, Structs::SegmentOptions
45
+
46
+ field :route, [Structs::RoutePoint]
47
+ end
48
+ end
@@ -0,0 +1,6 @@
1
+ module Gillbus::V2
2
+ class Structs::SegmentOptions < Structs::Base
3
+ field :luggage_conditions, [:string]
4
+ field :special_conditions, [:string]
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ module Gillbus::V2
2
+ class Structs::SegmentPrice < Structs::Base
3
+ field :total, Money, from: { amount: "total", currency: "currency" }
4
+ field :netto, Money, from: { amount: "netto", currency: "currency" }
5
+
6
+ field :tariffs, [Structs::SegmentTariff], enrich_with: %w[currency]
7
+ end
8
+ end
@@ -0,0 +1,20 @@
1
+ module Gillbus::V2
2
+ class Structs::SegmentTariff < Structs::Base
3
+ field :id, :string
4
+ field :code, :string
5
+
6
+ field :cost, Money, from: { amount: "cost", currency: "currency" }
7
+
8
+ field :seat_class, :string, from: "class"
9
+
10
+ field :start_date, :date
11
+ field :end_date, :date
12
+
13
+ field :name, :string
14
+ field :description, :string, from: "description_tariff"
15
+ field :notes, :string, from: "note"
16
+
17
+ field :refund_conditions, [Structs::RefundConditions],
18
+ enrich_with: %w[currency]
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ module Gillbus::V2
2
+ class Structs::Trip < Structs::Base
3
+ field :id, :integer, from: "trip_id"
4
+ field :direct_segments, [Structs::Segment], from: "direct_trip"
5
+ field :back_segments, [Structs::Segment], from: "back_trip"
6
+ end
7
+ end
@@ -0,0 +1,16 @@
1
+ module Gillbus::V2
2
+ class Structs::Vehicle < Structs::Base
3
+ field :id, :string
4
+
5
+ field :model, :string
6
+ field :number, :string
7
+
8
+ field :capacity, :integer
9
+
10
+ field :vehicle_type, :string
11
+ field :vehicle_class, :string
12
+
13
+ field :picture_url, :string
14
+ field :tumbnail_url, :string
15
+ end
16
+ end
@@ -1,5 +1,5 @@
1
1
  module Gillbus
2
2
  module V2
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
data/lib/gillbus/v2.rb CHANGED
@@ -2,6 +2,7 @@ require "date"
2
2
  require "json"
3
3
 
4
4
  require "faraday"
5
+ require "money"
5
6
 
6
7
  require "gillbus/v2/version"
7
8
  require "gillbus/v2/errors"
@@ -15,7 +16,18 @@ require "gillbus/v2/structs/location"
15
16
  require "gillbus/v2/structs/location_type"
16
17
  require "gillbus/v2/structs/location_sub_type"
17
18
  require "gillbus/v2/structs/location_additional_field"
19
+ require "gillbus/v2/structs/refund_conditions"
20
+ require "gillbus/v2/structs/segment_tariff"
21
+ require "gillbus/v2/structs/segment_price"
22
+ require "gillbus/v2/structs/segment_options"
23
+ require "gillbus/v2/structs/route_point"
24
+ require "gillbus/v2/structs/segment"
25
+ require "gillbus/v2/structs/trip"
26
+ require "gillbus/v2/structs/carrier"
27
+ require "gillbus/v2/structs/vehicle"
28
+ require "gillbus/v2/structs/point"
18
29
 
19
30
  require "gillbus/v2/responses/base"
20
31
  require "gillbus/v2/responses/authenticate"
21
32
  require "gillbus/v2/responses/locations"
33
+ require "gillbus/v2/responses/search_trips"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gillbus-v2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Khrebtov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-02 00:00:00.000000000 Z
11
+ date: 2019-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: money
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -132,13 +146,25 @@ files:
132
146
  - lib/gillbus/v2/responses/authenticate.rb
133
147
  - lib/gillbus/v2/responses/base.rb
134
148
  - lib/gillbus/v2/responses/locations.rb
149
+ - lib/gillbus/v2/responses/search_trips.rb
135
150
  - lib/gillbus/v2/structs/access_token.rb
136
151
  - lib/gillbus/v2/structs/base.rb
152
+ - lib/gillbus/v2/structs/carrier.rb
137
153
  - lib/gillbus/v2/structs/location.rb
138
154
  - lib/gillbus/v2/structs/location_additional_field.rb
139
155
  - lib/gillbus/v2/structs/location_sub_type.rb
140
156
  - lib/gillbus/v2/structs/location_type.rb
141
157
  - lib/gillbus/v2/structs/pagination.rb
158
+ - lib/gillbus/v2/structs/point.rb
159
+ - lib/gillbus/v2/structs/refund_conditions.rb
160
+ - lib/gillbus/v2/structs/route_point.rb
161
+ - lib/gillbus/v2/structs/seat.rb
162
+ - lib/gillbus/v2/structs/segment.rb
163
+ - lib/gillbus/v2/structs/segment_options.rb
164
+ - lib/gillbus/v2/structs/segment_price.rb
165
+ - lib/gillbus/v2/structs/segment_tariff.rb
166
+ - lib/gillbus/v2/structs/trip.rb
167
+ - lib/gillbus/v2/structs/vehicle.rb
142
168
  - lib/gillbus/v2/version.rb
143
169
  homepage: https://github.com/busfor/gillbus-v2-ruby
144
170
  licenses: