Skip to content

Commit

Permalink
changed how the Operation handles the body Hash
Browse files Browse the repository at this point in the history
replaced the options Hash with an Operation#body accessor,
because endpoint and headers work this way and there will
soon be a #header accessor.
  • Loading branch information
rubiii committed Jun 2, 2013
1 parent 5dd27fc commit 58c5de5
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 197 deletions.
6 changes: 0 additions & 6 deletions lib/savon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ def operation(service_name, port_name, operation_name)
Operation.new(operation, @wsdl, @http)
end

# Public: Calls an operation by service, port and operation name
# and returns a Response object. Also accepts a Hash of options.
def call(service_name, port_name, operation_name, options = {})
operation(service_name, port_name, operation_name).call(options)
end

private

# Private: Returns a new instance of the HTTP adapter to use.
Expand Down
12 changes: 6 additions & 6 deletions lib/savon/envelope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ class Envelope

NSID = 'lol'

def initialize(operation, options = {})
def initialize(operation, body)
@logger = Logging.logger[self]

@operation = operation
@message = options[:message]
@body = body

unless @message
@logger.warn("No :message Hash given for the #{operation.name.inspect} operation.")
@message = {}
unless @body
@logger.warn("No request body Hash given for the #{operation.name.inspect} operation.")
@body = {}
end

@nsid_counter = -1
Expand All @@ -26,7 +26,7 @@ def register_namespace(namespace)
end

def to_s
body = Body.new(self, @operation.input).build(@message)
body = Body.new(self, @operation.input).build(@body)
body = build_rpc_wrapper(body) if rpc_call?

build_envelope(body)
Expand Down
17 changes: 9 additions & 8 deletions lib/savon/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,22 @@ def headers
# Public: Sets the Hash of HTTP headers.
attr_writer :headers

# Public: Create an example request Hash.
def example_request
# Public: Sets the request body Hash.
attr_accessor :body

# Public: Create an example request body Hash.
def example_body
ExampleMessage.new(@operation.input).to_hash
end

# Public: Build the request XML for this operation.
def build(options = {})
Envelope.new(@operation, options).to_s
def build
Envelope.new(@operation, body).to_s
end

# Public: Call the operation.
def call(options = {})
body = build(options)

raw_response = @http.post(endpoint, headers, body)
def call
raw_response = @http.post(endpoint, headers, build)
Response.new(raw_response)
end

Expand Down
45 changes: 22 additions & 23 deletions spec/integration/betfair_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
it 'creates a proper example request for messages with Arrays' do
operation = client.operation(service_name, port_name, :getMUBetsLite)

expect(operation.example_request).to eq(
expect(operation.example_body).to eq(
getMUBetsLite: {
request: {

Expand Down Expand Up @@ -118,29 +118,27 @@
operation = client.operation(service_name, port_name, :getMUBetsLite)
datetime_value = (Time.now - 365).xmlschema

request = Nokogiri.XML operation.build(
message: {
getMUBetsLite: {
request: {
header: {
clientStamp: 'test',
sessionToken: 'token'
},
betStatus: 'U',
marketId: 1,
betIds: {
betId: [1, 2, 3]
},
orderBy: 'NONE',
sortOrder: 'DESC',
recordCount: 10,
startRecord: 1,
matchedSince: datetime_value,
excludeLastSecond: true
}
operation.body = {
getMUBetsLite: {
request: {
header: {
clientStamp: 'test',
sessionToken: 'token'
},
betStatus: 'U',
marketId: 1,
betIds: {
betId: [1, 2, 3]
},
orderBy: 'NONE',
sortOrder: 'DESC',
recordCount: 10,
startRecord: 1,
matchedSince: datetime_value,
excludeLastSecond: true
}
}
)
}

expected = Nokogiri.XML(%{
<env:Envelope
Expand Down Expand Up @@ -174,7 +172,8 @@
</env:Envelope>
})

expect(request).to be_equivalent_to(expected).respecting_element_order
expect(Nokogiri.XML operation.build).
to be_equivalent_to(expected).respecting_element_order
end

end
15 changes: 7 additions & 8 deletions spec/integration/blz_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
it 'creates an example request' do
operation = client.operation(service_name, port_name, :getBank)

expect(operation.example_request).to eq(
expect(operation.example_body).to eq(
getBank: {
blz: 'string'
}
Expand All @@ -20,13 +20,11 @@
it 'builds a request' do
operation = client.operation(service_name, port_name, :getBank)

request = Nokogiri.XML operation.build(
message: {
getBank: {
blz: 70070010
}
operation.body = {
getBank: {
blz: 70070010
}
)
}

expected = Nokogiri.XML(%{
<env:Envelope
Expand All @@ -41,7 +39,8 @@
</env:Envelope>
})

expect(request).to be_equivalent_to(expected).respecting_element_order
expect(Nokogiri.XML operation.build).
to be_equivalent_to(expected).respecting_element_order
end

end
49 changes: 21 additions & 28 deletions spec/integration/document_literal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
it 'works with op1' do
op1 = client.operation(service_name, port_name, :op1)

# Check the example request.
expect(op1.example_request).to eq(
expect(op1.example_body).to eq(
op1: {
in: {
data1: 'int',
Expand All @@ -20,17 +19,14 @@
}
)

# Build the request.
actual = Nokogiri.XML op1.build(
message: {
op1: {
in: {
data1: 24,
data2: 36
}
op1.body = {
op1: {
in: {
data1: 24,
data2: 36
}
}
)
}

# The expected request.
expected = Nokogiri.XML('
Expand All @@ -49,14 +45,14 @@
</env:Envelope>
')

expect(actual).to be_equivalent_to(expected).respecting_element_order
expect(Nokogiri.XML op1.build).
to be_equivalent_to(expected).respecting_element_order
end

it 'works with op3' do
op3 = client.operation(service_name, port_name, :op3)

# Check the example request.
expect(op3.example_request).to eq(
expect(op3.example_body).to eq(
op3: {
DataElem: {
data1: 'int',
Expand All @@ -68,22 +64,18 @@
}
)

# Build the request.
actual = Nokogiri.XML op3.build(
message: {
op3: {
DataElem: {
data1: 64,
data2: 128
},
in2: {
RefDataElem: 3
}
op3.body = {
op3: {
DataElem: {
data1: 64,
data2: 128
},
in2: {
RefDataElem: 3
}
}
)
}

# The expected request.
expected = Nokogiri.XML('
<env:Envelope
xmlns:lol0="http://apiNamespace.com"
Expand All @@ -105,7 +97,8 @@
</env:Envelope>
')

expect(actual).to be_equivalent_to(expected).respecting_element_order
expect(Nokogiri.XML op3.build).
to be_equivalent_to(expected).respecting_element_order
end

end
17 changes: 8 additions & 9 deletions spec/integration/email_verification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
it 'creates an example request' do
operation = client.operation(service_name, port_name, :VerifyEmail)

expect(operation.example_request).to eq(
expect(operation.example_body).to eq(
VerifyEmail: {
email: 'string',
LicenseKey: 'string'
Expand All @@ -54,14 +54,12 @@
it 'builds a request' do
operation = client.operation(service_name, port_name, :VerifyEmail)

request = Nokogiri.XML operation.build(
message: {
VerifyEmail: {
email: '[email protected]',
LicenseKey: '?'
}
operation.body = {
VerifyEmail: {
email: '[email protected]',
LicenseKey: '?'
}
)
}

expected = Nokogiri.XML(%{
<env:Envelope
Expand All @@ -77,7 +75,8 @@
</env:Envelope>
})

expect(request).to be_equivalent_to(expected).respecting_element_order
expect(Nokogiri.XML operation.build).
to be_equivalent_to(expected).respecting_element_order
end

end
55 changes: 26 additions & 29 deletions spec/integration/interhome_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
it 'creates an example request including optional elements' do
operation = client.operation(service_name, port_name, :Availability)

expect(operation.example_request).to eq(
expect(operation.example_body).to eq(
Availability: {

# These are optional.
Expand All @@ -102,35 +102,32 @@
it 'skips optional elements in the request' do
operation = client.operation(service_name, port_name, :Availability)

expect(
Nokogiri.XML operation.build(
message: {
Availability: {
inputValue: {

# We're leaving out two elements on purpose.
AccommodationCode: 'secret'

}
}
operation.body = {
Availability: {
inputValue: {
# We're leaving out two elements on purpose.
AccommodationCode: 'secret'
}
)
).to be_equivalent_to(
Nokogiri.XML('
<env:Envelope
xmlns:lol0="http://www.interhome.com/webservice"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<env:Body>
<lol0:Availability>
<lol0:inputValue>
<lol0:AccommodationCode>secret</lol0:AccommodationCode>
</lol0:inputValue>
</lol0:Availability>
</env:Body>
</env:Envelope>
')
).respecting_element_order
}
}

expected = Nokogiri.XML('
<env:Envelope
xmlns:lol0="http://www.interhome.com/webservice"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<env:Body>
<lol0:Availability>
<lol0:inputValue>
<lol0:AccommodationCode>secret</lol0:AccommodationCode>
</lol0:inputValue>
</lol0:Availability>
</env:Body>
</env:Envelope>
')

expect(Nokogiri.XML operation.build).
to be_equivalent_to(expected).respecting_element_order
end

end
Loading

0 comments on commit 58c5de5

Please sign in to comment.