Class: Accession::Response

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/accession/accession/response.rb

Overview

Sucks the neccessary attributes from a RestClient response

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyObject (readonly)

Returns the value of attribute body.


7
8
9
# File 'lib/accession/accession/response.rb', line 7

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.


7
8
9
# File 'lib/accession/accession/response.rb', line 7

def code
  @code
end

#xmlObject (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_numberObject

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

Returns:

  • (Boolean)

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

#errorsObject

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

Returns:

  • (Boolean)

23
24
25
# File 'lib/accession/accession/response.rb', line 23

def failure?
  code.between?(400, 600)
end

#success?Boolean

Returns:

  • (Boolean)

19
20
21
# File 'lib/accession/accession/response.rb', line 19

def success?
  code.between?(200, 300)
end