module Savon::Model

Public Class Methods

extended(base) click to toggle source
# File lib/savon/model.rb, line 5
def self.extended(base)
  base.setup
end

Public Instance Methods

all_operations() click to toggle source
# File lib/savon/model.rb, line 23
def all_operations
  operations(*client.operations)
end
operations(*operations) click to toggle source

Accepts one or more SOAP operations and generates both class and instance methods named after the given operations. Each generated method accepts an optional SOAP message Hash.

# File lib/savon/model.rb, line 16
def operations(*operations)
  operations.each do |operation|
    define_class_operation(operation)
    define_instance_operation(operation)
  end
end
setup() click to toggle source
# File lib/savon/model.rb, line 9
def setup
  class_operation_module
  instance_operation_module
end

Private Instance Methods

class_operation_module() click to toggle source

Class methods.

# File lib/savon/model.rb, line 48
def class_operation_module
  @class_operation_module ||= Module.new {

    def client(globals = {})
      @client ||= Savon::Client.new(globals)
    rescue InitializationError
      raise_initialization_error!
    end

    def global(option, *value)
      client.globals[option] = value
    end

    def raise_initialization_error!
      raise InitializationError,
        "Expected the model to be initialized with either a WSDL document or the SOAP endpoint and target namespace options.\n" \
        "Make sure to setup the model by calling the .client class method before calling the .global method.\n\n" \
        "client(wsdl: '/Users/me/project/service.wsdl')                              # to use a local WSDL document\n" \
        "client(wsdl: 'http://example.com?wsdl')                                     # to use a remote WSDL document\n" \
        "client(endpoint: 'http://example.com', namespace: 'http://v1.example.com')  # if you don't have a WSDL document"
    end

  }.tap { |mod| extend(mod) }
end
client(globals = {}) click to toggle source
# File lib/savon/model.rb, line 51
def client(globals = {})
  @client ||= Savon::Client.new(globals)
rescue InitializationError
  raise_initialization_error!
end
define_class_operation(operation) click to toggle source

Defines a class-level SOAP operation.

# File lib/savon/model.rb, line 30
def define_class_operation(operation)
  class_operation_module.module_eval %{
    def #{StringUtils.snakecase(operation.to_s)}(locals = {})
      client.call #{operation.inspect}, locals
    end
  }
end
define_instance_operation(operation) click to toggle source

Defines an instance-level SOAP operation.

# File lib/savon/model.rb, line 39
def define_instance_operation(operation)
  instance_operation_module.module_eval %{
    def #{StringUtils.snakecase(operation.to_s)}(locals = {})
      self.class.#{StringUtils.snakecase(operation.to_s)} locals
    end
  }
end
global(option, *value) click to toggle source
# File lib/savon/model.rb, line 57
def global(option, *value)
  client.globals[option] = value
end
instance_operation_module() click to toggle source

Instance methods.

# File lib/savon/model.rb, line 74
def instance_operation_module
  @instance_operation_module ||= Module.new {

    def client
      self.class.client
    end

  }.tap { |mod| include(mod) }
end
raise_initialization_error!() click to toggle source
# File lib/savon/model.rb, line 61
def raise_initialization_error!
  raise InitializationError,
    "Expected the model to be initialized with either a WSDL document or the SOAP endpoint and target namespace options.\n" \
    "Make sure to setup the model by calling the .client class method before calling the .global method.\n\n" \
    "client(wsdl: '/Users/me/project/service.wsdl')                              # to use a local WSDL document\n" \
    "client(wsdl: 'http://example.com?wsdl')                                     # to use a remote WSDL document\n" \
    "client(endpoint: 'http://example.com', namespace: 'http://v1.example.com')  # if you don't have a WSDL document"
end