diff --git a/.gitignore b/.gitignore index 74c0ca53c..924db5e51 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ clients/ruby/git_push.sh clients/ruby/openapitools.json clients/ruby/*.gemspec clients/ruby/spec/*.rb +!clients/ruby/spec/response_spec.rb clients/ruby/spec/api/* clients/ruby/spec/models/ clients/ruby/docs diff --git a/clients/ruby/lib/phrase/response.rb b/clients/ruby/lib/phrase/response.rb index c1c2b1b7d..8ed9eb9e9 100644 --- a/clients/ruby/lib/phrase/response.rb +++ b/clients/ruby/lib/phrase/response.rb @@ -10,8 +10,8 @@ def initialize(data, headers) link_headers = headers["link"] if link_headers @paginated = true - parsed_links = LinkHeaderParser.parse(link_headers, base: 'https://api.phrase.com').group_by_relation_type - next_page_link = parsed_links[:next]&.first + parsed_links = LinkHeaderParser.parse(link_headers, base_uri: 'https://api.phrase.com').group_by_relation_type + next_page_link = parsed_links["next"]&.first if next_page_link @next_page = CGI.parse(URI.parse(next_page_link.target_uri).query)["page"]&.first&.to_i end diff --git a/clients/ruby/spec/response_spec.rb b/clients/ruby/spec/response_spec.rb new file mode 100644 index 000000000..f091fa278 --- /dev/null +++ b/clients/ruby/spec/response_spec.rb @@ -0,0 +1,53 @@ +require 'spec_helper' + +describe Phrase::Response do + context 'without a link header' do + it 'is not paginated' do + response = Phrase::Response.new({ 'foo' => 'bar' }, {}) + + expect(response.paginated?).to eq(false) + expect(response.next_page?).to eq(false) + expect(response.next_page).to be_nil + end + end + + context 'with a link header that has a next page' do + let(:headers) do + { + 'link' => '; rel="next", ' \ + '; rel="last"' + } + end + + it 'parses the next page number without raising' do + response = Phrase::Response.new({ 'foo' => 'bar' }, headers) + + expect(response.paginated?).to eq(true) + expect(response.next_page?).to eq(true) + expect(response.next_page).to eq(2) + end + end + + context 'with a link header that has no next page' do + let(:headers) do + { + 'link' => '; rel="first", ' \ + '; rel="last"' + } + end + + it 'is paginated but has no next page' do + response = Phrase::Response.new({ 'foo' => 'bar' }, headers) + + expect(response.paginated?).to eq(true) + expect(response.next_page?).to eq(false) + expect(response.next_page).to be_nil + end + end + + it 'delegates missing methods to the underlying data' do + response = Phrase::Response.new({ 'foo' => 'bar' }, {}) + + expect(response['foo']).to eq('bar') + end +end diff --git a/openapi-generator/templates/ruby-client/gemspec.mustache b/openapi-generator/templates/ruby-client/gemspec.mustache index c5dd6d398..b73e212f7 100644 --- a/openapi-generator/templates/ruby-client/gemspec.mustache +++ b/openapi-generator/templates/ruby-client/gemspec.mustache @@ -32,7 +32,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'typhoeus', '>= 1.0.1' {{/isFaraday}} s.add_runtime_dependency 'json', '>= 2.1.0' - s.add_runtime_dependency 'link-header-parser' + s.add_runtime_dependency 'link-header-parser', '>= 7.0' s.add_development_dependency 'rspec', '>= 3.6.0'