diff --git a/Gemfile.lock b/Gemfile.lock index 68f972e..096655d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - codifligne (0.1.7) + codifligne (0.1.14) nokogiri (>= 1.8.5) GEM diff --git a/lib/codifligne/common/operator.rb b/lib/codifligne/common/operator.rb index 0d723ba..679b59b 100644 --- a/lib/codifligne/common/operator.rb +++ b/lib/codifligne/common/operator.rb @@ -3,8 +3,10 @@ class Operator < Base attr_accessor :name, :stif_id, :xml def lines - client = Codifligne::API.new - client.lines(operator_name: self.name) + @lines ||= begin + client = Codifligne::API.new + client.lines(operator_name: self.name) + end end end end diff --git a/lib/codifligne/v2/api.rb b/lib/codifligne/v2/api.rb index a0b7e4c..ac8a2b1 100644 --- a/lib/codifligne/v2/api.rb +++ b/lib/codifligne/v2/api.rb @@ -97,13 +97,23 @@ def parse_address(node) end def operators(params = {}) + if params.empty? + # depending on the params, we may miss certain attributes on the lines + # so we only read them from the doc on the global endpoint + lines_per_operator = Hash.new { |h, k| h[k] = [] } + lines(params).each do |line| + lines_per_operator[line.operator_ref] << line + end + else + lines_per_operator = {} + end + get_doc(params).css('Operator').map do |operator| default_contact = parse_contact operator.css('ContactDetails').first private_contact = parse_contact operator.css('PrivateContactDetails').first customer_service_contact = parse_contact operator.css('CustomerServiceContactDetails').first address = parse_address operator.css('Address').first - V2::Operator.new({ name: operator.css('Name').first.content.strip, stif_id: operator.attribute('id').to_s.strip, @@ -111,6 +121,7 @@ def operators(params = {}) private_contact: private_contact, customer_service_contact: customer_service_contact, address: address, + lines: lines_per_operator[operator.attribute('id').to_s], xml: operator.to_xml }) end.to_a end diff --git a/lib/codifligne/version.rb b/lib/codifligne/version.rb index 66f9484..a165106 100644 --- a/lib/codifligne/version.rb +++ b/lib/codifligne/version.rb @@ -1,3 +1,3 @@ module Codifligne - VERSION = "0.1.13" + VERSION = "0.1.14" end diff --git a/spec/codifligne/codifligne_v2_spec.rb b/spec/codifligne/codifligne_v2_spec.rb index 1fb180c..577f6d9 100644 --- a/spec/codifligne/codifligne_v2_spec.rb +++ b/spec/codifligne/codifligne_v2_spec.rb @@ -141,4 +141,12 @@ expect(operator.lines.count).to equal(403) end + it 'should retrieve lines from the doc when available' do + xml = File.new(fixture_path + '/v2/index.xml') + stub_request(:get, api_index_url).to_return(body: xml) + + expect(client).to receive(:api_request).once.and_call_original + operator = client.operators.find { |o| o.name == 'RATP' } + expect(operator.lines.count).to equal(403) + end end