Skip to content

Commit b7dd055

Browse files
committed
Version 9.1.0
1 parent 2d78458 commit b7dd055

File tree

18 files changed

+536
-40
lines changed

18 files changed

+536
-40
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ measurement/*
1010
pkg/*
1111
.DS_Store
1212
.env
13-
demo.rb
13+
demo.rb
14+
.ruby-lsp

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Layout/LineLength:
2020
Exclude:
2121
- ./*.gemspec
2222

23+
Metrics/ClassLength:
24+
CountComments: false
25+
Max: 120
26+
2327
Metrics/MethodLength:
2428
CountComments: false
2529
Max: 10

docs/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ end
2727
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
2828
# and associated library.
2929
install_if -> { RUBY_PLATFORM =~ /mingw|mswin|java/ } do
30-
gem 'tzinfo', '~> 1.2'
30+
gem 'tzinfo', '~> 2.0'
3131
gem 'tzinfo-data'
3232
end
3333

docs/additional_info/changelog.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
# Changelog
22

3+
## 9.1.0 (15-May-2024)
4+
5+
* Add support for [cursor pagination](https://lokalise.github.io/ruby-lokalise-api/api/getting-started#cursor-pagination) for List keys and List translation endpoints:
6+
7+
```ruby
8+
cursor_pagination_params = {
9+
pagination: 'cursor',
10+
cursor: 'eyIxIjozMTk3ODIzNzJ9', # The starting cursor. Optional, string
11+
limit: 2 # The number of items to fetch. Optional, default is 100
12+
}
13+
14+
keys = @client.keys '123abcdef.01', cursor_pagination_params
15+
16+
keys.next_cursor? # => true
17+
keys.next_cursor # => 'eyIxIjozMTk3ODIzNzV9'
18+
19+
# Request keys from the next cursor (returns `nil` if the next cursor is not available):
20+
keys_next_cursor = keys.load_next_cursor
21+
```
22+
323
## 9.0.1 (13-Mar-2024)
424

525
* Handle cases when server responds with non-JSON

docs/api/getting-started.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,26 @@ first_translation = prev_page_translations.first # get first translation from th
138138
first_translation.translation # => 'Translation text'
139139
```
140140

141+
### Cursor pagination
142+
143+
The [List Keys](https://developers.lokalise.com/reference/list-all-keys) and [List Translations](https://developers.lokalise.com/reference/list-all-translations) endpoints support cursor pagination, which is recommended for its faster performance compared to traditional "offset" pagination. By default, "offset" pagination is used, so you must explicitly set `pagination` to `"cursor"` to use cursor pagination.
144+
145+
```ruby
146+
cursor_pagination_params = {
147+
pagination: 'cursor',
148+
cursor: 'eyIxIjozMTk3ODIzNzJ9', # The starting cursor. Optional, string
149+
limit: 2 # The number of items to fetch. Optional, default is 100
150+
}
151+
152+
keys = @client.keys '123abcdef.01', cursor_pagination_params
153+
154+
keys.next_cursor? # => true
155+
keys.next_cursor # => 'eyIxIjozMTk3ODIzNzV9'
156+
157+
# Request keys from the next cursor (returns `nil` if the next cursor is not available):
158+
keys_next_cursor = keys.load_next_cursor
159+
```
160+
141161
## Branching
142162

143163
If you are using [project branching feature](https://docs.lokalise.com/en/articles/3391861-project-branching), simply add branch name separated by semicolon to your project ID in any endpoint to access the branch. For example, in order to access `new-feature` branch for the project with an id `123abcdef.01`:

docs/api/keys.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
@client.keys(project_id, params = {}) # Input:
99
## project_id (string, required)
1010
## params (hash)
11-
### :page and :limit
11+
### pagination and cursor-related params
1212
# Output:
1313
## Collection of keys available in the given project
1414
```
1515

16+
**This endpoint also supports cursor pagination which is now a recommended approach, especially for fetching large amounts of data. Please [learn more in the Pagination docs](https://lokalise.github.io/ruby-lokalise-api/api/getting-started#cursor-pagination).**
17+
1618
For example:
1719

1820
```ruby

docs/api/translations.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
## project_id (string, required)
1010
## params (hash)
1111
### Find full list in the docs
12-
### :page and :limit
1312
# Output:
1413
## Collection of translations for the project
1514
```
1615

16+
**This endpoint also supports cursor pagination which is now a recommended approach, especially for fetching large amounts of data. Please [learn more in the Pagination docs](https://lokalise.github.io/ruby-lokalise-api/api/getting-started#cursor-pagination).**
17+
1718
For example:
1819

1920
```ruby

lib/ruby_lokalise_api/collections/base.rb

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Base
1818
def_delegators :collection, :[], :last, :each
1919

2020
attr_reader :total_pages, :total_results, :results_per_page, :current_page,
21-
:collection
21+
:collection, :next_cursor
2222

2323
def initialize(response)
2424
@self_endpoint = response.endpoint
@@ -32,9 +32,25 @@ def initialize(response)
3232
def next_page
3333
return nil if last_page?
3434

35+
override_params = { page: current_page + 1 }
36+
3537
self.class.new(
3638
reinit_endpoint(
37-
override_req_params: { page: current_page + 1 }
39+
override_req_params: override_params
40+
).do_get
41+
)
42+
end
43+
44+
# Tries to fetch the next cursor for paginated collection
45+
# Returns a new collection or nil if the next cursor is not available
46+
def load_next_cursor
47+
return nil unless next_cursor?
48+
49+
override_params = { cursor: next_cursor }
50+
51+
self.class.new(
52+
reinit_endpoint(
53+
override_req_params: override_params
3854
).do_get
3955
)
4056
end
@@ -75,6 +91,12 @@ def first_page?
7591
!prev_page?
7692
end
7793

94+
# Checks whether the next cursor is available
95+
# @return [Boolean]
96+
def next_cursor?
97+
!next_cursor.nil? && next_cursor != ''
98+
end
99+
78100
private
79101

80102
# This method is utilized to recreate an endpoint for the current collection
@@ -89,14 +111,17 @@ def populate_common_attrs_from(response)
89111
instance_variable_set :"@#{attrib}", response.content[attrib]
90112
end
91113

92-
headers = response.headers
114+
extract_common_from_headers(response.headers)
115+
end
93116

117+
def extract_common_from_headers(headers)
94118
return unless headers.any?
95119

96-
@total_results = headers[:'x-pagination-total-count']
97-
@total_pages = headers[:'x-pagination-page-count']
98-
@results_per_page = headers[:'x-pagination-limit']
99-
@current_page = headers[:'x-pagination-page']
120+
@total_results = headers[:'x-pagination-total-count'].to_i
121+
@total_pages = headers[:'x-pagination-page-count'].to_i
122+
@results_per_page = headers[:'x-pagination-limit'].to_i
123+
@current_page = headers[:'x-pagination-page'].to_i
124+
@next_cursor = headers[:'x-pagination-next-cursor']
100125
end
101126

102127
def produce_collection_from(response)

lib/ruby_lokalise_api/response.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module RubyLokaliseApi
55
class Response
66
# Lokalise returns pagination info in special headers
77
PAGINATION_HEADERS = %w[x-pagination-total-count x-pagination-page-count x-pagination-limit
8-
x-pagination-page].freeze
8+
x-pagination-page x-pagination-next-cursor].freeze
99

1010
attr_reader :content, :endpoint, :headers
1111

@@ -43,8 +43,7 @@ def extract_headers_from(raw_headers)
4343
raw_headers.
4444
to_h.
4545
keep_if { |header, _value| pagination_headers.include?(header) }.
46-
transform_keys(&:to_sym).
47-
transform_values(&:to_i)
46+
transform_keys(&:to_sym)
4847
end
4948
end
5049
end

lib/ruby_lokalise_api/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module RubyLokaliseApi
4-
VERSION = '9.0.1'
4+
VERSION = '9.1.0'
55
end

0 commit comments

Comments
 (0)