gdsapi-v2-ruby 0.1.3 → 0.1.4

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
  SHA1:
3
- metadata.gz: c781b442dc46eb87b3679ea913b7805c4c32b377
4
- data.tar.gz: 9e9c8735c65d9e44558ce37d1766e3dd43352c9f
3
+ metadata.gz: 682cc9ed559e0b6ac5d9af4074e5341b4b5d7c3a
4
+ data.tar.gz: 62b93de7d2a463f4a77d818068570ae0a066301c
5
5
  SHA512:
6
- metadata.gz: 5e28af36aea0cca34133980851bdcd3b5d509b74d3a58c259affdbc0af28d56d0db6c3344a1207ff8a1296a43832dae5a71ded156bbc22b654fd7bed28a3f811
7
- data.tar.gz: 298a8b3a496e1e2577199d3b405cc9338bbb164a606c61ad5fd488d789e4c002199bdff9bdf05a9f33ecc37ad35fd280ce0a3a8e5dc6876b0c1895e12acdcae2
6
+ metadata.gz: 430c0da15dab2bb293d415e9b30f740048ebe3e1a118c33d3c34b15805104ea54371ea8c00df23c4212421a2380da81257aa85ff6d17c82ed2c6a8c14f2c372e
7
+ data.tar.gz: 681427cad7b61007cf738f8017dc01498714da4f4afb89d76cd6ab8635dbad071e9cc87efd7b4700f5b04b96c34ba09876979c7ed09c2643ba76f300d9d058dd
@@ -5,7 +5,7 @@ require 'gdsapi/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "gdsapi-v2-ruby"
8
- spec.version = Gdsapi::V2::VERSION
8
+ spec.version = Gdsapi::VERSION
9
9
  spec.authors = ["Mike Yurchenkov"]
10
10
  spec.email = ["mikesehse@gmail.com"]
11
11
 
data/lib/gdsapi/client.rb CHANGED
@@ -1,85 +1,83 @@
1
1
  # Facade class for performing GDS requests
2
2
  module Gdsapi
3
- module V2
4
- class Client
5
- attr_reader :requester
3
+ class Client
4
+ attr_reader :requester
6
5
 
7
- # @param [Gdsapi::V2::Requester] requester - requester instance
8
- # It is `strongly recommended` to use Faraday(see ##with_faraday_requester)
9
- def initialize(requester)
10
- @requester = requester
11
- end
12
-
13
- class << self
14
- # Constructing routine that uses Faraday as driver
15
- # @param base_url: url param for Faraday(requred)
16
- # @param login: & password: - HTTP basic auth factors
17
- # @param language: - language slug for which request will be performed (available are: en, ru, uk, pl, th)
18
- def with_faraday_requester(base_url: nil, login: nil, password: nil, language: nil)
19
- driver = Faraday.new(url: base_url)
20
- new(Requester.new(driver, login: login, password: password, language: language))
21
- end
6
+ # @param [Gdsapi::Requester] requester - requester instance
7
+ # It is `strongly recommended` to use Faraday(see ##with_faraday_requester)
8
+ def initialize(requester)
9
+ @requester = requester
10
+ end
22
11
 
23
- # Constructing routine that uses given driver
24
- # @param driver - driver instance
25
- # @param login: & password: - HTTP basic auth factors
26
- # @param language: - language slug for which request will be performed (available are: en, ru, uk, pl, th)
27
- def with_requester(driver, login: nil, password: nil, language: nil)
28
- new(Requester.new(driver, login: login, password: password, language: language))
29
- end
12
+ class << self
13
+ # Constructing routine that uses Faraday as driver
14
+ # @param base_url: url param for Faraday(requred)
15
+ # @param login: & password: - HTTP basic auth factors
16
+ # @param language: - language slug for which request will be performed (available are: en, ru, uk, pl, th)
17
+ def with_faraday_requester(base_url: nil, login: nil, password: nil, language: nil)
18
+ driver = Faraday.new(url: base_url)
19
+ new(Requester.new(driver, login: login, password: password, language: language))
30
20
  end
31
21
 
32
- # Ad-hoc solution for adding multilanguage support
33
- def language=(value)
34
- requester.language = value
22
+ # Constructing routine that uses given driver
23
+ # @param driver - driver instance
24
+ # @param login: & password: - HTTP basic auth factors
25
+ # @param language: - language slug for which request will be performed (available are: en, ru, uk, pl, th)
26
+ def with_requester(driver, login: nil, password: nil, language: nil)
27
+ new(Requester.new(driver, login: login, password: password, language: language))
35
28
  end
29
+ end
36
30
 
37
- # GDS Method used to fetch GDS locations
38
- # For more information, see [http://demo.gillbus.com/v2/doc.html#получение-географии-получение-списка-остановок-get]
39
- # @param args - keyword args:
40
- # :country [Number|String] - GDS country id for which locations are fetched (optional)
41
- # :offset [Number|String] - number of records to be skipped when fetching chunks (optional)
42
- # :limit [Number|String] - number of records to be fetched when fetching chunks (optional)
43
- # :timestamp [String] - temporal checkpoint(i.e. date, records from which are interested)
44
- # (optional, format: yyyy-MM-dd’T’HH:mm:ssZ, GMT+2)
45
- def get_locations(**args)
46
- Methods::GetLocations.new(requester).call(**args)
47
- end
31
+ # Ad-hoc solution for adding multilanguage support
32
+ def language=(value)
33
+ requester.language = value
34
+ end
48
35
 
49
- # GDS Method used to fetch GDS points
50
- # For more information, see [http://demo.gillbus.com/v2/doc.html#получение-географии-получение-списка-остановок-get]
51
- # @param args - keyword args:
52
- # :country [Number|String] - GDS country id for which points are fetched (optional)
53
- # :location [Number|String] - GDS location id for which points are fetched (optional)
54
- # :offset [Number|String] - number of records to be skipped when fetching chunks (optional)
55
- # :limit [Number|String] - number of records to be fetched when fetching chunks (optional)
56
- # :timestamp [String] - temporal checkpoint(i.e. date, records from which are interested)
57
- # (optional, format: yyyy-MM-dd’T’HH:mm:ssZ, GMT+2)
58
- def get_points(**args)
59
- Methods::GetPoints.new(requester).call(**args)
60
- end
36
+ # GDS Method used to fetch GDS locations
37
+ # For more information, see [http://demo.gillbus.com/v2/doc.html#получение-географии-получение-списка-остановок-get]
38
+ # @param args - keyword args:
39
+ # :country [Number|String] - GDS country id for which locations are fetched (optional)
40
+ # :offset [Number|String] - number of records to be skipped when fetching chunks (optional)
41
+ # :limit [Number|String] - number of records to be fetched when fetching chunks (optional)
42
+ # :timestamp [String] - temporal checkpoint(i.e. date, records from which are interested)
43
+ # (optional, format: yyyy-MM-dd’T’HH:mm:ssZ, GMT+2)
44
+ def get_locations(**args)
45
+ Methods::GetLocations.new(requester).call(**args)
46
+ end
61
47
 
62
- # GDS Method used to fetch GDS countries
63
- # For more information, see [http://demo.gillbus.com/v2/doc.html#получение-географии-страны-get]
64
- # @param args - keyword args:
65
- # :offset [Number|String] - number of records to be skipped when fetching chunks (optional)
66
- # :limit [Number|String] - number of records to be fetched when fetching chunks (optional)
67
- def get_countries(**args)
68
- Methods::GetCountries.new(requester).call(**args)
69
- end
48
+ # GDS Method used to fetch GDS points
49
+ # For more information, see [http://demo.gillbus.com/v2/doc.html#получение-географии-получение-списка-остановок-get]
50
+ # @param args - keyword args:
51
+ # :country [Number|String] - GDS country id for which points are fetched (optional)
52
+ # :location [Number|String] - GDS location id for which points are fetched (optional)
53
+ # :offset [Number|String] - number of records to be skipped when fetching chunks (optional)
54
+ # :limit [Number|String] - number of records to be fetched when fetching chunks (optional)
55
+ # :timestamp [String] - temporal checkpoint(i.e. date, records from which are interested)
56
+ # (optional, format: yyyy-MM-dd’T’HH:mm:ssZ, GMT+2)
57
+ def get_points(**args)
58
+ Methods::GetPoints.new(requester).call(**args)
59
+ end
70
60
 
71
- # GDS Method used to check availability of trips
72
- # For more information, see [http://demo.gillbus.com/v2/doc.html#поиск-рейсов-доступность-рейсов-get]
73
- # @param args - keyword args:
74
- # :arrival [String] - GDS arrival point id (required)
75
- # :departure [String] - GDS departure point id (required)
76
- # :tickets [Number] - number of tickets for trip (required)
77
- # :mode [Number] - transport type (optional, buses are: 1,8)
78
- # :dates [String] - dates for trip (required, format: yyyy-mm-dd, delimiter: ';')
79
- # :return_only_branded [Boolean] - returns data only for branded trips if true
80
- def get_available_trips(**args)
81
- Methods::GetAvailableTrips.new(requester).call(**args)
82
- end
61
+ # GDS Method used to fetch GDS countries
62
+ # For more information, see [http://demo.gillbus.com/v2/doc.html#получение-географии-страны-get]
63
+ # @param args - keyword args:
64
+ # :offset [Number|String] - number of records to be skipped when fetching chunks (optional)
65
+ # :limit [Number|String] - number of records to be fetched when fetching chunks (optional)
66
+ def get_countries(**args)
67
+ Methods::GetCountries.new(requester).call(**args)
68
+ end
69
+
70
+ # GDS Method used to check availability of trips
71
+ # For more information, see [http://demo.gillbus.com/v2/doc.html#поиск-рейсов-доступность-рейсов-get]
72
+ # @param args - keyword args:
73
+ # :arrival [String] - GDS arrival point id (required)
74
+ # :departure [String] - GDS departure point id (required)
75
+ # :tickets [Number] - number of tickets for trip (required)
76
+ # :mode [Number] - transport type (optional, buses are: 1,8)
77
+ # :dates [String] - dates for trip (required, format: yyyy-mm-dd, delimiter: ';')
78
+ # :return_only_branded [Boolean] - returns data only for branded trips if true
79
+ def get_available_trips(**args)
80
+ Methods::GetAvailableTrips.new(requester).call(**args)
83
81
  end
84
82
  end
85
83
  end
@@ -1,103 +1,101 @@
1
1
  require_relative 'utils/parse_utils'
2
2
 
3
3
  module Gdsapi
4
- module V2
5
- module Methods
6
- class BaseMethod
7
- include ParseUtils
8
- attr_reader :requester
4
+ module Methods
5
+ class BaseMethod
6
+ include ParseUtils
7
+ attr_reader :requester
9
8
 
10
- # Constructor method for passing requester instance
11
- # @param [[Gdsapi::V2::Requester]] - requester instance
12
- def initialize(requester)
13
- @requester = requester
14
- end
9
+ # Constructor method for passing requester instance
10
+ # @param [[Gdsapi::Requester]] - requester instance
11
+ def initialize(requester)
12
+ @requester = requester
13
+ end
15
14
 
16
- ## Generic entry-point for action
17
- ## @param hash_styled `list` and `offset` args used for chunking requests
18
- ## @return - parsed results
19
- def call(**params)
20
- response_body = query params
21
- body = JSON.parse response_body
22
- parse body
23
- rescue JSON::ParserError
24
- raise MalformedGdsResponse, "Invalid JSON: #{response_body}"
25
- end
15
+ ## Generic entry-point for action
16
+ ## @param hash_styled `list` and `offset` args used for chunking requests
17
+ ## @return - parsed results
18
+ def call(**params)
19
+ response_body = query params
20
+ body = JSON.parse response_body
21
+ parse body
22
+ rescue JSON::ParserError
23
+ raise MalformedGdsResponse, "Invalid JSON: #{response_body}"
24
+ end
26
25
 
27
- protected
28
- # HTTP verb used to query GDS API for method
29
- # Defaults to :get
30
- # @return [Symbol] - symbolic alias for requester method corresponding to HTTP verb (i.e., :get or :post)
31
- def method
32
- :get
33
- end
26
+ protected
27
+ # HTTP verb used to query GDS API for method
28
+ # Defaults to :get
29
+ # @return [Symbol] - symbolic alias for requester method corresponding to HTTP verb (i.e., :get or :post)
30
+ def method
31
+ :get
32
+ end
34
33
 
35
- # GDS API path for method
36
- # It is required for method to specify path
37
- def parse(*args)
38
- raise NotImplementedError, 'Parsing is not allowed for abstract method'
39
- end
34
+ # GDS API path for method
35
+ # It is required for method to specify path
36
+ def parse(*args)
37
+ raise NotImplementedError, 'Parsing is not allowed for abstract method'
38
+ end
40
39
 
41
- # GDS API path for method
42
- # It is required for method to specify path
43
- def path
44
- raise NotImplementedError, 'Querying is not allowed for abstract method'
45
- end
40
+ # GDS API path for method
41
+ # It is required for method to specify path
42
+ def path
43
+ raise NotImplementedError, 'Querying is not allowed for abstract method'
44
+ end
46
45
 
47
- # Query routine to be executed while performing action
48
- # @param [Hash] args - args used for querying
49
- # @return [String] - response body string
50
- def query(args)
51
- params = transform_params(slice_params(args))
52
- response = requester.public_send(method, path, params: params)
53
- case response.status
54
- when (404)
55
- raise NotFoundError, "Not found on #{response.env.url}"
56
- when (401)
57
- raise UnathorizedError, "Invalid auth on #{response.env.url}"
58
- when (400..499)
59
- raise BadRequestError, "Bad request on #{response.env.url}: #{response.body.to_s}"
60
- when (500..599)
61
- raise GdsInternalError, "#{response.status} #{response.body.to_s}"
62
- when (300..399)
63
- raise GdsRedirectError, "#{response.status} #{response.headers['Location'].to_s}"
64
- else
65
- response.body
66
- end
46
+ # Query routine to be executed while performing action
47
+ # @param [Hash] args - args used for querying
48
+ # @return [String] - response body string
49
+ def query(args)
50
+ params = transform_params(slice_params(args))
51
+ response = requester.public_send(method, path, params: params)
52
+ case response.status
53
+ when (404)
54
+ raise NotFoundError, "Not found on #{response.env.url}"
55
+ when (401)
56
+ raise UnathorizedError, "Invalid auth on #{response.env.url}"
57
+ when (400..499)
58
+ raise BadRequestError, "Bad request on #{response.env.url}: #{response.body.to_s}"
59
+ when (500..599)
60
+ raise GdsInternalError, "#{response.status} #{response.body.to_s}"
61
+ when (300..399)
62
+ raise GdsRedirectError, "#{response.status} #{response.headers['Location'].to_s}"
63
+ else
64
+ response.body
67
65
  end
66
+ end
68
67
 
69
68
 
70
- # Transform params with accordance to requirements of API
71
- # @param [Hash[Symbol => Object]] filtered_params - passed params
72
- # @return [Hash[Symbol => Object]] transformed set of params
73
- def transform_params(filtered_params)
74
- filtered_params
75
- end
69
+ # Transform params with accordance to requirements of API
70
+ # @param [Hash[Symbol => Object]] filtered_params - passed params
71
+ # @return [Hash[Symbol => Object]] transformed set of params
72
+ def transform_params(filtered_params)
73
+ filtered_params
74
+ end
76
75
 
77
- # Select params which are allowed in querying for the method
78
- # @param [Hash[Symbol => Object]] params - passed params
79
- # @return [Hash[Symbol => Object]] params filtered using whitelist (see #permitted_params) and by presence (!nil?)
80
- def slice_params(params)
81
- params.select { |name, value| permitted_params.include?(name) && value }
82
- end
76
+ # Select params which are allowed in querying for the method
77
+ # @param [Hash[Symbol => Object]] params - passed params
78
+ # @return [Hash[Symbol => Object]] params filtered using whitelist (see #permitted_params) and by presence (!nil?)
79
+ def slice_params(params)
80
+ params.select { |name, value| permitted_params.include?(name) && value }
81
+ end
83
82
 
84
- # whitelist of permitted params
85
- # defaults to empty list
86
- # @return [Array[Symbol]] - params that are permitted for given instance of method
87
- def permitted_params
88
- []
89
- end
83
+ # whitelist of permitted params
84
+ # defaults to empty list
85
+ # @return [Array[Symbol]] - params that are permitted for given instance of method
86
+ def permitted_params
87
+ []
88
+ end
90
89
 
91
- # Editing geodata modes mapping (GDS -> output)
92
- # Used when dealing with temporal data (i.e. given timestamps)
93
- # U (upsert) - update or create record
94
- # D (delete) - delete record
95
- def modes
96
- {
97
- 'U' => :upsert,
98
- 'D' => :delete,
99
- }
100
- end
90
+ # Editing geodata modes mapping (GDS -> output)
91
+ # Used when dealing with temporal data (i.e. given timestamps)
92
+ # U (upsert) - update or create record
93
+ # D (delete) - delete record
94
+ def modes
95
+ {
96
+ 'U' => :upsert,
97
+ 'D' => :delete,
98
+ }
101
99
  end
102
100
  end
103
101
  end
@@ -1,33 +1,31 @@
1
1
  # GDS Method class used to check availability of trips
2
2
  # For more information, see [http://demo.gillbus.com/v2/doc.html#поиск-рейсов-доступность-рейсов-get]
3
3
  module Gdsapi
4
- module V2
5
- module Methods
6
- class GetAvailableTrips < BaseMethod
7
- private
4
+ module Methods
5
+ class GetAvailableTrips < BaseMethod
6
+ private
8
7
 
9
- ## Parsing routine to be executed while performing action
10
- ## Actually, does nothing
11
- ## @param [Array] body - parsed JSON payload from GDS
12
- ## @return [Array] set of available dates for given trip
13
- def parse(body)
14
- body
15
- end
8
+ ## Parsing routine to be executed while performing action
9
+ ## Actually, does nothing
10
+ ## @param [Array] body - parsed JSON payload from GDS
11
+ ## @return [Array] set of available dates for given trip
12
+ def parse(body)
13
+ body
14
+ end
16
15
 
17
- def path
18
- 'available_trips'
19
- end
16
+ def path
17
+ 'available_trips'
18
+ end
20
19
 
21
- # Query params
22
- # :arrival [String] - GDS arrival point id (required)
23
- # :departure [String] - GDS departure point id (required)
24
- # :tickets [Number] - number of tickets for trip (required)
25
- # :mode [Number] - transport type (optional, buses are: 1,8)
26
- # :dates [String] - dates for trip (required, format: yyyy-mm-dd, delimiter: ';')
27
- # :return_only_branded [Boolean] - returns data only for branded trips if true
28
- def permitted_params
29
- %i(arrival departure tickets mode dates return_only_branded)
30
- end
20
+ # Query params
21
+ # :arrival [String] - GDS arrival point id (required)
22
+ # :departure [String] - GDS departure point id (required)
23
+ # :tickets [Number] - number of tickets for trip (required)
24
+ # :mode [Number] - transport type (optional, buses are: 1,8)
25
+ # :dates [String] - dates for trip (required, format: yyyy-mm-dd, delimiter: ';')
26
+ # :return_only_branded [Boolean] - returns data only for branded trips if true
27
+ def permitted_params
28
+ %i(arrival departure tickets mode dates return_only_branded)
31
29
  end
32
30
  end
33
31
  end
@@ -1,42 +1,40 @@
1
1
  # GDS Method class used to fetch GDS countries
2
2
  # For more information, see [http://demo.gillbus.com/v2/doc.html#получение-географии-страны-get]
3
3
  module Gdsapi
4
- module V2
5
- module Methods
6
- class GetCountries < BaseMethod
4
+ module Methods
5
+ class GetCountries < BaseMethod
7
6
 
8
- private
7
+ private
9
8
 
10
- def path
11
- 'countries'
12
- end
9
+ def path
10
+ 'countries'
11
+ end
13
12
 
14
- ## Parsing routine to be executed while performing action
15
- ## @param [Hash] body - parsed JSON payload from GDS
16
- ## @return [Array[Gdsapi::V2::Structs::Country]] parsed set of countries
17
- def parse(body)
18
- return [] if body.empty?
13
+ ## Parsing routine to be executed while performing action
14
+ ## @param [Hash] body - parsed JSON payload from GDS
15
+ ## @return [Array[Gdsapi::Structs::Country]] parsed set of countries
16
+ def parse(body)
17
+ return [] if body.empty?
19
18
 
20
- countries = body['countries']
21
- types = body['types']
22
- attributes = body['attributes']
23
- countries.map do |country|
24
- args = {
25
- id: country['id'],
26
- name: country['name'],
27
- type: types.find { |type| type['id'] == country['type_id'] },
28
- attributes: match_attributes(country['attributes'], attributes),
29
- }
30
- Gdsapi::V2::Structs::Country.new args
31
- end
19
+ countries = body['countries']
20
+ types = body['types']
21
+ attributes = body['attributes']
22
+ countries.map do |country|
23
+ args = {
24
+ id: country['id'],
25
+ name: country['name'],
26
+ type: types.find { |type| type['id'] == country['type_id'] },
27
+ attributes: match_attributes(country['attributes'], attributes),
28
+ }
29
+ Gdsapi::Structs::Country.new args
32
30
  end
31
+ end
33
32
 
34
- # Query params
35
- # :offset [Number|String] - number of records to be skipped when fetching chunks (optional)
36
- # :limit [Number|String] - number of records to be fetched when fetching chunks (optional)
37
- def permitted_params
38
- %i(limit offset])
39
- end
33
+ # Query params
34
+ # :offset [Number|String] - number of records to be skipped when fetching chunks (optional)
35
+ # :limit [Number|String] - number of records to be fetched when fetching chunks (optional)
36
+ def permitted_params
37
+ %i(limit offset])
40
38
  end
41
39
  end
42
40
  end
@@ -1,69 +1,67 @@
1
1
  # GDS Method class used to fetch GDS locations
2
2
  # For more information, see [http://demo.gillbus.com/v2/doc.html#получение-географии-получение-населенных-пунктов-get]
3
3
  module Gdsapi
4
- module V2
5
- module Methods
6
- class GetLocations < BaseMethod
4
+ module Methods
5
+ class GetLocations < BaseMethod
7
6
 
8
- private
7
+ private
9
8
 
10
- # Parsing routine invoked on JSON response parsed to hash
11
- # @param [Hash] body - given response hash from GDS
12
- # @return [Hash[Symbol => Array[Gdsapi::V2::Structs::Location]]] - location objects grouped by editing mode:
13
- # :upsert - objects inserted or updated since given timestamp
14
- # :delete - objects deleted since given timestamp
15
- def parse(body)
16
- return {} if body.empty?
9
+ # Parsing routine invoked on JSON response parsed to hash
10
+ # @param [Hash] body - given response hash from GDS
11
+ # @return [Hash[Symbol => Array[Gdsapi::Structs::Location]]] - location objects grouped by editing mode:
12
+ # :upsert - objects inserted or updated since given timestamp
13
+ # :delete - objects deleted since given timestamp
14
+ def parse(body)
15
+ return {} if body.empty?
17
16
 
18
- locations = body['locations']
19
- types = body['types']
20
- attributes = body['attributes']
21
- sub_types = body['subTypes']
22
- grouped_locations = locations.group_by { |location| location['mod'] }
23
- parsed_locations = grouped_locations.map do |mod, hash_locations|
24
- parsed_locations = hash_locations.map do |location|
25
- args = {
26
- id: location['id'],
27
- name: location['name'],
28
- latitude: location['latitude'],
29
- longitude: location['longitude'],
30
- parent: location['parent'],
31
- type: types.find { |type| type['id'] == location['type_id'] },
32
- sub_type: sub_types.find { |sub_type| sub_type['id'] == location['sub_type_id'] },
33
- attributes: match_attributes(location['attributes'], attributes),
34
- }
35
- Gdsapi::V2::Structs::Location.new args
36
- end
37
- [modes[mod], parsed_locations]
17
+ locations = body['locations']
18
+ types = body['types']
19
+ attributes = body['attributes']
20
+ sub_types = body['subTypes']
21
+ grouped_locations = locations.group_by { |location| location['mod'] }
22
+ parsed_locations = grouped_locations.map do |mod, hash_locations|
23
+ parsed_locations = hash_locations.map do |location|
24
+ args = {
25
+ id: location['id'],
26
+ name: location['name'],
27
+ latitude: location['latitude'],
28
+ longitude: location['longitude'],
29
+ parent: location['parent'],
30
+ type: types.find { |type| type['id'] == location['type_id'] },
31
+ sub_type: sub_types.find { |sub_type| sub_type['id'] == location['sub_type_id'] },
32
+ attributes: match_attributes(location['attributes'], attributes),
33
+ }
34
+ Gdsapi::Structs::Location.new args
38
35
  end
39
- Hash[parsed_locations]
36
+ [modes[mod], parsed_locations]
40
37
  end
38
+ Hash[parsed_locations]
39
+ end
41
40
 
42
- def path
43
- 'locations'
44
- end
41
+ def path
42
+ 'locations'
43
+ end
45
44
 
46
- def transform_params(filtered_params)
47
- filtered_params.tap do |params|
48
- if params[:timestamp]
49
- if params[:timestamp].respond_to?(:strftime)
50
- params[:timestamp] = params[:timestamp].strftime('%FT%T%z')
51
- else
52
- params[:timestamp] = params[:timestamp].to_s
53
- end
45
+ def transform_params(filtered_params)
46
+ filtered_params.tap do |params|
47
+ if params[:timestamp]
48
+ if params[:timestamp].respond_to?(:strftime)
49
+ params[:timestamp] = params[:timestamp].strftime('%FT%T%z')
50
+ else
51
+ params[:timestamp] = params[:timestamp].to_s
54
52
  end
55
53
  end
56
54
  end
55
+ end
57
56
 
58
- # Query params
59
- # :country [Number|String] - GDS country id for which locations are fetched (optional)
60
- # :offset [Number|String] - number of records to be skipped when fetching chunks (optional)
61
- # :limit [Number|String] - number of records to be fetched when fetching chunks (optional)
62
- # :timestamp [String] - temporal checkpoint(i.e. date, records from which are interested)
63
- # (optional, format: yyyy-MM-dd’T’HH:mm:ssZ, GMT +2)
64
- def permitted_params
65
- %i(country offset limit timestamp)
66
- end
57
+ # Query params
58
+ # :country [Number|String] - GDS country id for which locations are fetched (optional)
59
+ # :offset [Number|String] - number of records to be skipped when fetching chunks (optional)
60
+ # :limit [Number|String] - number of records to be fetched when fetching chunks (optional)
61
+ # :timestamp [String] - temporal checkpoint(i.e. date, records from which are interested)
62
+ # (optional, format: yyyy-MM-dd’T’HH:mm:ssZ, GMT +2)
63
+ def permitted_params
64
+ %i(country offset limit timestamp)
67
65
  end
68
66
  end
69
67
  end
@@ -1,67 +1,65 @@
1
1
  # GDS Method class used to fetch GDS locations
2
2
  # For more information, see [http://demo.gillbus.com/v2/doc.html#получение-географии-получение-списка-остановок-get]
3
3
  module Gdsapi
4
- module V2
5
- module Methods
6
- class GetPoints < BaseMethod
4
+ module Methods
5
+ class GetPoints < BaseMethod
7
6
 
8
- private
7
+ private
9
8
 
10
- # Parsing routine invoked on JSON response parsed to hash
11
- # @param [Hash] body - given response hash from GDS
12
- # @return [Hash[Symbol => Array[Gdsapi::V2::Structs::Point]]] - point objects grouped by editing mode:
13
- # :upsert - objects inserted or updated since given timestamp
14
- # :delete - objects deleted since given timestamp
15
- def parse(body)
16
- return {} if body.empty?
9
+ # Parsing routine invoked on JSON response parsed to hash
10
+ # @param [Hash] body - given response hash from GDS
11
+ # @return [Hash[Symbol => Array[Gdsapi::Structs::Point]]] - point objects grouped by editing mode:
12
+ # :upsert - objects inserted or updated since given timestamp
13
+ # :delete - objects deleted since given timestamp
14
+ def parse(body)
15
+ return {} if body.empty?
17
16
 
18
- points = body['points']
19
- attributes = body['attributes']
20
- grouped_points = points.group_by { |point| point['mod'] }
21
- parsed_points = grouped_points.map do |mod, hash_points|
22
- parsed_points = hash_points.map do |point|
23
- args = {
24
- id: point['id'],
25
- name: point['name'],
26
- latitude: point['latitude'],
27
- longitude: point['longitude'],
28
- parent: point['parent'],
29
- address: point['address'],
30
- attributes: match_attributes(point['attributes'], attributes),
31
- }
32
- Gdsapi::V2::Structs::Point.new args
33
- end
34
- [modes[mod], parsed_points]
17
+ points = body['points']
18
+ attributes = body['attributes']
19
+ grouped_points = points.group_by { |point| point['mod'] }
20
+ parsed_points = grouped_points.map do |mod, hash_points|
21
+ parsed_points = hash_points.map do |point|
22
+ args = {
23
+ id: point['id'],
24
+ name: point['name'],
25
+ latitude: point['latitude'],
26
+ longitude: point['longitude'],
27
+ parent: point['parent'],
28
+ address: point['address'],
29
+ attributes: match_attributes(point['attributes'], attributes),
30
+ }
31
+ Gdsapi::Structs::Point.new args
35
32
  end
36
- Hash[parsed_points]
33
+ [modes[mod], parsed_points]
37
34
  end
35
+ Hash[parsed_points]
36
+ end
38
37
 
39
- def path
40
- 'points'
41
- end
38
+ def path
39
+ 'points'
40
+ end
42
41
 
43
- def transform_params(filtered_params)
44
- filtered_params.tap do |params|
45
- if params[:timestamp]
46
- if params[:timestamp].respond_to?(:strftime)
47
- params[:timestamp] = params[:timestamp].strftime('%FT%T%z')
48
- else
49
- params[:timestamp] = params[:timestamp].to_s
50
- end
42
+ def transform_params(filtered_params)
43
+ filtered_params.tap do |params|
44
+ if params[:timestamp]
45
+ if params[:timestamp].respond_to?(:strftime)
46
+ params[:timestamp] = params[:timestamp].strftime('%FT%T%z')
47
+ else
48
+ params[:timestamp] = params[:timestamp].to_s
51
49
  end
52
50
  end
53
51
  end
52
+ end
54
53
 
55
- # Query params
56
- # :country [Number|String] - GDS country id for which points are fetched (optional)
57
- # :location [Number|String] - GDS location id for which points are fetched (optional)
58
- # :offset [Number|String] - number of records to be skipped when fetching chunks (optional)
59
- # :limit [Number|String] - number of records to be fetched when fetching chunks (optional)
60
- # :timestamp [String] - temporal checkpoint(i.e. date, records from which are interested)
61
- # (optional, format: yyyy-MM-dd’T’HH:mm:ssZ, GMT +2)
62
- def permitted_params
63
- %i(country location offset limit timestamp)
64
- end
54
+ # Query params
55
+ # :country [Number|String] - GDS country id for which points are fetched (optional)
56
+ # :location [Number|String] - GDS location id for which points are fetched (optional)
57
+ # :offset [Number|String] - number of records to be skipped when fetching chunks (optional)
58
+ # :limit [Number|String] - number of records to be fetched when fetching chunks (optional)
59
+ # :timestamp [String] - temporal checkpoint(i.e. date, records from which are interested)
60
+ # (optional, format: yyyy-MM-dd’T’HH:mm:ssZ, GMT +2)
61
+ def permitted_params
62
+ %i(country location offset limit timestamp)
65
63
  end
66
64
  end
67
65
  end
@@ -1,32 +1,30 @@
1
1
  # Module representing parsing utility methods
2
2
  module Gdsapi
3
- module V2
4
- module Methods
5
- module ParseUtils
6
- ## Helping routine for matching given attributes with dictionary
7
- ## @param set [Array[Hash] | nil] - set of {'id', 'value'}-ish attributes for country
8
- ## @param dictionary [Array[Hash]] - dictionary of {'id', 'name'}-ish attributes
9
- ## @return [Array[Hash]|nil] - {id:, name:, value:}-ish attributes hash if set is present, nil otherwise
10
- def match_attributes(set, dictionary)
11
- return unless set
3
+ module Methods
4
+ module ParseUtils
5
+ ## Helping routine for matching given attributes with dictionary
6
+ ## @param set [Array[Hash] | nil] - set of {'id', 'value'}-ish attributes for country
7
+ ## @param dictionary [Array[Hash]] - dictionary of {'id', 'name'}-ish attributes
8
+ ## @return [Array[Hash]|nil] - {id:, name:, value:}-ish attributes hash if set is present, nil otherwise
9
+ def match_attributes(set, dictionary)
10
+ return unless set
12
11
 
13
- grouped_set = set.group_by { |attribute| attribute['id'] }
14
- grouped_set.map do |id, set_attributes|
15
- dictionary_attribute = dictionary.find {|dict_attr| dict_attr['id'] == id }
16
- values = set_attributes.map { |set_attribute| set_attribute['value'] }
17
- if dictionary_attribute
18
- {
19
- id: dictionary_attribute['id'],
20
- name: dictionary_attribute['name'],
21
- values: values,
22
- }
23
- else
24
- raise MalformedGdsResponse,
25
- "Invalid response: missing attribute #{id}"
26
- end
12
+ grouped_set = set.group_by { |attribute| attribute['id'] }
13
+ grouped_set.map do |id, set_attributes|
14
+ dictionary_attribute = dictionary.find {|dict_attr| dict_attr['id'] == id }
15
+ values = set_attributes.map { |set_attribute| set_attribute['value'] }
16
+ if dictionary_attribute
17
+ {
18
+ id: dictionary_attribute['id'],
19
+ name: dictionary_attribute['name'],
20
+ values: values,
21
+ }
22
+ else
23
+ raise MalformedGdsResponse,
24
+ "Invalid response: missing attribute #{id}"
27
25
  end
28
26
  end
29
27
  end
30
28
  end
31
29
  end
32
- end
30
+ end
@@ -1,8 +1,6 @@
1
1
  #TODO: Here provide package documentation
2
2
  ## This package is used to declare GDS method types
3
3
  module Gdsapi
4
- module V2
5
- module Methods
6
- end
4
+ module Methods
7
5
  end
8
6
  end
@@ -2,52 +2,50 @@
2
2
  # Encapsulates supportive data to be used when querying
3
3
  # Also responsible for performing HTTP requests
4
4
  module Gdsapi
5
- module V2
6
- class Requester
7
- attr_reader :login
8
- attr_reader :password
9
- attr_reader :driver
10
- attr_reader :language
5
+ class Requester
6
+ attr_reader :login
7
+ attr_reader :password
8
+ attr_reader :driver
9
+ attr_reader :language
11
10
 
12
- # @param [Faraday::Connection] driver - driver instance, i.e. object that responds to
13
- # #get(url, params, headers),
14
- # #post(url, body, params),
15
- # #basic_auth(login, password),
16
- # and querying and modifying #headers in Hash-style
17
- # It is `strongly recommended` to use Faraday
18
- # @param login: & password: - HTTP basic auth factors
19
- # @param language: - language slug for which request will be performed
20
- def initialize(driver, login: '', password: '', language: 'ru')
21
- @driver = driver
22
- @login = login || ''
23
- @password = password || ''
24
- @language = language || 'ru'
25
- set_language
26
- set_basic_auth
27
- end
11
+ # @param [Faraday::Connection] driver - driver instance, i.e. object that responds to
12
+ # #get(url, params, headers),
13
+ # #post(url, body, params),
14
+ # #basic_auth(login, password),
15
+ # and querying and modifying #headers in Hash-style
16
+ # It is `strongly recommended` to use Faraday
17
+ # @param login: & password: - HTTP basic auth factors
18
+ # @param language: - language slug for which request will be performed
19
+ def initialize(driver, login: '', password: '', language: 'ru')
20
+ @driver = driver
21
+ @login = login || ''
22
+ @password = password || ''
23
+ @language = language || 'ru'
24
+ set_language
25
+ set_basic_auth
26
+ end
28
27
 
29
- def language=(value)
30
- @language = value
31
- set_language
32
- end
28
+ def language=(value)
29
+ @language = value
30
+ set_language
31
+ end
33
32
 
34
- def get(url, params: {}, headers: {})
35
- driver.get url, params, headers
36
- end
33
+ def get(url, params: {}, headers: {})
34
+ driver.get url, params, headers
35
+ end
37
36
 
38
- def post(url, body: {}, headers: {})
39
- driver.post url, body, headers
40
- end
37
+ def post(url, body: {}, headers: {})
38
+ driver.post url, body, headers
39
+ end
41
40
 
42
- private
41
+ private
43
42
 
44
- def set_language
45
- driver.headers['Accept-Language'] = language
46
- end
43
+ def set_language
44
+ driver.headers['Accept-Language'] = language
45
+ end
47
46
 
48
- def set_basic_auth
49
- driver.basic_auth login, password
50
- end
47
+ def set_basic_auth
48
+ driver.basic_auth login, password
51
49
  end
52
50
  end
53
51
  end
@@ -2,21 +2,19 @@ require_relative '../structs'
2
2
 
3
3
  ## Country struct
4
4
  ## Used when syncing geodata with GDS
5
- ## See [Gdsapi::V2::Methods::GetCountries] for details
5
+ ## See [Gdsapi::Methods::GetCountries] for details
6
6
  ## Also see http://demo.gillbus.com/v2/doc.html#получение-географии-страны for schema definition
7
7
  module Gdsapi
8
- module V2
9
- module Structs
10
- class Country < ::Dry::Struct
11
- attribute :id, Structs::Coercible::Int
12
- attribute :name, Structs::Coercible::String
13
- attribute :type, Type.optional
14
- attribute :attributes, Attributes.optional
8
+ module Structs
9
+ class Country < ::Dry::Struct
10
+ attribute :id, Structs::Coercible::Int
11
+ attribute :name, Structs::Coercible::String
12
+ attribute :type, Type.optional
13
+ attribute :attributes, Attributes.optional
15
14
 
16
- def attribute_by_id(id)
17
- if attributes && id
18
- attributes.find {|attribute| attribute.id == id}
19
- end
15
+ def attribute_by_id(id)
16
+ if attributes && id
17
+ attributes.find {|attribute| attribute.id == id}
20
18
  end
21
19
  end
22
20
  end
@@ -1,24 +1,22 @@
1
1
  ## Country struct
2
2
  ## Used when syncing geodata with GDS
3
- ## See [[Gdsapi::V2::Methods::GetLocations]] for details
3
+ ## See [[Gdsapi::Methods::GetLocations]] for details
4
4
  ## Also see [http://demo.gillbus.com/v2/doc.html#получение-географии-получение-населенных-пунктов-get] for schema definition
5
5
  module Gdsapi
6
- module V2
7
- module Structs
8
- class Location < ::Dry::Struct
9
- attribute :id, Structs::Coercible::Int
10
- attribute :name, Structs::Coercible::String
11
- attribute :latitude, Structs::Coercible::Float
12
- attribute :longitude, Structs::Coercible::Float
13
- attribute :parent, Parent.optional
14
- attribute :type, Type.optional
15
- attribute :sub_type, Type.optional
16
- attribute :attributes, Attributes.optional
6
+ module Structs
7
+ class Location < ::Dry::Struct
8
+ attribute :id, Structs::Coercible::Int
9
+ attribute :name, Structs::Coercible::String
10
+ attribute :latitude, Structs::Coercible::Float
11
+ attribute :longitude, Structs::Coercible::Float
12
+ attribute :parent, Parent.optional
13
+ attribute :type, Type.optional
14
+ attribute :sub_type, Type.optional
15
+ attribute :attributes, Attributes.optional
17
16
 
18
- def attribute_by_id(id)
19
- if attributes && id
20
- attributes.find {|attribute| attribute.id == id}
21
- end
17
+ def attribute_by_id(id)
18
+ if attributes && id
19
+ attributes.find {|attribute| attribute.id == id}
22
20
  end
23
21
  end
24
22
  end
@@ -1,18 +1,16 @@
1
1
  ## Point struct
2
2
  ## Used when syncing geodata with GDS
3
- ## See [[Gdsapi::V2::Methods::GetPoints]] for details
3
+ ## See [[Gdsapi::Methods::GetPoints]] for details
4
4
  ## Also see [http://demo.gillbus.com/v2/doc.html#получение-географии-получение-списка-остановок-get] for schema definition
5
5
  module Gdsapi
6
- module V2
7
- module Structs
8
- class Point < ::Dry::Struct
9
- attribute :id, Structs::Coercible::Int
10
- attribute :name, Structs::Coercible::String
11
- attribute :latitude, Structs::Coercible::Float
12
- attribute :longitude, Structs::Coercible::Float
13
- attribute :parent, Parent.optional
14
- attribute :address, Structs::Coercible::String
15
- end
6
+ module Structs
7
+ class Point < ::Dry::Struct
8
+ attribute :id, Structs::Coercible::Int
9
+ attribute :name, Structs::Coercible::String
10
+ attribute :latitude, Structs::Coercible::Float
11
+ attribute :longitude, Structs::Coercible::Float
12
+ attribute :parent, Parent.optional
13
+ attribute :address, Structs::Coercible::String
16
14
  end
17
15
  end
18
16
  end
@@ -3,17 +3,15 @@ require 'dry-struct'
3
3
  ## This package is used to declare struct types expected from GDS
4
4
  ## dry-struct is used in order to provide type definitions and flexible coercions mechanism
5
5
  module Gdsapi
6
- module V2
7
- module Structs
8
- include Dry::Types.module
6
+ module Structs
7
+ include Dry::Types.module
9
8
 
10
- Attributes = Structs::Array.member(Structs::Hash.symbolized(id: Structs::Coercible::Int,
11
- name: Structs::Coercible::String,
12
- values: Structs::Array.member(Structs::Coercible::String)))
9
+ Attributes = Structs::Array.member(Structs::Hash.symbolized(id: Structs::Coercible::Int,
10
+ name: Structs::Coercible::String,
11
+ values: Structs::Array.member(Structs::Coercible::String)))
13
12
 
14
- Type = Structs::Hash.symbolized(id: Structs::Coercible::Int, name: Structs::Coercible::String)
13
+ Type = Structs::Hash.symbolized(id: Structs::Coercible::Int, name: Structs::Coercible::String)
15
14
 
16
- Parent = Structs::Hash.symbolized(id: Structs::Coercible::Int)
17
- end
15
+ Parent = Structs::Hash.symbolized(id: Structs::Coercible::Int)
18
16
  end
19
17
  end
@@ -1,5 +1,3 @@
1
1
  module Gdsapi
2
- module V2
3
- VERSION = "0.1.3"
4
- end
2
+ VERSION = "0.1.4"
5
3
  end
data/lib/gdsapi.rb CHANGED
@@ -3,20 +3,18 @@ Dir[File.expand_path('gdsapi/**/*.rb', __dir__)].each { |file| require file }
3
3
  # This provides core package manifesto
4
4
  ## Possible error definitions are provided here
5
5
  module Gdsapi
6
- module V2
7
- # Error thrown on something done wrong on request-side
8
- class BadRequestError < StandardError ; end
9
- # Error thrown on 401 from GDS
10
- class UnathorizedError < BadRequestError ; end
11
- # Error thrown on 404 from GDS
12
- class NotFoundError < BadRequestError ; end
13
- # Error thrown when something is weird with GDS
14
- class GdsError < StandardError ; end
15
- # Error thrown when Gds returned server-side exception
16
- class GdsInternalError < GdsError ; end
17
- # Error thrown when GDS returned sudden redirect
18
- class GdsRedirectError < GdsError ; end
19
- # Error thrown when GDS returned somewhat invalid
20
- class MalformedGdsResponse < GdsError ; end
21
- end
6
+ # Error thrown on something done wrong on request-side
7
+ class BadRequestError < StandardError ; end
8
+ # Error thrown on 401 from GDS
9
+ class UnathorizedError < BadRequestError ; end
10
+ # Error thrown on 404 from GDS
11
+ class NotFoundError < BadRequestError ; end
12
+ # Error thrown when something is weird with GDS
13
+ class GdsError < StandardError ; end
14
+ # Error thrown when Gds returned server-side exception
15
+ class GdsInternalError < GdsError ; end
16
+ # Error thrown when GDS returned sudden redirect
17
+ class GdsRedirectError < GdsError ; end
18
+ # Error thrown when GDS returned somewhat invalid
19
+ class MalformedGdsResponse < GdsError ; end
22
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gdsapi-v2-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Yurchenkov