lifen_fhir 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +2 -0
  4. data/.ruby-version +1 -0
  5. data/CHANGELOG.md +9 -0
  6. data/Gemfile +3 -0
  7. data/Gemfile.lock +74 -0
  8. data/License.txt +22 -0
  9. data/README.md +131 -0
  10. data/lib/lifen_fhir/app_authenticated_client.rb +38 -0
  11. data/lib/lifen_fhir/attachment.rb +26 -0
  12. data/lib/lifen_fhir/base.rb +7 -0
  13. data/lib/lifen_fhir/binary.rb +29 -0
  14. data/lib/lifen_fhir/category.rb +26 -0
  15. data/lib/lifen_fhir/channel.rb +31 -0
  16. data/lib/lifen_fhir/client.rb +107 -0
  17. data/lib/lifen_fhir/communication_request.rb +67 -0
  18. data/lib/lifen_fhir/configuration.rb +22 -0
  19. data/lib/lifen_fhir/content_string.rb +15 -0
  20. data/lib/lifen_fhir/error.rb +27 -0
  21. data/lib/lifen_fhir/medium.rb +15 -0
  22. data/lib/lifen_fhir/patient.rb +27 -0
  23. data/lib/lifen_fhir/practitioner.rb +97 -0
  24. data/lib/lifen_fhir/user_authenticated_client.rb +39 -0
  25. data/lib/lifen_fhir/version.rb +3 -0
  26. data/lib/lifen_fhir.rb +29 -0
  27. data/lifen_fhir.gemspec +30 -0
  28. data/spec/binary_spec.rb +29 -0
  29. data/spec/cassettes/binary/download/invalid.yml +65 -0
  30. data/spec/cassettes/binary/download/valid.yml +1320 -0
  31. data/spec/cassettes/communication_request/send/invalid_medium.yml +69 -0
  32. data/spec/cassettes/communication_request/send/valid_attributes.yml +75 -0
  33. data/spec/cassettes/communication_request/send/valid_attributes_binary.yml +74 -0
  34. data/spec/cassettes/practitionner/create_channel/address/old_valid_attributes.yml +71 -0
  35. data/spec/cassettes/practitionner/create_channel/address/valid_attributes.yml +71 -0
  36. data/spec/cassettes/practitionner/create_channel/telecom/valid_attributes.yml +70 -0
  37. data/spec/cassettes/practitionner/find_by_rpps/existing_rpps.yml +125 -0
  38. data/spec/cassettes/practitionner/find_by_rpps/missing_line_attribute.yml +122 -0
  39. data/spec/cassettes/practitionner/find_by_rpps/wrong_rpps.yml +68 -0
  40. data/spec/category_spec.rb +21 -0
  41. data/spec/communication_request_spec.rb +64 -0
  42. data/spec/practitionner_spec.rb +76 -0
  43. data/spec/spec_helper.rb +24 -0
  44. data/spec/support/master_plan.pdf +0 -0
  45. metadata +232 -0
@@ -0,0 +1,27 @@
1
+ module LifenFhir
2
+ class Patient < Base
3
+
4
+ attribute :first_name, String
5
+ attribute :last_name, String
6
+ attribute :birthdate, Date
7
+
8
+ def fhir_payload
9
+ {
10
+ id: "patient",
11
+ resourceType: "Patient",
12
+ name: [
13
+ {
14
+ family: [
15
+ last_name
16
+ ],
17
+ given: [
18
+ first_name
19
+ ]
20
+ }
21
+ ],
22
+ birthDate: birthdate.to_s
23
+ }
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,97 @@
1
+ module LifenFhir
2
+ class Practitioner < Base
3
+
4
+ attribute :channels, [LifenFhir::Channel]
5
+
6
+ attribute :uuid, String
7
+ attribute :last_name, String
8
+ attribute :first_name, String
9
+ attribute :rpps, String
10
+
11
+ def fhir_payload
12
+ { reference: "Practitioner/#{uuid}" }
13
+ end
14
+
15
+ def self.find_by_rpps(rpps)
16
+ json = application_client.get("fhir/Practitioner/?identifier=#{rpps}")
17
+
18
+ raise "Practitioner not found" if Array(json["entry"]).size != 1
19
+
20
+ user_json = Array(json["entry"]).first.fetch("resource") { {} }
21
+
22
+ user_json[:uuid] = user_json["id"]
23
+
24
+ user = new(user_json)
25
+
26
+ Array(user_json["telecom"]).each do |telecom_json|
27
+ user.channels << LifenFhir::Channel.from_json(telecom_json, "telecom")
28
+ end
29
+
30
+ Array(user_json["address"]).each do |address_json|
31
+ user.channels << LifenFhir::Channel.from_json(address_json, "address")
32
+ end
33
+
34
+ user
35
+ end
36
+
37
+ def create_address(params)
38
+ filtered_params = {"resourceType" => "Practitioner"}
39
+
40
+ address = {
41
+ "line": Array(params[:lines]),
42
+ "city": params[:city],
43
+ "postalCode": params[:postal_code],
44
+ "country": params[:country]
45
+ }
46
+
47
+ filtered_params[params[:type]] = address
48
+
49
+ json = application_client.post("fhir/Practitioner/#{uuid}/$add-address", filtered_params)
50
+
51
+ channel = Channel.new(uuid: json["issue"][0]["id"], type: params[:type], value: "#{Array(params[:lines]).join(", ")}, #{params[:postal_code]} #{params[:city]}")
52
+
53
+ self.channels << channel
54
+
55
+ channel
56
+ end
57
+
58
+ def self.from_json(json)
59
+ reference = json["reference"]
60
+
61
+ uuid = reference.gsub("Practitioner/", "")
62
+
63
+ new(uuid: uuid)
64
+ end
65
+
66
+ def create_telecom(params)
67
+ filtered_params = {"resourceType" => "Practitioner"}
68
+
69
+ telecom = {
70
+ "system": params[:system],
71
+ "value": params[:value]
72
+ }
73
+
74
+ filtered_params[params[:type]] = telecom
75
+
76
+ json = application_client.post("fhir/Practitioner/#{uuid}/$add-telecom", filtered_params)
77
+
78
+ channel = Channel.new(uuid: json["issue"][0]["id"], type: params[:type], value: params[:value])
79
+
80
+ self.channels << channel
81
+
82
+ channel
83
+
84
+ end
85
+
86
+ private
87
+
88
+ def application_client
89
+ @application_client ||= AppAuthenticatedClient.new
90
+ end
91
+
92
+ def self.application_client
93
+ @application_client ||= AppAuthenticatedClient.new
94
+ end
95
+
96
+ end
97
+ end
@@ -0,0 +1,39 @@
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
@@ -0,0 +1,3 @@
1
+ module LifenFhir
2
+ VERSION = "0.1.1"
3
+ end
data/lib/lifen_fhir.rb ADDED
@@ -0,0 +1,29 @@
1
+ module LifenFhir
2
+
3
+ require 'virtus'
4
+ require 'faraday'
5
+ require 'inflecto'
6
+ require 'base64'
7
+
8
+ require "lifen_fhir/version"
9
+
10
+ require 'lifen_fhir/error'
11
+ require 'lifen_fhir/client'
12
+ require 'lifen_fhir/user_authenticated_client'
13
+ require 'lifen_fhir/app_authenticated_client'
14
+ require 'lifen_fhir/configuration'
15
+ require 'lifen_fhir/base'
16
+
17
+ require 'lifen_fhir/channel'
18
+ require 'lifen_fhir/practitioner'
19
+ require 'lifen_fhir/category'
20
+ require 'lifen_fhir/medium'
21
+ require 'lifen_fhir/attachment'
22
+ require 'lifen_fhir/binary'
23
+ require 'lifen_fhir/patient'
24
+ require 'lifen_fhir/content_string'
25
+ require 'lifen_fhir/communication_request'
26
+
27
+ Virtus.finalize
28
+
29
+ end
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'lifen_fhir/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "lifen_fhir"
7
+ spec.version = LifenFhir::VERSION
8
+ spec.authors = ["Leonard Sellam", "Etienne Depaulis"]
9
+ spec.email = ["leonard@lifen.fr", "etienne@lifen.fr"]
10
+ spec.homepage = "https://github.com/honestica/lifen-fhir"
11
+ spec.description = %q{Lifen FHIR API Ruby client}
12
+ spec.summary = %q{Lifen FHIR API Ruby client}
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", '~> 1.12'
21
+ spec.add_development_dependency "rake", '~> 10.5'
22
+ spec.add_development_dependency "rspec", '~> 3.5'
23
+ spec.add_development_dependency "vcr", '~> 3.0'
24
+ spec.add_development_dependency "webmock", '~> 1.24'
25
+ spec.add_development_dependency "awesome_print"
26
+
27
+ spec.add_runtime_dependency "virtus", '>= 1.0'
28
+ spec.add_runtime_dependency "inflecto"
29
+ spec.add_runtime_dependency "faraday", '>= 0.9'
30
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe LifenFhir::Binary do
4
+
5
+ describe ':download with right uuid' do
6
+
7
+ let(:binary) { LifenFhir::Binary.new(uuid: "valid_binary_id") }
8
+
9
+ it 'works' do
10
+
11
+ VCR.use_cassette "binary/download/valid" do
12
+ expect(Digest::MD5.hexdigest(binary.download)).to eq("2285abb032c5e9054a518690b4701ab6")
13
+ end
14
+ end
15
+ end
16
+
17
+ describe ':download with right uuid' do
18
+
19
+ let(:binary) { LifenFhir::Binary.new(uuid: "wrong-binary-uuid") }
20
+
21
+ it 'fails with wrong uuid' do
22
+
23
+ VCR.use_cassette "binary/download/invalid" do
24
+ expect{ binary.download }.to raise_error LifenFhir::Error
25
+ end
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,65 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://develop.lifen.fr/fhir/Binary/wrong-binary-uuid
6
+ body:
7
+ encoding: UTF-8
8
+ string: "{}"
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.11.0
12
+ Authorization:
13
+ - Bearer valid_application_access_token
14
+ Accept:
15
+ - application/pdf
16
+ Accept-Encoding:
17
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
18
+ response:
19
+ status:
20
+ code: 404
21
+ message: Not Found
22
+ headers:
23
+ Server:
24
+ - nginx/1.6.2
25
+ Date:
26
+ - Wed, 05 Apr 2017 12:08:41 GMT
27
+ Content-Type:
28
+ - application/xml+fhir;charset=UTF-8
29
+ Transfer-Encoding:
30
+ - chunked
31
+ Connection:
32
+ - keep-alive
33
+ X-B3-Sampled:
34
+ - '1'
35
+ X-B3-Spanid:
36
+ - fb30b5b1b27d94fe
37
+ X-B3-Traceid:
38
+ - fb30b5b1b27d94fe
39
+ X-Content-Type-Options:
40
+ - nosniff
41
+ X-Xss-Protection:
42
+ - 1; mode=block
43
+ Cache-Control:
44
+ - no-cache, no-store, max-age=0, must-revalidate
45
+ Pragma:
46
+ - no-cache
47
+ Expires:
48
+ - '0'
49
+ X-Powered-By:
50
+ - HAPI FHIR 2.2 REST Server (FHIR Server; FHIR 1.0.2/DSTU2)
51
+ Access-Control-Allow-Credentials:
52
+ - 'true'
53
+ body:
54
+ encoding: UTF-8
55
+ string: |-
56
+ <OperationOutcome xmlns="http://hl7.org/fhir">
57
+ <issue>
58
+ <severity value="error"/>
59
+ <code value="not-found"/>
60
+ <diagnostics value="Unknown Resource id=wrong-binary-uuid"/>
61
+ </issue>
62
+ </OperationOutcome>
63
+ http_version:
64
+ recorded_at: Wed, 05 Apr 2017 12:08:41 GMT
65
+ recorded_with: VCR 3.0.3