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


11
12
13
14
15
16
17
18
# File 'lib/accession/accession/response.rb', line 11

def initialize(response)
  @code = response.code
  @body = response.body.to_s

  if success?
    @xml = Nokogiri::XML::Document.parse(body)
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body


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

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code


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

def code
  @code
end

#xmlObject (readonly)

Returns the value of attribute xml


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

def xml
  @xml
end

Instance Method Details

#accession_numberObject

If the request was successful and the receipt says so extract the accession number


39
40
41
42
43
# File 'lib/accession/accession/response.rb', line 39

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)

30
31
32
33
34
35
36
# File 'lib/accession/accession/response.rb', line 30

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.


46
47
48
49
50
# File 'lib/accession/accession/response.rb', line 46

def errors
  return unless success?

  xml.search('ERROR').collect(&:text)
end

#failure?Boolean

Returns:

  • (Boolean)

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

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

#success?Boolean

Returns:

  • (Boolean)

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

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