Class: Accession::Response
- Inherits:
-
Object
- Object
- Accession::Response
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/accession/accession/response.rb
Overview
Sucks the neccessary attributes from a RestClient response
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#xml ⇒ Object
readonly
Returns the value of attribute xml.
Instance Method Summary collapse
-
#accession_number ⇒ Object
If the request was successful and the receipt says so extract the accession number.
-
#accessioned? ⇒ Boolean
If the request was a success extract a boolean value to state whether accessioning happened based on the xml receipt.
-
#errors ⇒ Object
If the request failed extract the errors from the receipt.
- #failure? ⇒ Boolean
-
#initialize(response) ⇒ Response
constructor
extracts the response code and body from response and pars the body into xml.
- #success? ⇒ Boolean
Constructor Details
#initialize(response) ⇒ Response
extracts the response code and body from response and pars the body into xml
12 13 14 15 16 17 |
# File 'lib/accession/accession/response.rb', line 12 def initialize(response) @code = response.code @body = response.body.to_s @xml = Nokogiri::XML::Document.parse(body) if success? end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
7 8 9 |
# File 'lib/accession/accession/response.rb', line 7 def body @body end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
7 8 9 |
# File 'lib/accession/accession/response.rb', line 7 def code @code end |
#xml ⇒ Object (readonly)
Returns the value of attribute xml.
7 8 9 |
# File 'lib/accession/accession/response.rb', line 7 def xml @xml end |
Instance Method Details
#accession_number ⇒ Object
If the request was successful and the receipt says so extract the accession number
36 37 38 39 40 |
# File 'lib/accession/accession/response.rb', line 36 def accession_number return unless success? xml.at('SAMPLE').try(:attribute, 'accession').try(:value) end |
#accessioned? ⇒ Boolean
If the request was a success extract a boolean value to state whether accessioning happened based on the xml receipt
29 30 31 32 33 |
# File 'lib/accession/accession/response.rb', line 29 def accessioned? return false unless success? ActiveRecord::Type::Boolean.new.cast(xml.at('RECEIPT').attribute('success').value) end |
#errors ⇒ Object
If the request failed extract the errors from the receipt.
43 44 45 46 47 |
# File 'lib/accession/accession/response.rb', line 43 def errors return unless success? xml.search('ERROR').collect(&:text) end |
#failure? ⇒ Boolean
23 24 25 |
# File 'lib/accession/accession/response.rb', line 23 def failure? code.between?(400, 600) end |
#success? ⇒ Boolean
19 20 21 |
# File 'lib/accession/accession/response.rb', line 19 def success? code.between?(200, 300) end |