gillbus-v2 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: c0b1da1daa5d8dbad4918b9924877bb40492c30b4557daa20464505493b9d78b
4
- data.tar.gz: 5ffe4476faafc56d9fca5803b8f45651a5cb14a6ea5a80ace63d9b126ddd2168
3
+ metadata.gz: e492282b609d3c3b20ad09f90a14def0027ace86577f4fc5f38152f7aef7646e
4
+ data.tar.gz: 4a0ab148b572b5dbb982a73d6462371070a1fec9ca8e9d83d4446bd599e338c3
5
5
  SHA512:
6
- metadata.gz: 7ff084e7e3e8cd633467e573f500e5df9d501065b71ba2c5a3126962fef7a33002b8bbd27d2d8ebc6c4886b28326b62d317220136e4111bc93944c292975317c
7
- data.tar.gz: a87780270f9bd71b62009114c2dff5c0a88a5b4d1422c57dda33ad96131a599bfde6acf73b1355ee615d9ee72b2c235fb748edde24849f48f6e38dc8c398f3a0
6
+ metadata.gz: c59e9c4d3f612f2e5f01d865fabc123a20ca0d6ec7d0f77eaee5c57a6ee08efd42fbb5f7a123d6a7657600d3c54b920295d4f29a13130ebb22b2029b11685944
7
+ data.tar.gz: f646e149e2dda7b7f82dc0e9cf7f886aeefdc69c2e2133302ead3fa50bd01fc381dff176a6d0082353edfa22be53b509983693dd4457fc177112490ad014c9b1
data/README.md CHANGED
@@ -78,6 +78,78 @@ end
78
78
 
79
79
  The method `expired?` returns `false` when there is no info about the token’s expiration time.
80
80
 
81
+ ### Fetch locations
82
+
83
+ For fetch the first page you should use method `get_locations`, other pages - `get_locations_page`. For example:
84
+
85
+ ```ruby
86
+ response = client.get_locations(
87
+ langs: %w[en ru uk pl],
88
+ limit_for_page: 100,
89
+ )
90
+ # first 100 locations
91
+ response.locations
92
+
93
+ while response.pagination.next_page
94
+ response = client.get_locations_page(
95
+ pagination_uuid: response.pagination.uuid,
96
+ page_number: response.pagination.next_page,
97
+ )
98
+ # other locations by 100 records
99
+ response.locations
100
+ end
101
+ ```
102
+
103
+ Every response should contain the following data:
104
+
105
+ ```ruby
106
+ # list of locations
107
+ response.locations
108
+
109
+ # dictionaries
110
+ response.location_types
111
+ response.location_sub_types
112
+ response.location_additional_fields
113
+
114
+ # usage of dictionaries
115
+ location = response.locations.first
116
+ location_type =
117
+ response.location_types.detect do |item|
118
+ item.id == location.type_id
119
+ end
120
+ location_sub_type =
121
+ response.location_sub_types.detect do |item|
122
+ item.id == location.sub_type_id
123
+ end
124
+ location.additional_fields.each do |field_id, field_value|
125
+ field =
126
+ response.location_additional_fields.detect do |item|
127
+ item.id == field_id
128
+ end
129
+ field_name = field.name
130
+ end
131
+ ```
132
+
133
+ Also you can load changes from a certain point of time instead of loading of the whole list. In order to do this you should pass param`from_datetime` to `get_locations`. Only the changed locations will be included in the response.
134
+
135
+ ```ruby
136
+ # get changes for last hour
137
+ response = client.get_locations(
138
+ langs: %w[en ru uk pl],
139
+ limit_for_page: 10,
140
+ from_datetime: DateTime.now - 3600,
141
+ )
142
+
143
+ while response.pagination.next_page
144
+ # ...
145
+ end
146
+ ```
147
+
148
+ Each location contains fields:
149
+
150
+ - `modified_at` - last modification time
151
+ - `deleted` - if true then location has been removed
152
+
81
153
  ## Development
82
154
 
83
155
  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.
@@ -10,6 +10,12 @@ require "gillbus/v2/client"
10
10
  require "gillbus/v2/parser"
11
11
  require "gillbus/v2/structs/base"
12
12
  require "gillbus/v2/structs/access_token"
13
+ require "gillbus/v2/structs/pagination"
14
+ require "gillbus/v2/structs/location"
15
+ require "gillbus/v2/structs/location_type"
16
+ require "gillbus/v2/structs/location_sub_type"
17
+ require "gillbus/v2/structs/location_additional_field"
13
18
 
14
19
  require "gillbus/v2/responses/base"
15
20
  require "gillbus/v2/responses/authenticate"
21
+ require "gillbus/v2/responses/locations"
@@ -41,6 +41,27 @@ module Gillbus::V2
41
41
  end
42
42
  end
43
43
 
44
+ def get_locations(langs: %w[en], limit_for_page: 50, from_datetime: nil)
45
+ params = {
46
+ lang_array: langs,
47
+ limit: limit_for_page,
48
+ timestamp: from_datetime&.rfc3339,
49
+ }
50
+ call_api(:get, "/geo/v2/locations", params,
51
+ response_class: Responses::Locations,
52
+ )
53
+ end
54
+
55
+ def get_locations_page(pagination_uuid:, page_number:)
56
+ params = {
57
+ page_uuid: pagination_uuid,
58
+ number_page: page_number,
59
+ }
60
+ call_api(:get, "/geo/v2/locations/page", params,
61
+ response_class: Responses::Locations,
62
+ )
63
+ end
64
+
44
65
  private
45
66
 
46
67
  def call_api(http_method, url, params, auth_required: true, response_class: Responses::Base)
@@ -56,6 +77,9 @@ module Gillbus::V2
56
77
  request.params[key.to_s] = value
57
78
  end
58
79
  request.headers['accept'] = 'application/json'
80
+ if auth_required
81
+ request.headers['Authorization'] = access_token.auth_header
82
+ end
59
83
  end
60
84
  rescue Faraday::Error => e
61
85
  raise e
@@ -68,6 +92,7 @@ module Gillbus::V2
68
92
  @connection ||= Faraday.new(url: base_url) do |faraday|
69
93
  faraday.request :url_encoded
70
94
  faraday.adapter Faraday.default_adapter
95
+ faraday.options[:params_encoder] = Faraday::FlatParamsEncoder
71
96
  end
72
97
  end
73
98
  end
@@ -2,29 +2,51 @@ module Gillbus::V2
2
2
  class Parser
3
3
  def parse_fields(raw_data, fields_settings)
4
4
  result = {}
5
- fields_settings.each do |name:, type:, default:|
6
- raw_value = raw_data[name.to_s]
7
- result[name] =
5
+ fields_settings.each do |field|
6
+ if field[:type] == :translations_hash && raw_data["translations"]
7
+ result[field[:name]] =
8
+ fetch_from_translations(raw_data["translations"], field[:from])
9
+ next
10
+ end
11
+
12
+ raw_value = raw_data[field[:from]]
13
+ result[field[:name]] =
8
14
  if raw_value.nil?
9
- default
15
+ field[:default]
10
16
  else
11
- coerce_value(raw_value, type)
17
+ coerce_value(raw_value, field[:type])
12
18
  end
13
19
  end
14
20
  result
15
21
  end
16
22
 
23
+ private
24
+
17
25
  def coerce_value(value, type)
18
26
  case type
27
+ when :boolean
28
+ value
19
29
  when :string
20
30
  value.to_s
21
31
  when :integer
22
32
  value.to_i
33
+ when :float
34
+ value.to_f
35
+ when :date_time
36
+ DateTime.rfc3339(value)
23
37
  when :date_time_from_timestamp
24
38
  DateTime.strptime(value.to_s, "%s")
39
+ when :translations_hash
40
+ value.to_h
25
41
  else
26
42
  raise "Type #{type} not supported"
27
43
  end
28
44
  end
45
+
46
+ def fetch_from_translations(translations, field_name)
47
+ translations.each_with_object({}) do |item, result|
48
+ result[item["lang"]] = item[field_name]
49
+ end
50
+ end
29
51
  end
30
52
  end
@@ -0,0 +1,77 @@
1
+ module Gillbus::V2
2
+ module Responses
3
+ class Locations < Responses::Base
4
+ def locations
5
+ @locations ||= (json_body["locations"] || []).map do |item|
6
+ Structs::Location.from_raw_data(item)
7
+ end
8
+ end
9
+
10
+ def location_types
11
+ @location_types ||= begin
12
+ data = formatted_dictionaries["location_types"]&.values || []
13
+ data.map do |item|
14
+ Structs::LocationType.from_raw_data(item)
15
+ end
16
+ end
17
+ end
18
+
19
+ def location_sub_types
20
+ @location_sub_types ||= begin
21
+ data = formatted_dictionaries["location_subtypes"]&.values || []
22
+ data.map do |item|
23
+ Structs::LocationSubType.from_raw_data(item)
24
+ end
25
+ end
26
+ end
27
+
28
+ def location_additional_fields
29
+ @location_additional_fields ||= begin
30
+ data = formatted_dictionaries["location_data_type"]&.values || []
31
+ data.map do |item|
32
+ Structs::LocationAdditionalField.from_raw_data(item)
33
+ end
34
+ end
35
+ end
36
+
37
+ def pagination
38
+ @pagination ||=
39
+ if json_body["pages_info"]
40
+ Structs::Pagination.from_raw_data(json_body["pages_info"])
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def formatted_dictionaries
47
+ @formatted_dictionaries ||=
48
+ format_dictionaries(json_body["dictionaries"])
49
+ end
50
+
51
+ def format_dictionaries(dictionaries)
52
+ result = {}
53
+ return result unless dictionaries
54
+
55
+ dictionaries.each do |lang, dicts|
56
+ dicts.each do |dict_key, dict|
57
+ dict.each do |item|
58
+ item_id = item["id"]
59
+ result[dict_key] ||= {}
60
+ result[dict_key][item_id] ||= {}
61
+ item.each do |item_attr, attr_value|
62
+ if item_attr == "id"
63
+ result[dict_key][item_id][item_attr] ||= attr_value
64
+ else
65
+ result[dict_key][item_id][item_attr] ||= {}
66
+ result[dict_key][item_id][item_attr][lang] = attr_value
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ result
74
+ end
75
+ end
76
+ end
77
+ end
@@ -17,5 +17,9 @@ module Gillbus::V2
17
17
  def to_s
18
18
  access_token
19
19
  end
20
+
21
+ def auth_header
22
+ "#{token_type} #{access_token}"
23
+ end
20
24
  end
21
25
  end
@@ -10,12 +10,13 @@ module Gillbus::V2
10
10
  end
11
11
  end
12
12
 
13
- def self.field(name, type, default: nil)
13
+ def self.field(name, type, from: name, default: nil)
14
14
  attr_reader name
15
15
  @fields_settings ||= []
16
16
  @fields_settings << {
17
17
  name: name,
18
18
  type: type,
19
+ from: from.to_s,
19
20
  default: default,
20
21
  }
21
22
  end
@@ -0,0 +1,29 @@
1
+ module Gillbus::V2
2
+ class Structs::Location < Structs::Base
3
+ field :id, :integer
4
+ field :parent_id, :integer
5
+ field :country_id, :integer
6
+ field :type_id, :integer
7
+ field :sub_type_id, :integer, from: "subtype_id"
8
+
9
+ field :modified_at, :date_time, from: "date_modified"
10
+ field :deleted, :boolean
11
+
12
+ field :latitude, :float
13
+ field :longitude, :float
14
+
15
+ field :timezone, :string
16
+
17
+ field :default_name, :string
18
+ field :name, :translations_hash
19
+ field :region_name, :translations_hash
20
+ field :country_name, :translations_hash
21
+
22
+ def additional_fields
23
+ @additional_fields ||=
24
+ raw_data["location_data"].each_with_object({}) do |item, result|
25
+ result[item["id"]] = item["value"]
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,6 @@
1
+ module Gillbus::V2
2
+ class Structs::LocationAdditionalField < Structs::Base
3
+ field :id, :integer
4
+ field :name, :translations_hash
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+ module Gillbus::V2
2
+ class Structs::LocationSubType < Structs::Base
3
+ field :id, :integer
4
+ field :name, :translations_hash
5
+ field :short_name, :translations_hash
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ module Gillbus::V2
2
+ class Structs::LocationType < Structs::Base
3
+ field :id, :integer
4
+ field :name, :translations_hash
5
+ end
6
+ end
@@ -0,0 +1,12 @@
1
+ module Gillbus::V2
2
+ class Structs::Pagination < Structs::Base
3
+ field :uuid, :string, from: "page_uuid"
4
+ field :pages_count, :integer, from: "page_count"
5
+ field :current_page, :integer, from: "page_number"
6
+ field :expires_in, :integer
7
+
8
+ def next_page
9
+ current_page + 1 if current_page < pages_count
10
+ end
11
+ end
12
+ end
@@ -1,5 +1,5 @@
1
1
  module Gillbus
2
2
  module V2
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
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.1
4
+ version: 0.1.2
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-03-28 00:00:00.000000000 Z
11
+ date: 2019-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -131,8 +131,14 @@ files:
131
131
  - lib/gillbus/v2/parser.rb
132
132
  - lib/gillbus/v2/responses/authenticate.rb
133
133
  - lib/gillbus/v2/responses/base.rb
134
+ - lib/gillbus/v2/responses/locations.rb
134
135
  - lib/gillbus/v2/structs/access_token.rb
135
136
  - lib/gillbus/v2/structs/base.rb
137
+ - lib/gillbus/v2/structs/location.rb
138
+ - lib/gillbus/v2/structs/location_additional_field.rb
139
+ - lib/gillbus/v2/structs/location_sub_type.rb
140
+ - lib/gillbus/v2/structs/location_type.rb
141
+ - lib/gillbus/v2/structs/pagination.rb
136
142
  - lib/gillbus/v2/version.rb
137
143
  homepage: https://github.com/busfor/gillbus-v2-ruby
138
144
  licenses: