Class: FHIR::ClientReply
- Inherits:
-
Object
- Object
- FHIR::ClientReply
- Defined in:
- lib/fhir_client/model/client_reply.rb
Constant Summary collapse
- @@validation_rules =
JSON.parse(File.open(File.join(File.('..', File.dirname(File.absolute_path(__FILE__))), 'fhir_api_validation.json'), 'r:UTF-8', &:read))
- @@path_regexes =
{ '[type]' => "(#{FHIR::RESOURCES.join('|')})", '[id]' => FHIR::PRIMITIVES['id']['regex'], '[vid]' => FHIR::PRIMITIVES['id']['regex'], '[name]' => "([A-Za-z\-]+)" }
- @@rfs1123 =
/\A\s* (?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s*,\s*)? (\d{1,2})\s+ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+ (\d{2,})\s+ (\d{2})\s* :\s*(\d{2})\s* (?::\s*(\d{2}))?\s+ ([+-]\d{4}| UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Z])/ix
- @@header_regexes =
{ 'Content-Type' => Regexp.new("(#{FHIR::Formats::ResourceFormat::RESOURCE_XML.gsub('+', '\\\+')}|#{FHIR::Formats::ResourceFormat::RESOURCE_JSON.gsub('+', '\\\+')})(([ ;]+)(charset)([ =]+)(UTF-8|utf-8))?"), 'Accept' => Regexp.new("(#{FHIR::Formats::ResourceFormat::RESOURCE_XML.gsub('+', '\\\+')}|#{FHIR::Formats::ResourceFormat::RESOURCE_JSON.gsub('+', '\\\+')})"), 'Prefer' => Regexp.new('(return=minimal|return=representation)'), 'ETag' => Regexp.new('(W\/)?"[\dA-Za-z]+"'), 'If-Modified-Since' => @@rfs1123, 'If-Match' => Regexp.new('(W\/)?"[\dA-Za-z]+"'), 'If-None-Match' => Regexp.new('(W\/)?"[\dA-Za-z]+"'), 'If-None-Exist' => Regexp.new('([\w\-]+(=[\w\-.:\/\|]*)?(&[\w\-]+(=[\w\-.:\/\|]*)?)*)?'), 'Location' => Regexp.new("http(s)?:\/\/[A-Za-z0-9\/\\-\\.]+\/#{@@path_regexes['[type]']}\/#{@@path_regexes['[id]']}\/_history\/#{@@path_regexes['[vid]']}"), 'Last-Modified' => @@rfs1123 }
Instance Attribute Summary collapse
-
#request ⇒ Object
{ :method => :get, :url => ‘bonfire.mitre.org/fhir/Patient/123/$everything’, :path => ‘Patient/123/$everything’ :headers => {}, :payload => nil # body of request goes here in POST }.
-
#resource ⇒ Object
a FHIR resource.
-
#resource_class ⇒ Object
class of the :resource.
-
#response ⇒ Object
{ :code => ‘200’, :headers => {}, :body => ‘or json here’ }.
Instance Method Summary collapse
- #body ⇒ Object
- #code ⇒ Object
- #id ⇒ Object
-
#initialize(request, response) ⇒ ClientReply
constructor
A new instance of ClientReply.
- #is_valid? ⇒ Boolean
- #self_link ⇒ Object
- #to_hash ⇒ Object
- #validate ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(request, response) ⇒ ClientReply
Returns a new instance of ClientReply.
51 52 53 54 |
# File 'lib/fhir_client/model/client_reply.rb', line 51 def initialize(request, response) @request = request @response = response end |
Instance Attribute Details
#request ⇒ Object
:method => :get,
:url => 'http://bonfire.mitre.org/fhir/Patient/123/$everything',
:path => 'Patient/123/$everything'
:headers => {,
:payload => nil # body of request goes here in POST
}
41 42 43 |
# File 'lib/fhir_client/model/client_reply.rb', line 41 def request @request end |
#resource ⇒ Object
a FHIR resource
48 49 50 |
# File 'lib/fhir_client/model/client_reply.rb', line 48 def resource @resource end |
#resource_class ⇒ Object
class of the :resource
49 50 51 |
# File 'lib/fhir_client/model/client_reply.rb', line 49 def resource_class @resource_class end |
#response ⇒ Object
:code => '200',
:headers => {,
:body => 'or json here'
}
47 48 49 |
# File 'lib/fhir_client/model/client_reply.rb', line 47 def response @response end |
Instance Method Details
#body ⇒ Object
84 85 86 |
# File 'lib/fhir_client/model/client_reply.rb', line 84 def body @response[:body] unless @response.nil? end |
#code ⇒ Object
56 57 58 |
# File 'lib/fhir_client/model/client_reply.rb', line 56 def code @response[:code].to_i unless @response.nil? end |
#id ⇒ Object
60 61 62 63 64 |
# File 'lib/fhir_client/model/client_reply.rb', line 60 def id return nil if @resource_class.nil? (self_link || @request[:url]) =~ %r{(?<=#{@resource_class.name.demodulize}\/)([^\/]+)} Regexp.last_match(1) end |
#is_valid? ⇒ Boolean
95 96 97 |
# File 'lib/fhir_client/model/client_reply.rb', line 95 def is_valid? validate.empty? end |
#self_link ⇒ Object
80 81 82 |
# File 'lib/fhir_client/model/client_reply.rb', line 80 def self_link (@response[:headers]['content-location'] || @response[:headers]['location']) unless @response.nil? || @response[:headers].nil? end |
#to_hash ⇒ Object
88 89 90 91 92 93 |
# File 'lib/fhir_client/model/client_reply.rb', line 88 def to_hash hash = {} hash['request'] = @request hash['response'] = @response hash end |
#validate ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/fhir_client/model/client_reply.rb', line 99 def validate errors = [] @@validation_rules.each do |rule| next unless rule['verb'] == @request[:method].to_s.upcase rule_match = false rule['path'].each do |path| rule_regex = path.gsub('/', '(\/)').gsub('?', '\?') @@path_regexes.each do |token, regex| rule_regex.gsub!(token, regex) end rule_match = true if Regexp.new(rule_regex) =~ @request[:path] end next unless rule_match # check the request headers errors << validate_headers("#{rule['interaction'].upcase} REQUEST", @request[:headers], rule['request']['headers']) # check the request body errors << validate_body("#{rule['interaction'].upcase} REQUEST", @request[:payload], rule['request']['body']) # check the response codes unless rule['response']['status'].include?(@response[:code].to_i) errors << "#{rule['interaction'].upcase} RESPONSE: Invalid response code: #{@response[:code]}" end next unless @response[:code].to_i < 400 # check the response headers errors << validate_headers("#{rule['interaction'].upcase} RESPONSE", @response[:headers], rule['response']['headers']) # check the response body errors << validate_body("#{rule['interaction'].upcase} RESPONSE", @response[:body], rule['response']['body']) end errors.flatten end |
#version ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/fhir_client/model/client_reply.rb', line 66 def version version = nil link = self_link if link && link.include?('_history/') begin tokens = link.split('_history/') version = tokens.last.split('/').first rescue version = nil end end version end |