Skip to content

Commit

Permalink
feat(referentiel_service): basic implementation pour call API
Browse files Browse the repository at this point in the history
  • Loading branch information
mfo committed Dec 23, 2024
1 parent 3a5a444 commit e1f59cb
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
43 changes: 43 additions & 0 deletions app/services/referentiel_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

class ReferentielService
def test
case type_de_champ.referentiel_adapter
when 'url'
ReferentielApiClient.new(type_de_champ:).valid?(type_de_champ.referentiel_test_data)
else
fail "not yet implemented: #{type_de_champ.referentiel_adapter}"
end
end

private

attr_reader :type_de_champ
def initialize(type_de_champ:)
@type_de_champ = type_de_champ
end

class ReferentielApiClient
attr_reader :type_de_champ
def initialize(type_de_champ:)
@type_de_champ = type_de_champ
end

def valid?(value)
case call(value)
when Dry::Monads[:result]::Success
true
else
false
end
end

def call(value) = API::Client.new.(url: build_url(value))

def build_url(value)
original_url = @type_de_champ.referentiel_url

original_url.gsub('{id}', value)
end
end
end
55 changes: 55 additions & 0 deletions spec/fixtures/cassettes/referentiel/test.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions spec/services/referentiel_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

RSpec.describe ReferentielService, type: :service do
describe '.test' do
let(:type_de_champ) do
build(:type_de_champ_referentiel, referentiel_url:, referentiel_test_data:, referentiel_adapter:)
end
let(:referentiel_adapter) { 'url' }
let(:referentiel_url) { "https://api.fr/{id}/" }
let(:referentiel_test_data) { "kthxbye" }

context 'when referentiel_adapter is url', vcr: 'referentiel/test' do
subject { described_class.new(type_de_champ:).test }

it { is_expected.to eq(true) }
end
end
end

0 comments on commit e1f59cb

Please sign in to comment.