Skip to content
This repository has been archived by the owner on Aug 25, 2023. It is now read-only.

Commit

Permalink
Save n+1 queries to fetch operators lines
Browse files Browse the repository at this point in the history
  • Loading branch information
zog committed May 29, 2019
1 parent 62e9266 commit 483bbac
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
codifligne (0.1.7)
codifligne (0.1.14)
nokogiri (>= 1.8.5)

GEM
Expand Down
6 changes: 4 additions & 2 deletions lib/codifligne/common/operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 12 additions & 1 deletion lib/codifligne/v2/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,31 @@ 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,
default_contact: default_contact,
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
Expand Down
2 changes: 1 addition & 1 deletion lib/codifligne/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Codifligne
VERSION = "0.1.13"
VERSION = "0.1.14"
end
8 changes: 8 additions & 0 deletions spec/codifligne/codifligne_v2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 483bbac

Please sign in to comment.