Class: IsbmAdaptor::Client
- Inherits:
-
Object
- Object
- IsbmAdaptor::Client
- Defined in:
- lib/isbm_adaptor/client.rb
Direct Known Subclasses
ChannelManagement, ConsumerPublication, ConsumerRequest, ProviderPublication, ProviderRequest
Instance Method Summary collapse
-
#extract_message(response) ⇒ IsbmAdaptor::Message
Creates an IsbmAdaptor::Message from a ISBM response.
-
#initialize(wsdl_file, endpoint, options = {}) ⇒ Client
constructor
Creates a new ISBM client.
-
#validate_inclusion_in(value, set, name) ⇒ void
Validates the inclusion of the passed value in a given set.
-
#validate_presence_of(value, name) ⇒ void
Validates the presence of the passed value.
-
#validate_xml(xml) ⇒ void
Validates the well formedness of the XML string and raises an error if any errors are encountered.
Constructor Details
#initialize(wsdl_file, endpoint, options = {}) ⇒ Client
Creates a new ISBM client.
12 13 14 15 16 17 |
# File 'lib/isbm_adaptor/client.rb', line 12 def initialize(wsdl_file, endpoint, = {}) [:wsdl] = wsdl_dir + wsdl_file [:endpoint] = endpoint () @client = Savon.client() end |
Instance Method Details
#extract_message(response) ⇒ IsbmAdaptor::Message
Creates an IsbmAdaptor::Message from a ISBM response.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/isbm_adaptor/client.rb', line 66 def (response) # Extract the message element # e.g. /Envelope/Body/ReadPublicationResponse/PublicationMessage = response.doc.root.first_element_child.first_element_child.first_element_child return nil unless id = .element_children[0].text content = .element_children[1].first_element_child topics = .element_children[2..-1].map {|e| e.text} # Retain any ancestor namespaces in case they are applicable for the element # and/or children. This is because content.to_xml does not output ancestor # namespaces. # There may be unnecessary namespaces carried across (e.g. ISBM, SOAP), but we # can't tell if the content uses them without parsing the content itself. content.namespaces.each do |key, value| # Don't replace default namespace if it already exists next if key == 'xmlns' && content['xmlns'] content[key] = value end # Wrap content in a separate Nokogiri document. This allows the ability to # validate the content against a schema. doc = Nokogiri::XML(content.to_xml) IsbmAdaptor::Message.new(id, doc, topics) end |
#validate_inclusion_in(value, set, name) ⇒ void
This method returns an undefined value.
Validates the inclusion of the passed value in a given set.
46 47 48 49 50 |
# File 'lib/isbm_adaptor/client.rb', line 46 def validate_inclusion_in(value, set, name) unless set.include?(value) raise ArgumentError, "#{value} is not a valid #{name}. Must be #{set[0..-2].join(', ')} or #{set.last}." end end |
#validate_presence_of(value, name) ⇒ void
This method returns an undefined value.
Validates the presence of the passed value.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/isbm_adaptor/client.rb', line 25 def validate_presence_of(value, name) if value.respond_to?(:each) value.each do |v| if v.blank? raise ArgumentError, "Values in #{name} must not be blank" end end else if value.blank? raise ArgumentError, "#{name} must not be blank" end end end |
#validate_xml(xml) ⇒ void
This method returns an undefined value.
Validates the well formedness of the XML string and raises an error if any errors are encountered.
57 58 59 60 |
# File 'lib/isbm_adaptor/client.rb', line 57 def validate_xml(xml) doc = Nokogiri.XML(xml) raise ArgumentError, "XML is not well formed: #{xml}" unless doc.errors.empty? end |