lifen_fhir 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4f268ed9c624f69baa94cf0c7b9ad04cf5778ffc
4
- data.tar.gz: cfdeb52172b4385d3fce6a0a1f497931156669d2
3
+ metadata.gz: 883c936ea9075219b23c3cd2a999237d00096ab6
4
+ data.tar.gz: 62045d010b9ef604b067f17b319bcf57c712f176
5
5
  SHA512:
6
- metadata.gz: '003841d9ab0e8df08ae247d1c5d7c9b05c42e095af4c6c298778e1de768527f0f697679f95249a516e39240093cfb79d0aa3e1e8711d0ea59c012bce2b6185d3'
7
- data.tar.gz: f05094f969f7c04dd8b8b5ba7774c89c21c6e84870b44a291bcac42cf5a9e9c91c394389d733c662d5aa04306e381e29060fcbf0a5e663c610cd91f48299e007
6
+ metadata.gz: 6cc241e7aa995d4eeab7295ddf4ed87f305b91894e797be38f9e7eb1298b1571b33110e6b43d4ce9e988c0c918b3f3154510bce69ab18c6cc6fc401d2732cab0
7
+ data.tar.gz: b13b9722a50eab03eef4415dce10651f663b88cd9840dc677d4ee035d8129e912cb41fbb86dd67dde84c085e1907626739be02069cbac44751d87f15fb819c38
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ 0.6.0
2
+ -----
3
+
4
+ - Internal errors provides the client with a context that can be passed on issue tracker
5
+
6
+ 0.5.1
7
+ -----
8
+
9
+ - Fix missing fields issue in find_by_uuid for CommunicationRequest
10
+
1
11
  0.5.0
2
12
  -----
3
13
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lifen_fhir (0.5.0)
4
+ lifen_fhir (0.6.0)
5
5
  faraday (>= 0.9)
6
6
  inflecto
7
7
  virtus (>= 1.0)
data/README.md CHANGED
@@ -40,9 +40,21 @@ Optionnal configuration:
40
40
  - `proxy_url`: route all your requests via a proxy
41
41
  - `logger`: log all requests to a custom logger (on Rails, `Rails.logger` is selected automatically)
42
42
 
43
- ### Internal
43
+ ### Managing errors
44
44
 
45
- #### Managing a Binary
45
+ `LifenFhir::Error` provides a `context` that can be passed to your issue manager in order to have a better understanding of what is going wrong when an error happens. Here is an example with Sentry:
46
+
47
+ ```ruby
48
+ begin
49
+ # some code using the gem
50
+ rescue LifenFhir::Error => e
51
+ Raven.capture_exception(e)
52
+ Raven.extra_context(lifen: e.context)
53
+ end
54
+ ```
55
+
56
+
57
+ ### Managing a Binary
46
58
 
47
59
  ```ruby
48
60
  binary = LifenFhir::Binary.new(uuid: "b7c7dae671b93e951ce6a4f530736276")
@@ -85,7 +97,7 @@ communication_request_request.send
85
97
 
86
98
  ```
87
99
 
88
- #### Custom Channels management
100
+ ### Custom Channels management
89
101
 
90
102
  ```ruby
91
103
  recipient = LifenFhir::Practitioner.new(uuid: "valid-user-uuid")
@@ -97,7 +109,7 @@ channel = recipient.create_address(type: "address", lines: ["39 rue Aboukir"], c
97
109
  channel = recipient.create_telecom(type: "telecom", system: "fax", value: "+33102030405")
98
110
  ```
99
111
 
100
- #### Managing Patients
112
+ ### Managing Patients
101
113
 
102
114
  ```ruby
103
115
  # To create a new patient
@@ -126,6 +138,10 @@ Once the new version is validated, the deployment follows those steps :
126
138
  6. publish the gem using `gem publish lifen_fhir-X.X.X.gem`
127
139
  7. run `bundle update lifen` in related projects :)
128
140
 
141
+ ## Contributing
142
+
143
+ TBD
144
+
129
145
  ## License
130
146
 
131
147
  The MIT License (MIT)
@@ -9,27 +9,21 @@ module LifenFhir
9
9
 
10
10
  case response.status
11
11
  when 400
12
- raise Error, "Error 400, Unknown error, #{response_error(response, params)}"
12
+ raise_error("400 Error, Unknown error", response, params)
13
13
  when 401
14
- raise InvalidSecretTokenError, "Error 401, Invalid app bearer, #{response_error(response, params)}"
15
- when 403
16
- raise UserAlreadyExistingError, "Error 403, User already existing, #{response_error(response, params)}"
14
+ raise_error("401 Error, Invalid app bearer", response, params)
17
15
  when 404
18
- raise Error, "Error 404, Page not found, #{response_error(response, params)}"
16
+ raise_error("404 Error, Record not found", response, params)
19
17
  when 422
20
18
  json = JSON.parse response.body
21
19
 
22
20
  diagnostic = json["issue"][0]["diagnostics"]
23
21
 
24
- raise Error, "Error 422, Unprocessable Entity, Diagnostic: '#{diagnostic}', #{response_error(response, params)}"
22
+ raise_error("422 Error, Unprocessable Entity, Diagnostic: '#{diagnostic}'", response, params)
25
23
  end
26
24
 
27
25
  end
28
26
 
29
- def response_error(response, params)
30
- "App Client, #{super(response, params)}"
31
- end
32
-
33
27
  def bearer
34
28
  LifenFhir.configuration.application_access_token
35
29
  end
@@ -41,12 +41,7 @@ module LifenFhir
41
41
 
42
42
  def handle_errors(response, params)
43
43
  if response.status == 500
44
-
45
- json = JSON.parse response.body
46
-
47
- trace_id = json.fetch("X-B3-TraceId", "unknown")
48
-
49
- raise Error, "Error 500, Internal server error (trace ID: #{trace_id}), #{response_error(response, params)}"
44
+ raise_error("500 Error, Internal server error", response, params)
50
45
  end
51
46
  end
52
47
 
@@ -88,6 +83,12 @@ module LifenFhir
88
83
  LifenFhir.configuration.proxy_url
89
84
  end
90
85
 
86
+ def raise_error(message, response, params)
87
+ message += " -- #{response.env.method.upcase} '#{response.env.url}'"
88
+
89
+ raise Error.new(message: message, response: response, params: params, client: "app", token: bearer)
90
+ end
91
+
91
92
  def logger
92
93
  LifenFhir.configuration.logger
93
94
  end
@@ -95,28 +96,18 @@ module LifenFhir
95
96
  def before_request
96
97
  end
97
98
 
98
- def response_error(response, params)
99
- params[:payload] = "filtered" if params.is_a?(Hash) and params.has_key? :payload
99
+ def use_and_remove_accept(params)
100
+ return params.delete :accept if params.is_a?(Hash) and params.has_key? :accept
100
101
 
101
- "#{response.env.method.upcase} '#{response.env.url}' with params '#{params.inspect}' and bearer '#{trucanted_bearer}'"
102
+ "application/json"
102
103
  end
103
104
 
104
- def trucanted_bearer
105
- if m = /^(.{24})(.*)$/.match(bearer)
106
- "#{m[1]}#{"*" * m[2].length}"
107
- else
108
- bearer
109
- end
105
+ def before_request
110
106
  end
111
107
 
112
108
  def bearer
113
109
  raise "A bearer method must be defined"
114
110
  end
115
111
 
116
- def use_and_remove_accept(params)
117
- return params.delete :accept if params.is_a?(Hash) and params.has_key? :accept
118
-
119
- "application/json"
120
- end
121
112
  end
122
113
  end
@@ -38,14 +38,22 @@ module LifenFhir
38
38
 
39
39
  self.status = json.fetch("status") { "unknown" }
40
40
 
41
- self.binary = Binary.new.attributes_from_json(Array(json["payload"]).first)
41
+ if has_payload?(json)
42
+ self.binary = Binary.new.attributes_from_json(Array(json["payload"]).first)
43
+ end
42
44
 
43
- self.category = Category.new.attributes_from_json(Array(json["category"]).first)
45
+ if has_category?(json)
46
+ self.category = Category.new.attributes_from_json(Array(json["category"]).first)
47
+ end
44
48
 
45
- self.sender = Practitioner.new.attributes_from_json(json["sender"])
49
+ if has_sender?(json)
50
+ self.sender = Practitioner.new.attributes_from_json(json["sender"])
51
+ end
46
52
 
47
- self.recipients = Array(json["recipient"]).map do |recipient_json|
48
- Practitioner.new.attributes_from_json(recipient_json)
53
+ if has_recipient?(json)
54
+ self.recipients = Array(json["recipient"]).map do |recipient_json|
55
+ Practitioner.new.attributes_from_json(recipient_json)
56
+ end
49
57
  end
50
58
  end
51
59
 
@@ -85,5 +93,20 @@ module LifenFhir
85
93
  end
86
94
  end
87
95
 
96
+ def has_category?(json)
97
+ json.key?("category")
98
+ end
99
+
100
+ def has_sender?(json)
101
+ json.key?("sender")
102
+ end
103
+
104
+ def has_recipient?(json)
105
+ json.key?("recipient")
106
+ end
107
+
108
+ def has_payload?(json)
109
+ json.key?("payload")
110
+ end
88
111
  end
89
112
  end
@@ -1,27 +1,56 @@
1
1
  module LifenFhir
2
2
 
3
3
  class Error < StandardError
4
- end
5
4
 
6
- class UnauthorizedError < Error
7
- end
5
+ attr_accessor :response, :params
8
6
 
9
- class InvalidCredentialsError < Error
10
- end
7
+ def initialize(message: , response: nil, params: {}, client: "app", token: nil)
8
+ self.response = response
9
+ self.params = params
11
10
 
12
- class InvalidParamsError < Error
13
- end
11
+ @client = client
12
+ @token = token
14
13
 
15
- class InvalidSecretTokenError < Error
16
- end
14
+ super(message)
15
+ end
17
16
 
18
- class InvalidTokenError < Error
19
- end
17
+ def context
18
+ return {} if response.nil?
20
19
 
21
- class UserAlreadyExistingError < Error
22
- end
20
+ {
21
+ url: response.env.url.to_s,
22
+ method: response.env.method.to_s,
23
+ token: trucanted_token,
24
+ payload_sent: filtered_params,
25
+ trace_id: extracted_trace_id,
26
+ payload_received: response.body,
27
+ client: @client
28
+ }
29
+
30
+ end
31
+
32
+ private
33
+
34
+ def extracted_trace_id
35
+ response.headers.fetch("X-B3-Traceid") { "unknown" }
36
+ end
37
+
38
+
39
+ def trucanted_token
40
+ if m = /^(.{24})(.*)$/.match(@token)
41
+ "#{m[1]}#{"*" * m[2].length}"
42
+ else
43
+ @token
44
+ end
45
+ end
46
+
47
+ def filtered_params
48
+ filtered_params = params
49
+
50
+ filtered_params[:payload] = "filtered" if filtered_params.is_a?(Hash) and filtered_params.has_key? :payload
23
51
 
24
- class CantRefreshTokenError < Error
52
+ filtered_params
53
+ end
25
54
  end
26
55
 
27
- end
56
+ end
@@ -1,3 +1,3 @@
1
1
  module LifenFhir
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
data/lib/lifen_fhir.rb CHANGED
@@ -22,7 +22,6 @@ module LifenFhir
22
22
  require 'lifen_fhir/logger'
23
23
  require 'lifen_fhir/error'
24
24
  require 'lifen_fhir/client'
25
- require 'lifen_fhir/user_authenticated_client'
26
25
  require 'lifen_fhir/app_authenticated_client'
27
26
  require 'lifen_fhir/configuration'
28
27
  require 'lifen_fhir/base'
@@ -0,0 +1,70 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://develop.lifen.fr/fhir/CommunicationRequest/f1924980-6685-40eb-b07d-51f317365ae5
6
+ body:
7
+ encoding: UTF-8
8
+ string: "{}"
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.12.1
12
+ Authorization:
13
+ - Bearer valid_application_access_token
14
+ Accept:
15
+ - application/json
16
+ Accept-Encoding:
17
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Server:
24
+ - Apache-Coyote/1.1
25
+ X-B3-Traceid:
26
+ - 6d110814fbb41d26
27
+ X-B3-Spanid:
28
+ - 6d110814fbb41d26
29
+ X-Content-Type-Options:
30
+ - nosniff
31
+ X-Xss-Protection:
32
+ - 1; mode=block
33
+ Cache-Control:
34
+ - no-cache, no-store, max-age=0, must-revalidate
35
+ Pragma:
36
+ - no-cache
37
+ Expires:
38
+ - '0'
39
+ X-Powered-By:
40
+ - HAPI FHIR 2.4 REST Server (FHIR Server; FHIR 3.0.1/DSTU3)
41
+ Location:
42
+ - http://rc.lifen.fr/fhir/CommunicationRequest/f1924980-6685-40eb-b07d-51f317365ae5
43
+ Content-Type:
44
+ - application/json+fhir;charset=UTF-8
45
+ Transfer-Encoding:
46
+ - chunked
47
+ Date:
48
+ - Wed, 07 Jun 2017 08:34:57 GMT
49
+ Connection:
50
+ - close
51
+ Access-Control-Allow-Credentials:
52
+ - 'true'
53
+ body:
54
+ encoding: UTF-8
55
+ string: |-
56
+ {
57
+ "resourceType": "CommunicationRequest",
58
+ "id": "f1924980-6685-40eb-b07d-51f317365ae5",
59
+ "status": "cancelled",
60
+ "payload": [
61
+ {
62
+ "contentReference": {
63
+ "reference": "Binary/11e746b1-6cd7-1483-96ef-0242ac110004"
64
+ }
65
+ }
66
+ ]
67
+ }
68
+ http_version:
69
+ recorded_at: Wed, 07 Jun 2017 08:34:16 GMT
70
+ recorded_with: VCR 3.0.3
@@ -0,0 +1,63 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://develop.lifen.fr/fhir/CommunicationRequest/f1924980-6685-40eb-b07d-51f317365ae5
6
+ body:
7
+ encoding: UTF-8
8
+ string: "{}"
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.12.1
12
+ Authorization:
13
+ - Bearer valid_application_access_token
14
+ Accept:
15
+ - application/json
16
+ Accept-Encoding:
17
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Server:
24
+ - Apache-Coyote/1.1
25
+ X-B3-Traceid:
26
+ - 6d110814fbb41d26
27
+ X-B3-Spanid:
28
+ - 6d110814fbb41d26
29
+ X-Content-Type-Options:
30
+ - nosniff
31
+ X-Xss-Protection:
32
+ - 1; mode=block
33
+ Cache-Control:
34
+ - no-cache, no-store, max-age=0, must-revalidate
35
+ Pragma:
36
+ - no-cache
37
+ Expires:
38
+ - '0'
39
+ X-Powered-By:
40
+ - HAPI FHIR 2.4 REST Server (FHIR Server; FHIR 3.0.1/DSTU3)
41
+ Location:
42
+ - http://rc.lifen.fr/fhir/CommunicationRequest/f1924980-6685-40eb-b07d-51f317365ae5
43
+ Content-Type:
44
+ - application/json+fhir;charset=UTF-8
45
+ Transfer-Encoding:
46
+ - chunked
47
+ Date:
48
+ - Wed, 07 Jun 2017 08:34:57 GMT
49
+ Connection:
50
+ - close
51
+ Access-Control-Allow-Credentials:
52
+ - 'true'
53
+ body:
54
+ encoding: UTF-8
55
+ string: |-
56
+ {
57
+ "resourceType": "CommunicationRequest",
58
+ "id": "f1924980-6685-40eb-b07d-51f317365ae5",
59
+ "status": "cancelled"
60
+ }
61
+ http_version:
62
+ recorded_at: Wed, 07 Jun 2017 08:34:16 GMT
63
+ recorded_with: VCR 3.0.3
@@ -0,0 +1,62 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://develop.lifen.fr/fhir/CommunicationRequest/valid_uuid
6
+ body:
7
+ encoding: UTF-8
8
+ string: "{}"
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.12.1
12
+ Authorization:
13
+ - Bearer invalid_application_access_token
14
+ Accept:
15
+ - application/json
16
+ Accept-Encoding:
17
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
18
+ response:
19
+ status:
20
+ code: 401
21
+ message: Unauthorized
22
+ headers:
23
+ Server:
24
+ - Apache-Coyote/1.1
25
+ X-Content-Type-Options:
26
+ - nosniff
27
+ X-Xss-Protection:
28
+ - 1; mode=block
29
+ Cache-Control:
30
+ - no-cache, no-store, max-age=0, must-revalidate
31
+ Pragma:
32
+ - no-cache
33
+ Expires:
34
+ - '0'
35
+ X-Powered-By:
36
+ - HAPI FHIR 2.4 REST Server (FHIR Server; FHIR 3.0.1/DSTU3)
37
+ Content-Type:
38
+ - application/json+fhir;charset=UTF-8
39
+ Transfer-Encoding:
40
+ - chunked
41
+ Date:
42
+ - Mon, 05 Jun 2017 06:46:53 GMT
43
+ Connection:
44
+ - close
45
+ Access-Control-Allow-Credentials:
46
+ - 'true'
47
+ body:
48
+ encoding: UTF-8
49
+ string: |-
50
+ {
51
+ "resourceType": "OperationOutcome",
52
+ "issue": [
53
+ {
54
+ "severity": "error",
55
+ "code": "login",
56
+ "diagnostics": "Invalid access token"
57
+ }
58
+ ]
59
+ }
60
+ http_version:
61
+ recorded_at: Mon, 05 Jun 2017 06:47:02 GMT
62
+ recorded_with: VCR 3.0.3
@@ -151,6 +151,33 @@ describe LifenFhir::CommunicationRequest do
151
151
  expect(@communication_request.status).to eq "cancelled"
152
152
  end
153
153
 
154
+ context ':when only status, id and binary' do
155
+
156
+ it 'works' do
157
+ VCR.use_cassette "communication_request/find_by_uuid/valid_uuid_with_status_and_binary_only" do
158
+ @communication_request = LifenFhir::CommunicationRequest.find_by_uuid("f1924980-6685-40eb-b07d-51f317365ae5")
159
+ end
160
+
161
+ expect(@communication_request.status).to eq "cancelled"
162
+ expect(@communication_request.uuid).to eq "f1924980-6685-40eb-b07d-51f317365ae5"
163
+ expect(@communication_request.sender).to be_nil
164
+ expect(@communication_request.recipients.count).to eq(0)
165
+ expect(@communication_request.category.code).to eq("MEDICAL_REPORT")
166
+ end
167
+ end
168
+
169
+ context ':when only status and communication_request id' do
170
+
171
+ it 'works' do
172
+ VCR.use_cassette "communication_request/find_by_uuid/valid_uuid_with_status_only" do
173
+ @communication_request = LifenFhir::CommunicationRequest.find_by_uuid("f1924980-6685-40eb-b07d-51f317365ae5")
174
+ end
175
+
176
+ expect(@communication_request.status).to eq "cancelled"
177
+ expect(@communication_request.binary).to be_nil
178
+ end
179
+ end
180
+
154
181
  context 'with no defined status' do
155
182
  it 'works' do
156
183
  VCR.use_cassette "communication_request/find_by_uuid/valid_uuid_with_no_status" do
@@ -180,7 +207,7 @@ describe LifenFhir::CommunicationRequest do
180
207
  VCR.use_cassette "communication_request/find_by_uuid/invalid_uuid" do
181
208
  @communication_request = LifenFhir::CommunicationRequest.find_by_uuid("invalid_uuid")
182
209
  end
183
- }.to raise_error LifenFhir::Error, "Error 404, Page not found, App Client, GET 'https://develop.lifen.fr/fhir/CommunicationRequest/invalid_uuid' with params '{}' and bearer 'valid_application_access******'"
210
+ }.to raise_error LifenFhir::Error
184
211
  end
185
212
 
186
213
  end
@@ -0,0 +1,168 @@
1
+ require 'spec_helper'
2
+
3
+ describe LifenFhir::Error do
4
+
5
+ describe 'in real conditions' do
6
+
7
+ context '404' do
8
+
9
+ it 'extracts the correct context' do
10
+
11
+ begin
12
+ VCR.use_cassette "communication_request/find_by_uuid/invalid_uuid" do
13
+ communication_request = LifenFhir::CommunicationRequest.find_by_uuid("invalid_uuid")
14
+ end
15
+ rescue LifenFhir::Error => e
16
+
17
+ expect(e.to_s).to eq "404 Error, Record not found -- GET 'https://develop.lifen.fr/fhir/CommunicationRequest/invalid_uuid'"
18
+
19
+ current_context = e.context
20
+
21
+ expect(current_context[:client]).to eq "app"
22
+ expect(current_context[:url]).to eq "https://develop.lifen.fr/fhir/CommunicationRequest/invalid_uuid"
23
+ expect(current_context[:method]).to eq "get"
24
+ expect(current_context[:token]).to eq "valid_application_access******"
25
+ expect(current_context[:payload_sent]).to eq({})
26
+ expect(current_context[:trace_id]).to eq "unknown"
27
+ expect(current_context[:payload_received]).to include('"diagnostics": "Resource CommunicationRequest/invalid_uuid is not known"')
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+
34
+ context '400' do
35
+
36
+ let(:address) {
37
+ LifenFhir::Address.new(
38
+ lines: ["M et Mme Potter", "39 rue d'Aboukir"],
39
+ city: "paris",
40
+ postal_code: "75002",
41
+ country: "France"
42
+ )
43
+ }
44
+
45
+ let(:patient) {
46
+ patient = LifenFhir::Patient.new(
47
+ first_name: ["Pierre", "Henri"],
48
+ last_name: "Potter",
49
+ birth_date: Date.new(1974, 12, 25),
50
+ address: address
51
+ )
52
+ }
53
+
54
+ it 'extracts the correct context' do
55
+ patient.last_name = nil
56
+
57
+ begin
58
+ VCR.use_cassette("patient/create/invalid_attributes") do
59
+ patient.save
60
+ end
61
+ rescue LifenFhir::Error => e
62
+
63
+ expect(e.to_s).to eq "400 Error, Unknown error -- POST 'https://develop.lifen.fr/fhir/Patient'"
64
+
65
+ current_context = e.context
66
+
67
+ expect(current_context[:client]).to eq "app"
68
+ expect(current_context[:url]).to eq "https://develop.lifen.fr/fhir/Patient"
69
+ expect(current_context[:method]).to eq "post"
70
+ expect(current_context[:token]).to eq "valid_application_access******"
71
+ expect(current_context[:payload_sent]["birthDate"]).to eq("1974-12-25")
72
+ expect(current_context[:trace_id]).to eq "unknown"
73
+ expect(current_context[:payload_received]).to include('"diagnostics": "Patient\'s family name is mandatory."')
74
+ end
75
+ end
76
+
77
+ end
78
+
79
+ context '401' do
80
+
81
+ before do
82
+ LifenFhir.configure do |config|
83
+ config.application_access_token = "invalid_application_access_token"
84
+ end
85
+ end
86
+
87
+ it 'extracts the correct context' do
88
+ begin
89
+ VCR.use_cassette "errors/401" do
90
+ communication_request = LifenFhir::CommunicationRequest.find_by_uuid("valid_uuid")
91
+ end
92
+ rescue LifenFhir::Error => e
93
+
94
+ expect(e.to_s).to eq "401 Error, Invalid app bearer -- GET 'https://develop.lifen.fr/fhir/CommunicationRequest/valid_uuid'"
95
+
96
+ current_context = e.context
97
+
98
+ expect(current_context[:client]).to eq "app"
99
+ expect(current_context[:url]).to eq "https://develop.lifen.fr/fhir/CommunicationRequest/valid_uuid"
100
+ expect(current_context[:method]).to eq "get"
101
+ expect(current_context[:token]).to eq "invalid_application_acce********"
102
+ expect(current_context[:payload_sent]).to eq({})
103
+ expect(current_context[:trace_id]).to eq "unknown"
104
+ expect(current_context[:payload_received]).to include('"diagnostics": "Invalid access token"')
105
+ end
106
+
107
+ end
108
+ end
109
+
110
+ context '422' do
111
+ let(:practitioner) { LifenFhir::Practitioner.new(uuid: "11e5c85e-9bc0-4c6e-9b29-deb9993a92c0") }
112
+
113
+ it 'extracts the correct context' do
114
+ begin
115
+ VCR.use_cassette "practitioner/create_channel/address/invalid_attributes" do
116
+ practitioner.create_address(type: "address", lines: [], city: "Paris", postal_code: "75002", country: "France")
117
+ end
118
+ rescue LifenFhir::Error => e
119
+
120
+ expect(e.to_s).to eq "422 Error, Unprocessable Entity, Diagnostic: ''line' is mandatory in address data.' -- POST 'https://develop.lifen.fr/fhir/Practitioner/11e5c85e-9bc0-4c6e-9b29-deb9993a92c0/$add-address'"
121
+
122
+ current_context = e.context
123
+
124
+ expect(current_context[:client]).to eq "app"
125
+ expect(current_context[:url]).to eq "https://develop.lifen.fr/fhir/Practitioner/11e5c85e-9bc0-4c6e-9b29-deb9993a92c0/$add-address"
126
+ expect(current_context[:method]).to eq "post"
127
+ expect(current_context[:token]).to eq "valid_application_access******"
128
+ expect(current_context[:payload_sent]["resourceType"]).to eq("Practitioner")
129
+ expect(current_context[:trace_id]).to eq "unknown"
130
+ expect(current_context[:payload_received]).to include('"diagnostics": "\'line\' is mandatory in address data."')
131
+ end
132
+ end
133
+ end
134
+
135
+ context '500' do
136
+ let(:medium) { LifenFhir::Medium.new(uuid: "valid-medium-uuid-1") }
137
+ let(:sender) { LifenFhir::Practitioner.new(uuid: "valid-sender-uuid") }
138
+ let(:recipient) { LifenFhir::Practitioner.new(uuid: "valid-recipient-uuid-1") }
139
+ let(:binary) { LifenFhir::Binary.new(uuid: "invalid-binary-uuid") }
140
+ let(:communication_request) { LifenFhir::CommunicationRequest.new(sender: sender, recipients: [recipient], medium: [medium], binary: binary) }
141
+
142
+ it 'extracts the correct context' do
143
+ begin
144
+ VCR.use_cassette "communication_request/send/unknown_binary" do
145
+ communication_request.send
146
+ end
147
+ rescue LifenFhir::Error => e
148
+
149
+ expect(e.to_s).to eq "500 Error, Internal server error -- POST 'https://develop.lifen.fr/fhir/CommunicationRequest'"
150
+
151
+ current_context = e.context
152
+
153
+ expect(current_context[:client]).to eq "app"
154
+ expect(current_context[:url]).to eq "https://develop.lifen.fr/fhir/CommunicationRequest"
155
+ expect(current_context[:method]).to eq "post"
156
+ expect(current_context[:token]).to eq "valid_application_access******"
157
+ expect(current_context[:payload_sent][:resourceType]).to eq("CommunicationRequest")
158
+ expect(current_context[:trace_id]).to eq "unknown"
159
+ expect(current_context[:payload_received]).to include('"diagnostics": "Failed to call access method"')
160
+ end
161
+ end
162
+
163
+
164
+ end
165
+
166
+ end
167
+
168
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lifen_fhir
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leonard Sellam
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-06-05 00:00:00.000000000 Z
12
+ date: 2017-06-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -217,7 +217,6 @@ files:
217
217
  - lib/lifen_fhir/patient.rb
218
218
  - lib/lifen_fhir/practitioner.rb
219
219
  - lib/lifen_fhir/telecom.rb
220
- - lib/lifen_fhir/user_authenticated_client.rb
221
220
  - lib/lifen_fhir/version.rb
222
221
  - lifen_fhir.gemspec
223
222
  - spec/binary_spec.rb
@@ -226,6 +225,8 @@ files:
226
225
  - spec/cassettes/communication_request/find_by_uuid/invalid_uuid.yml
227
226
  - spec/cassettes/communication_request/find_by_uuid/valid_uuid_with_a_status.yml
228
227
  - spec/cassettes/communication_request/find_by_uuid/valid_uuid_with_no_status.yml
228
+ - spec/cassettes/communication_request/find_by_uuid/valid_uuid_with_status_and_binary_only.yml
229
+ - spec/cassettes/communication_request/find_by_uuid/valid_uuid_with_status_only.yml
229
230
  - spec/cassettes/communication_request/send/invalid_medium.yml
230
231
  - spec/cassettes/communication_request/send/patient/invalid_attributes_binary.yml
231
232
  - spec/cassettes/communication_request/send/patient/valid_attributes_binary.yml
@@ -234,6 +235,7 @@ files:
234
235
  - spec/cassettes/communication_request/send/unknown_recipient.yml
235
236
  - spec/cassettes/communication_request/send/valid_attributes.yml
236
237
  - spec/cassettes/communication_request/send/valid_attributes_binary.yml
238
+ - spec/cassettes/errors/401.yml
237
239
  - spec/cassettes/logger/invalid.yml
238
240
  - spec/cassettes/logger/valid.yml
239
241
  - spec/cassettes/patient/create/invalid_attributes.yml
@@ -251,6 +253,7 @@ files:
251
253
  - spec/cassettes/practitioner/find_by_rpps/wrong_rpps.yml
252
254
  - spec/category_spec.rb
253
255
  - spec/communication_request_spec.rb
256
+ - spec/error_spec.rb
254
257
  - spec/logger_spec.rb
255
258
  - spec/patient_spec.rb
256
259
  - spec/practitioner_spec.rb
@@ -288,6 +291,8 @@ test_files:
288
291
  - spec/cassettes/communication_request/find_by_uuid/invalid_uuid.yml
289
292
  - spec/cassettes/communication_request/find_by_uuid/valid_uuid_with_a_status.yml
290
293
  - spec/cassettes/communication_request/find_by_uuid/valid_uuid_with_no_status.yml
294
+ - spec/cassettes/communication_request/find_by_uuid/valid_uuid_with_status_and_binary_only.yml
295
+ - spec/cassettes/communication_request/find_by_uuid/valid_uuid_with_status_only.yml
291
296
  - spec/cassettes/communication_request/send/invalid_medium.yml
292
297
  - spec/cassettes/communication_request/send/patient/invalid_attributes_binary.yml
293
298
  - spec/cassettes/communication_request/send/patient/valid_attributes_binary.yml
@@ -296,6 +301,7 @@ test_files:
296
301
  - spec/cassettes/communication_request/send/unknown_recipient.yml
297
302
  - spec/cassettes/communication_request/send/valid_attributes.yml
298
303
  - spec/cassettes/communication_request/send/valid_attributes_binary.yml
304
+ - spec/cassettes/errors/401.yml
299
305
  - spec/cassettes/logger/invalid.yml
300
306
  - spec/cassettes/logger/valid.yml
301
307
  - spec/cassettes/patient/create/invalid_attributes.yml
@@ -313,6 +319,7 @@ test_files:
313
319
  - spec/cassettes/practitioner/find_by_rpps/wrong_rpps.yml
314
320
  - spec/category_spec.rb
315
321
  - spec/communication_request_spec.rb
322
+ - spec/error_spec.rb
316
323
  - spec/logger_spec.rb
317
324
  - spec/patient_spec.rb
318
325
  - spec/practitioner_spec.rb
@@ -1,39 +0,0 @@
1
- module LifenFhir
2
- class UserAuthenticatedClient < Client
3
-
4
- def initialize(token)
5
- @token = token
6
- end
7
-
8
- attr_reader :token
9
-
10
- private
11
-
12
- def handle_errors(response, params)
13
- super(response, params)
14
-
15
- case response.status
16
- when 400
17
- raise InvalidParamsError, "Error 400, Invalid params, #{response_error(response, params)}"
18
- when 401
19
- raise UnauthorizedError, "Error 401, Token is not valid, #{response_error(response, params)}"
20
- when 403
21
- raise Error, "Error 403, Action is forbidden, #{response_error(response, params)}"
22
- end
23
-
24
- end
25
-
26
- def response_error(response, params)
27
- "User Client, #{super(response, params)}"
28
- end
29
-
30
- def bearer
31
- token.value
32
- end
33
-
34
- def before_request
35
- token.refresh_once_if_needed
36
- end
37
-
38
- end
39
- end