Skip to content

Commit

Permalink
move symbolize_keys to Propono namespace
Browse files Browse the repository at this point in the history
* behaviour is quirky and conflicts with e.g. ActiveSupport implementation
* retained Propono implementation rather than depend on yet another library, but don't mixin with Hash
* change impact: anyone inheriting Hash#symbolise_keys from Propono and using elsewhere in their projects would need to decide to use another library for symbolize_keys, or use Propono::Utils.symbolize_keys explicitly
  • Loading branch information
tardate committed Jan 10, 2015
1 parent 297bec5 commit 57d7ff7
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -11,7 +11,7 @@ end

namespace :test do
Rake::TestTask.new(:local) do |t|
t.pattern = "test/{components/,services/,helpers/,}*_test.rb"
t.pattern = "test/{components/,services/,utils/,}*_test.rb"
end
end

Expand Down
3 changes: 1 addition & 2 deletions lib/propono.rb
Expand Up @@ -5,8 +5,7 @@
require 'propono/propono_error'
require 'propono/logger'
require 'propono/configuration'

require "propono/helpers/hash"
require "propono/utils"

require 'propono/components/aws_config'
require 'propono/components/sns'
Expand Down
2 changes: 1 addition & 1 deletion lib/propono/components/sqs_message.rb
Expand Up @@ -9,7 +9,7 @@ def initialize(raw_message)
body = JSON.parse(@raw_body_json["Message"])

@raw_message = raw_message
@context = body.symbolize_keys
@context = Propono::Utils.symbolize_keys body
@failure_count = context[:num_failures] || 0
@message = context.delete(:message)
@receipt_handle = raw_message["receipt_handle"]
Expand Down
10 changes: 0 additions & 10 deletions lib/propono/helpers/hash.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/propono/services/publisher.rb
Expand Up @@ -17,7 +17,7 @@ def initialize(topic_id, message, options = {})
raise PublisherError.new("Topic is nil") if topic_id.nil?
raise PublisherError.new("Message is nil") if message.nil?

options = options.symbolize_keys
options = Propono::Utils.symbolize_keys options

@topic_id = topic_id
@message = message
Expand Down
2 changes: 1 addition & 1 deletion lib/propono/services/tcp_listener.rb
Expand Up @@ -29,7 +29,7 @@ def receive_and_process
end

def process_tcp_data(tcp_data)
json = JSON.parse(tcp_data).symbolize_keys
json = Propono::Utils.symbolize_keys JSON.parse(tcp_data)

# Legacy syntax is covered in the else statement
# This conditional and the else block will be removed in v1.
Expand Down
2 changes: 1 addition & 1 deletion lib/propono/services/udp_listener.rb
Expand Up @@ -27,7 +27,7 @@ def receive_and_process
end

def process_udp_data(udp_data)
json = JSON.parse(udp_data).symbolize_keys
json = Propono::Utils.symbolize_keys JSON.parse(udp_data)

# Legacy syntax is covered in the else statement
# This conditional and the else block will be removed in v1.
Expand Down
17 changes: 17 additions & 0 deletions lib/propono/utils.rb
@@ -0,0 +1,17 @@
module Propono
module Utils

# Returns +hash+ with all primary and nested keys to string values symbolised
# To avoid conflicts with ActiveSupport and other libraries that provide Hash symbolisation,
# this method is kept within the Propono namespace and not mixed into Hash
def self.symbolize_keys(hash)
hash.inject({}) do |result, (key, value)|
new_key = key.is_a?(String) ? key.to_sym : key
new_value = value.is_a?(Hash) ? symbolize_keys(value) : value
result[new_key] = new_value
result
end
end

end
end
2 changes: 1 addition & 1 deletion test/helpers/hash_test.rb → test/utils/hash_test.rb
Expand Up @@ -22,7 +22,7 @@ def test_symbolize_keys_works
}
}

assert_equal expected, input.symbolize_keys
assert_equal expected, Propono::Utils.symbolize_keys(input)
end
end
end
Expand Down

0 comments on commit 57d7ff7

Please sign in to comment.