Class: IsbmAdaptor::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/isbm_adaptor/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(wsdl_file, endpoint, options = {}) ⇒ Client

Creates a new ISBM client.

Parameters:

  • wsdl_file (String)

    the filename of the WSDL

  • endpoint (String)

    the SOAP endpoint URI

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :logger (Object) — default: Rails.logger or $stdout

    location where log should be output

  • :log (Boolean) — default: true

    specify whether requests are logged

  • :pretty_print_xml (Boolean) — default: false

    specify whether request and response XML are formatted



12
13
14
15
16
17
# File 'lib/isbm_adaptor/client.rb', line 12

def initialize(wsdl_file, endpoint, options = {})
  options[:wsdl] = wsdl_dir + wsdl_file
  options[:endpoint] = endpoint
  default_savon_options(options)
  @client = Savon.client(options)
end

Instance Method Details

#extract_message(response) ⇒ IsbmAdaptor::Message

Creates an IsbmAdaptor::Message from a ISBM response.

Parameters:

  • response (Savon::Response)

    the ISBM response

Returns:



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 extract_message(response)
  # Extract the message element
  # e.g. /Envelope/Body/ReadPublicationResponse/PublicationMessage
  message = response.doc.root.first_element_child.first_element_child.first_element_child

  return nil unless message

  id = message.element_children[0].text
  content = message.element_children[1].first_element_child
  topics = message.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.

Parameters:

  • value (Object)

    object to validate inclusion

  • set (Array)

    set to validate inclusion

  • name (String)

    name of value to include in error message if not present

Raises:

  • (ArgumentError)

    if value is not included



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.

Parameters:

  • value (Object)

    object to validate presence

  • name (String)

    name of value to include in error message if not present

Raises:

  • (ArgumentError)

    if value is not present



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.

Parameters:

  • xml (String)

    the XML string to parse

Raises:

  • (ArgumentError)


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