ruby-lokalise-api 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f2212a93b34d63ef5bec7884f3003088d74a2c616affdf9931ad9767ae8b1f5d
4
- data.tar.gz: e56cee6b0d8bc2974591f215191a1543f2d1b9d28a774b46c7bec1fff96c5ccb
3
+ metadata.gz: 5258f1c07fdb9ffa68ab473b4bddf714f42874a049fa6657f1c3791f3cad3f6b
4
+ data.tar.gz: '05957d96074c503f46c49fb0a88292e6b5a30248c643d094b5dbabfa51dbb7b3'
5
5
  SHA512:
6
- metadata.gz: ec48688594d2492bade883ac5dda7baa533d9f27a6c7cd59dc90cfd30e7be5e8517885f85ca3a43b3d8009bd8052945334a4c38dcee60a1a4720960289e4e338
7
- data.tar.gz: 2c367579e4617dc4c89474afbd09b7b62b10a768ddaca01162934527e3211391f225ea39d3cc1a8fa1310c661d9eaab758c8239a887c2d7e091c0f4152211be0
6
+ metadata.gz: 5539b8cf6ab910c93ffe47b1ffd1b47ac4d323c971560d869fdf7dd0531e81b2312bbc45f1d4c6ea3d30af4fd451726bd14333a440bec0d8fb85ffc5c30caa62
7
+ data.tar.gz: c02b44af1ca82999d596e278fa65c7831590080f64168ab16b7d9f407e04087fdbe92e4d7177ab41bb56cedde30624277dce8fb2bf22873439c9160372bf83a4
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.1.0
4
+
5
+ * Added methods to work with pagination (`next_page?`, `last_page?`, `prev_page?`, `first_page?`, `next_page`, `prev_page`)
6
+
7
+ ## 1.0.1 (10-Dec-18)
8
+
9
+ * Fixed incorrect build
10
+
3
11
  ## 1.0.0 (10-Dec-18)
4
12
 
5
13
  * Initial release
data/README.md CHANGED
@@ -99,11 +99,29 @@ Collections respond to the following methods:
99
99
  * `#total_results`
100
100
  * `#results_per_page`
101
101
  * `#current_page`
102
+ * `#next_page?`
103
+ * `#last_page?`
104
+ * `#prev_page?`
105
+ * `#first_page?`
102
106
 
103
107
  For example:
104
108
 
105
109
  ```ruby
106
110
  projects.current_page #=> 3
111
+ projects.last_page? #=> true, this is the last page and there are no more projects available
112
+ ```
113
+
114
+ On top of that, you may easily fetch the next or the previous page of the collection by using:
115
+
116
+ * `#next_page`
117
+ * `#prev_page`
118
+
119
+ These methods return instances of the same collection class or `nil` if the next/previous page is unavailable. Methods respect the parameters you've initially passed:
120
+
121
+ ```ruby
122
+ translations = @client.translations 'project_id', limit: 4, page: 2, disable_references: 0 # => we passed three parameters here
123
+
124
+ translations.prev_page # => will load the previous page while preserving the `limit` and `disable_references` params
107
125
  ```
108
126
 
109
127
  ## Available Resources
@@ -5,29 +5,81 @@ module Lokalise
5
5
  extend Lokalise::Utils::AttributeHelpers
6
6
  include Lokalise::Utils::AttributeHelpers
7
7
 
8
- attr_reader :raw_data, :total_pages, :total_results, :results_per_page, :current_page, :collection,
9
- :project_id, :team_id
8
+ attr_reader :total_pages, :total_results, :results_per_page, :current_page, :collection,
9
+ :project_id, :team_id, :request_params, :client, :ids
10
10
 
11
- def initialize(response)
11
+ # Initializes a new collection based on the response
12
+ #
13
+ # @param response [Hash]
14
+ # @param params [Hash]
15
+ # @param ids [Array, Integer, String]
16
+ # @return [Lokalise::Collections::Base]
17
+ def initialize(response, params = {}, *ids)
12
18
  produce_collection_for response
13
- @total_results = response['x-pagination-total-count'].to_i
14
- @total_pages = response['x-pagination-page-count'].to_i
15
- @results_per_page = response['x-pagination-limit'].to_i
16
- @current_page = response['x-pagination-page'].to_i
19
+ populate_pagination_data_for response
17
20
  # Project and team id may not be present in some cases
18
21
  @project_id = response['content']['project_id']
19
22
  @team_id = response['content']['team_id']
23
+ @request_params = params
24
+ @client = response['client']
25
+ @ids = ids
20
26
  end
21
27
 
22
28
  class << self
23
29
  # Performs a batch query fetching multiple records
24
30
  def all(client, params = {}, *ids)
25
- new get(endpoint(*ids), client, params)
31
+ new get(endpoint(*ids), client, params),
32
+ params, *ids
26
33
  end
27
34
  end
28
35
 
36
+ # @return [Boolean]
37
+ def next_page?
38
+ @current_page.positive? && @current_page < @total_pages
39
+ end
40
+
41
+ # @return [Boolean]
42
+ def last_page?
43
+ !next_page?
44
+ end
45
+
46
+ # @return [Boolean]
47
+ def prev_page?
48
+ @current_page > 1
49
+ end
50
+
51
+ # @return [Boolean]
52
+ def first_page?
53
+ !prev_page?
54
+ end
55
+
56
+ def next_page
57
+ return nil if last_page?
58
+
59
+ fetch_page @current_page + 1
60
+ end
61
+
62
+ def prev_page
63
+ return nil if first_page?
64
+
65
+ fetch_page @current_page - 1
66
+ end
67
+
29
68
  private
30
69
 
70
+ def populate_pagination_data_for(response)
71
+ @total_results = response['x-pagination-total-count'].to_i
72
+ @total_pages = response['x-pagination-page-count'].to_i
73
+ @results_per_page = response['x-pagination-limit'].to_i
74
+ @current_page = response['x-pagination-page'].to_i
75
+ end
76
+
77
+ def fetch_page(page_num)
78
+ self.class.all @client,
79
+ @request_params.merge(page: page_num),
80
+ *@ids
81
+ end
82
+
31
83
  # Dynamically produces collection of resources based on the given response
32
84
  # Collection example: `{ "content": {"comments": [ ... ]} }`
33
85
  def produce_collection_for(response)
@@ -39,7 +39,7 @@ module Lokalise
39
39
  private
40
40
 
41
41
  def prepare(path)
42
- path.gsub /\/\//, '/'
42
+ path.gsub %r{//}, '/'
43
43
  end
44
44
 
45
45
  def respond_with(response, client)
@@ -47,8 +47,9 @@ module Lokalise
47
47
  respond_with_error(response.status, body) if body.respond_to?(:has_key?) && body.key?('error')
48
48
  response.
49
49
  headers.
50
+ to_h.
50
51
  keep_if { |k, _v| PAGINATION_HEADERS.include?(k) }.
51
- merge(content: body, client: client)
52
+ merge('content' => body, 'client' => client)
52
53
  end
53
54
 
54
55
  def respond_with_error(code, body)
@@ -7,6 +7,10 @@ module Lokalise
7
7
 
8
8
  attr_reader :raw_data, :project_id, :client
9
9
 
10
+ # Initializes a new resource based on the response
11
+ #
12
+ # @param response [Hash]
13
+ # @return [Lokalise::Resources::Base]
10
14
  def initialize(response)
11
15
  populate_attributes_for response['content']
12
16
 
@@ -42,7 +46,7 @@ module Lokalise
42
46
  client,
43
47
  body_from(params, object_key))
44
48
 
45
- object_from response
49
+ object_from response, params, endpoint_ids
46
50
  end
47
51
 
48
52
  # Updates one or multiple records
@@ -51,7 +55,7 @@ module Lokalise
51
55
  client,
52
56
  body_from(params, object_key))
53
57
 
54
- object_from response
58
+ object_from response, params, endpoint_ids
55
59
  end
56
60
 
57
61
  # Destroys records by given ids
@@ -83,12 +87,14 @@ module Lokalise
83
87
  end
84
88
 
85
89
  # Instantiates a new resource or collection based on the given response
86
- def object_from(response)
90
+ def object_from(response, params, endpoint_ids)
87
91
  model_class = name.base_class_name
88
92
  data_key_plural = data_key_for model_class, true
89
93
 
90
94
  if response['content'].key? data_key_plural
91
- Module.const_get("Lokalise::Collections::#{model_class}").new response
95
+ Module.const_get("Lokalise::Collections::#{model_class}").new response,
96
+ params,
97
+ endpoint_ids
92
98
  else
93
99
  new response
94
100
  end
@@ -1,3 +1,3 @@
1
1
  module Lokalise
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
@@ -22,6 +22,14 @@ RSpec.describe Lokalise::Client do
22
22
  expect(comments.results_per_page).to eq(1)
23
23
  expect(comments.current_page).to eq(2)
24
24
  expect(comments.collection.first.comment).to eq('demo comment')
25
+
26
+ expect(comments.next_page?).to eq(true)
27
+ expect(comments.last_page?).to eq(false)
28
+ expect(comments.prev_page?).to eq(true)
29
+ expect(comments.first_page?).to eq(false)
30
+
31
+ expect(comments.ids).to include(project_id, key_id)
32
+ expect(comments.client).to be_an_instance_of(Lokalise::Client)
25
33
  end
26
34
  end
27
35
 
@@ -22,6 +22,11 @@ RSpec.describe Lokalise::Client do
22
22
  expect(contributors.total_pages).to eq(2)
23
23
  expect(contributors.results_per_page).to eq(1)
24
24
  expect(contributors.current_page).to eq(2)
25
+
26
+ expect(contributors.next_page?).to eq(false)
27
+ expect(contributors.last_page?).to eq(true)
28
+ expect(contributors.prev_page?).to eq(true)
29
+ expect(contributors.first_page?).to eq(false)
25
30
  end
26
31
  end
27
32
 
@@ -24,6 +24,9 @@ RSpec.describe Lokalise::Client do
24
24
  expect(files.total_pages).to eq(1)
25
25
  expect(files.results_per_page).to eq(1)
26
26
  expect(files.current_page).to eq(1)
27
+
28
+ expect(files.next_page).to be_nil
29
+ expect(files.prev_page).to be_nil
27
30
  end
28
31
  end
29
32
 
@@ -22,6 +22,11 @@ RSpec.describe Lokalise::Client do
22
22
  expect(keys.total_pages).to eq(1)
23
23
  expect(keys.results_per_page).to eq(1)
24
24
  expect(keys.current_page).to eq(1)
25
+
26
+ expect(keys.next_page?).to eq(false)
27
+ expect(keys.last_page?).to eq(true)
28
+ expect(keys.prev_page?).to eq(false)
29
+ expect(keys.first_page?).to eq(true)
25
30
  end
26
31
  end
27
32
 
@@ -85,10 +90,14 @@ RSpec.describe Lokalise::Client do
85
90
  tags: %w[bulk upd]
86
91
  }
87
92
  ]
88
- end.collection
93
+ end
94
+
95
+ # `update_keys` returns a collection but it should not be paginated
96
+ expect(keys.next_page?).to eq(false)
97
+ expect(keys.prev_page?).to eq(false)
89
98
 
90
- first_key = keys.first
91
- second_key = keys[1]
99
+ first_key = keys.collection.first
100
+ second_key = keys.collection[1]
92
101
 
93
102
  expect(first_key.key_id).to eq(key_id)
94
103
  expect(first_key.key_name['ios']).to eq('test')
@@ -22,6 +22,11 @@ RSpec.describe Lokalise::Client do
22
22
  expect(projects.total_pages).to eq(5)
23
23
  expect(projects.results_per_page).to eq(1)
24
24
  expect(projects.current_page).to eq(2)
25
+
26
+ expect(projects.next_page?).to eq(true)
27
+ expect(projects.last_page?).to eq(false)
28
+ expect(projects.prev_page?).to eq(true)
29
+ expect(projects.first_page?).to eq(false)
25
30
  end
26
31
  end
27
32
 
@@ -28,6 +28,11 @@ RSpec.describe Lokalise::Client do
28
28
  expect(snapshots.total_pages).to eq(2)
29
29
  expect(snapshots.results_per_page).to eq(1)
30
30
  expect(snapshots.current_page).to eq(2)
31
+
32
+ expect(snapshots.next_page?).to eq(false)
33
+ expect(snapshots.last_page?).to eq(true)
34
+ expect(snapshots.prev_page?).to eq(true)
35
+ expect(snapshots.first_page?).to eq(false)
31
36
  end
32
37
  end
33
38
 
@@ -13,7 +13,7 @@ RSpec.describe Lokalise::Client do
13
13
 
14
14
  it 'should support pagination' do
15
15
  translations = VCR.use_cassette('all_translations_pagination') do
16
- test_client.translations project_id, limit: 4, page: 2
16
+ test_client.translations project_id, limit: 4, page: 2, disable_references: 0
17
17
  end
18
18
 
19
19
  expect(translations.collection.count).to eq(4)
@@ -21,6 +21,37 @@ RSpec.describe Lokalise::Client do
21
21
  expect(translations.total_pages).to eq(3)
22
22
  expect(translations.results_per_page).to eq(4)
23
23
  expect(translations.current_page).to eq(2)
24
+ expect(translations.request_params[:page]).to eq(2)
25
+ expect(translations.request_params[:disable_references]).to eq(0)
26
+ expect(translations.ids).to eq([project_id])
27
+
28
+ next_page_trans = VCR.use_cassette('translations_next_page') do
29
+ translations.next_page
30
+ end
31
+
32
+ expect(next_page_trans).to be_an_instance_of(Lokalise::Collections::Translation)
33
+ expect(next_page_trans.client).to be_an_instance_of(Lokalise::Client)
34
+ expect(next_page_trans.request_params[:page]).to eq(3)
35
+ expect(next_page_trans.request_params[:disable_references]).to eq(0)
36
+ expect(next_page_trans.total_results).to eq(9)
37
+ expect(next_page_trans.current_page).to eq(3)
38
+ expect(next_page_trans.ids).to eq([project_id])
39
+ expect(next_page_trans.next_page?).to eq(false)
40
+ expect(next_page_trans.prev_page?).to eq(true)
41
+
42
+ prev_page_trans = VCR.use_cassette('translations_prev_page') do
43
+ translations.prev_page
44
+ end
45
+
46
+ expect(prev_page_trans).to be_an_instance_of(Lokalise::Collections::Translation)
47
+ expect(prev_page_trans.client).to be_an_instance_of(Lokalise::Client)
48
+ expect(prev_page_trans.request_params[:page]).to eq(1)
49
+ expect(next_page_trans.request_params[:disable_references]).to eq(0)
50
+ expect(prev_page_trans.total_results).to eq(9)
51
+ expect(prev_page_trans.current_page).to eq(1)
52
+ expect(prev_page_trans.next_page?).to eq(true)
53
+ expect(prev_page_trans.prev_page?).to eq(false)
54
+ expect(prev_page_trans.ids).to eq([project_id])
24
55
  end
25
56
  end
26
57
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-lokalise-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Bodrov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-10 00:00:00.000000000 Z
11
+ date: 2018-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday