Skip to content

Commit

Permalink
dont monkeypatch String (#988)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcai authored Nov 30, 2022
1 parent 7debea1 commit 06d6faf
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 79 deletions.
1 change: 1 addition & 0 deletions lib/savon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ def self.notify_observers(operation_name, builder, globals, locals)
require "savon/version"
require "savon/client"
require "savon/model"
require "savon/string_utils"
2 changes: 1 addition & 1 deletion lib/savon/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def serialized_messages
message_tag = serialized_message_tag[1]
@wsdl.soap_input(@operation_name.to_sym)[message_tag].each_pair do |message, type|
break if @locals[:message].nil?
message_locals = @locals[:message][message.snakecase.to_sym]
message_locals = @locals[:message][StringUtils.snakecase(message).to_sym]
message_content = Message.new(message_tag, namespace_identifier, @types, @used_namespaces, message_locals, :unqualified, @globals[:convert_request_keys_to], @globals[:unwrap]).to_s
messages += "<#{message} xsi:type=\"#{type.join(':')}\">#{message_content}</#{message}>"
end
Expand Down
30 changes: 0 additions & 30 deletions lib/savon/core_ext/string.rb

This file was deleted.

6 changes: 3 additions & 3 deletions lib/savon/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def all_operations
# Defines a class-level SOAP operation.
def define_class_operation(operation)
class_operation_module.module_eval %{
def #{operation.to_s.snakecase}(locals = {})
def #{StringUtils.snakecase(operation.to_s)}(locals = {})
client.call #{operation.inspect}, locals
end
}
Expand All @@ -38,8 +38,8 @@ def #{operation.to_s.snakecase}(locals = {})
# Defines an instance-level SOAP operation.
def define_instance_operation(operation)
instance_operation_module.module_eval %{
def #{operation.to_s.snakecase}(locals = {})
self.class.#{operation.to_s.snakecase} locals
def #{StringUtils.snakecase(operation.to_s)}(locals = {})
self.class.#{StringUtils.snakecase(operation.to_s)} locals
end
}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/savon/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def initialize(options = {})
:raise_errors => true,
:strip_namespaces => true,
:delete_namespace_attributes => false,
:convert_response_tags_to => lambda { |tag| tag.snakecase.to_sym},
:convert_response_tags_to => lambda { |tag| StringUtils.snakecase(tag).to_sym},
:convert_attributes_to => lambda { |k,v| [k,v] },
:multipart => false,
:adapter => nil,
Expand Down
17 changes: 17 additions & 0 deletions lib/savon/string_utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Savon
module StringUtils
def self.snakecase(inputstring)
str = inputstring.dup
str.gsub! /::/, '/'
str.gsub! /([A-Z]+)([A-Z][a-z])/, '\1_\2'
str.gsub! /([a-z\d])([A-Z])/, '\1_\2'
str.tr! ".", "_"
str.tr! "-", "_"
str.downcase!
str
end
end
end

38 changes: 0 additions & 38 deletions spec/savon/core_ext/string_spec.rb

This file was deleted.

8 changes: 4 additions & 4 deletions spec/savon/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ def to_s
it "can be changed to not strip any namespaces" do
client = new_client(
:endpoint => @server.url(:repeat),
:convert_response_tags_to => lambda { |tag| tag.snakecase },
:convert_response_tags_to => lambda { |tag| Savon::StringUtils.snakecase(tag) },
:strip_namespaces => false
)

Expand Down Expand Up @@ -877,7 +877,7 @@ def to_s

context "global :convert_response_tags_to" do
it "changes how XML tags from the SOAP response are translated into Hash keys" do
client = new_client(:endpoint => @server.url(:repeat), :convert_response_tags_to => lambda { |tag| tag.snakecase.upcase })
client = new_client(:endpoint => @server.url(:repeat), :convert_response_tags_to => lambda { |tag| Savon::StringUtils.snakecase(tag).upcase })
response = client.call(:authenticate, :xml => Fixture.response(:authentication))

expect(response.full_hash["ENVELOPE"]["BODY"]).to include("AUTHENTICATE_RESPONSE")
Expand All @@ -888,7 +888,7 @@ def to_s
globals.log false
globals.wsdl Fixture.wsdl(:authentication)
globals.endpoint @server.url(:repeat)
globals.convert_response_tags_to { |tag| tag.snakecase.upcase }
globals.convert_response_tags_to { |tag| Savon::StringUtils.snakecase(tag).upcase }
end

response = client.call(:authenticate) do |locals|
Expand Down Expand Up @@ -1115,7 +1115,7 @@ def to_s

context "request :response_parser" do
it "instructs Nori to change the response parser" do
nori = Nori.new(:strip_namespaces => true, :convert_tags_to => lambda { |tag| tag.snakecase.to_sym })
nori = Nori.new(:strip_namespaces => true, :convert_tags_to => lambda { |tag| Savon::StringUtils.snakecase(tag).to_sym })
Nori.expects(:new).with { |options| options[:parser] == :nokogiri }.returns(nori)

client = new_client(:endpoint => @server.url(:repeat))
Expand Down
2 changes: 1 addition & 1 deletion spec/savon/soap_fault_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
let(:soap_fault_no_body) { Savon::SOAPFault.new new_response(:body => {}), nori }
let(:no_fault) { Savon::SOAPFault.new new_response, nori }

let(:nori) { Nori.new(:strip_namespaces => true, :convert_tags_to => lambda { |tag| tag.snakecase.to_sym }) }
let(:nori) { Nori.new(:strip_namespaces => true, :convert_tags_to => lambda { |tag| Savon::StringUtils.snakecase(tag).to_sym }) }
let(:nori_no_convert) { Nori.new(:strip_namespaces => true, :convert_tags_to => nil) }

it "inherits from Savon::Error" do
Expand Down
38 changes: 38 additions & 0 deletions spec/savon/string_utils_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true
require "spec_helper"

RSpec.describe Savon::StringUtils do

describe "snakecase" do
it "lowercases one word CamelCase" do
expect(Savon::StringUtils.snakecase("Merb")).to eq("merb")
end

it "makes one underscore snakecase two word CamelCase" do
expect(Savon::StringUtils.snakecase("MerbCore")).to eq("merb_core")
end

it "handles CamelCase with more than 2 words" do
expect(Savon::StringUtils.snakecase("SoYouWantContributeToMerbCore")).to eq("so_you_want_contribute_to_merb_core")
end

it "handles CamelCase with more than 2 capital letter in a row" do
expect(Savon::StringUtils.snakecase("CNN")).to eq("cnn")
expect(Savon::StringUtils.snakecase("CNNNews")).to eq("cnn_news")
expect(Savon::StringUtils.snakecase("HeadlineCNNNews")).to eq("headline_cnn_news")
end

it "does NOT change one word lowercase" do
expect(Savon::StringUtils.snakecase("merb")).to eq("merb")
end

it "leaves snake_case as is" do
expect(Savon::StringUtils.snakecase("merb_core")).to eq("merb_core")
end

it "converts period characters to underscores" do
expect(Savon::StringUtils.snakecase("User.GetEmail")).to eq("user_get_email")
end
end

end
2 changes: 1 addition & 1 deletion spec/support/fixture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def full_hash(fixture)
private

def nori
Nori.new(:strip_namespaces => true, :convert_tags_to => lambda { |tag| tag.snakecase.to_sym })
Nori.new(:strip_namespaces => true, :convert_tags_to => lambda { |tag| Savon::StringUtils.snakecase(tag).to_sym })
end

def fixtures(type)
Expand Down

0 comments on commit 06d6faf

Please sign in to comment.