awrence-4.0.0/0000755000004100000410000000000015052041536013170 5ustar www-datawww-dataawrence-4.0.0/lib/0000755000004100000410000000000015052041536013736 5ustar www-datawww-dataawrence-4.0.0/lib/awrence/0000755000004100000410000000000015052041536015362 5ustar www-datawww-dataawrence-4.0.0/lib/awrence/methods.rb0000644000004100000410000000323015052041536017350 0ustar www-datawww-data# frozen_string_literal: true module Awrence module Methods # Recursively converts Rubyish snake_case hash keys to camelBack JSON-style # hash keys suitable for use with a JSON API. # def to_camelback_keys(value = self) process_value(:to_camelback_keys, value, first_upper: false) end # Recursively converts Rubyish snake_case hash keys to CamelCase JSON-style # hash keys suitable for use with a JSON API. # def to_camel_keys(value = self) process_value(:to_camel_keys, value, first_upper: true) end private def camelize_key(key, first_upper: true) case key when Symbol camelize(key.to_s, first_upper: first_upper).to_sym when String camelize(key, first_upper: first_upper) else key # Awrence can't camelize anything except strings and symbols end end def camelize(snake_word, first_upper: true) if first_upper str = snake_word.to_s str = gsubbed(str, /(?:^|_)([^_\s]+)/) gsubbed(str, %r{/([^/]*)}, "::") else parts = snake_word.split("_", 2) parts[0] << camelize(parts[1]) if parts.size > 1 parts[0] || "" end end def gsubbed(str, pattern, extra = "") str.gsub(pattern) do extra + (Awrence.acronyms[Regexp.last_match(1)] || Regexp.last_match(1).capitalize) end end def process_value(method, value, first_upper: true) case value when Array value.map { |v| send(method, v) } when Hash value.to_h { |k, v| [camelize_key(k, first_upper: first_upper), send(method, v)] } else value end end end end awrence-4.0.0/lib/awrence/ext/0000755000004100000410000000000015052041536016162 5ustar www-datawww-dataawrence-4.0.0/lib/awrence/ext/hash/0000755000004100000410000000000015052041536017105 5ustar www-datawww-dataawrence-4.0.0/lib/awrence/ext/hash/to_camel_keys.rb0000644000004100000410000000011115052041536022241 0ustar www-datawww-data# frozen_string_literal: true class Hash include Awrence::Methods end awrence-4.0.0/lib/awrence/ext/array/0000755000004100000410000000000015052041536017300 5ustar www-datawww-dataawrence-4.0.0/lib/awrence/ext/array/to_camel_keys.rb0000644000004100000410000000011215052041536022435 0ustar www-datawww-data# frozen_string_literal: true class Array include Awrence::Methods end awrence-4.0.0/lib/awrence/version.rb0000644000004100000410000000025415052041536017375 0ustar www-datawww-data# frozen_string_literal: true module Awrence version_file = File.join(File.dirname(__FILE__), "../../VERSION") VERSION = File.read(version_file).split("\n").first end awrence-4.0.0/lib/awrence.rb0000644000004100000410000000040115052041536015702 0ustar www-datawww-data# frozen_string_literal: true require "awrence/methods" require "awrence/ext/array/to_camel_keys" require "awrence/ext/hash/to_camel_keys" module Awrence class << self attr_writer :acronyms def acronyms @acronyms ||= {} end end end awrence-4.0.0/Rakefile0000644000004100000410000000031015052041536014627 0ustar www-datawww-data# frozen_string_literal: true require "bundler/gem_tasks" require "rspec/core/rake_task" RSpec::Core::RakeTask.new(:spec) do |t| t.pattern = Dir.glob("spec/**/*_spec.rb") end task default: :spec awrence-4.0.0/MIT-LICENSE0000644000004100000410000000204715052041536014627 0ustar www-datawww-dataCopyright (c) 2023 Technical Panda Ltd Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. awrence-4.0.0/awrence.gemspec0000644000004100000410000000276315052041536016171 0ustar www-datawww-data######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- # stub: awrence 4.0.0 ruby lib Gem::Specification.new do |s| s.name = "awrence".freeze s.version = "4.0.0" s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.metadata = { "rubygems_mfa_required" => "true" } if s.respond_to? :metadata= s.require_paths = ["lib".freeze] s.authors = ["Dave Hrycyszyn".freeze, "Stuart Chinery".freeze] s.date = "1980-01-02" s.description = "Have you ever needed to automatically convert Ruby-style snake_case to CamelCase or camelBack hash keys?\n\nAwrence to the rescue.\n\nThis gem recursively converts all snake_case keys in a hash structure to camelBack.".freeze s.email = ["dave@constructiveproof.com".freeze, "code@technicalpanda.co.uk".freeze] s.files = ["MIT-LICENSE".freeze, "README.md".freeze, "Rakefile".freeze, "VERSION".freeze, "lib/awrence.rb".freeze, "lib/awrence/ext/array/to_camel_keys.rb".freeze, "lib/awrence/ext/hash/to_camel_keys.rb".freeze, "lib/awrence/methods.rb".freeze, "lib/awrence/version.rb".freeze] s.homepage = "https://github.com/technicalpanda/awrence".freeze s.licenses = ["MIT".freeze] s.required_ruby_version = Gem::Requirement.new(">= 3.2".freeze) s.rubygems_version = "3.3.15".freeze s.summary = "Camelize your snake keys when working with JSON APIs".freeze end awrence-4.0.0/VERSION0000644000004100000410000000000615052041536014234 0ustar www-datawww-data4.0.0 awrence-4.0.0/README.md0000644000004100000410000000523115052041536014450 0ustar www-datawww-data# Awrence [![Gem Version](https://badge.fury.io/rb/awrence.svg)](https://badge.fury.io/rb/awrence) ![CI](https://github.com/technicalpanda/awrence/workflows/CI/badge.svg) Have you ever needed to automatically convert Rubyish `snake_case` to JSON-style `camelBack` or `CamelCase` hash keys? Awrence to the rescue! This gem recursively converts all snake_case keys in a hash structure to camelBack or CamelCase. ## Requirements * Ruby >= 3.0 For Ruby < 3.0 use version 2.0.1 ## Installation Add this to your Gemfile: ```ruby gem "awrence" ``` Or install it yourself as: ```bash gem install awrence ``` ## Usage ```ruby my_hash = {"first_key" => 1, "foo_bars" => [{"baz_baz" => "value"}, {"blah_blah" => "value"}]} camel_hash = my_hash.to_camel_keys # => {"FirstKey" => 1, "FooBars" => [{"BazBaz" => "value"}, {"BlahBlah" => "value"}]} # OR camel_hash = my_hash.to_camelback_keys # => {"firstKey" => 1, "fooBars" => [{"bazBaz" => "value"}, {"blahBlah" => "value"}]} ``` Awrence works on either string keys or symbolized keys. It has no dependencies, as it has its own `camelize` method lifted out of ActiveSupport. ### Acronyms You can set acronyms that Awrence will now check against when converting the keys. ```ruby Awrence.acronyms = { "url" => "URL", "id" => "ID" } my_hash = { "user_url" => "http://a.com", "user_id" => 2 } camel_hash = my_hash.to_camel_keys # => { "UserURL" => "http://a.com", "UserID" => 2 } camel_hash = my_hash.to_camelback_keys # => { "userURL" => "http://a.com", "userID" => 2 } ``` The acronym will be ignored when it's the first word and `to_camelback_keys` is called. ```ruby Awrence.acronyms = { "id" => "ID" } my_hash = { "id" => 2 } camel_hash = my_hash.to_camel_keys # => { "ID" => 2 } camel_hash = my_hash.to_camelback_keys # => { "id" => 2 } ``` ## Limitations * There is no `Hash#to_camel_keys!` form. # Going the other way If you've already got `CamelCase` and need to `snake_case` it, you are encouraged to try the [Plissken](http://github.com/technicalpanda/plissken) gem. ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/technicalpanda/awrence. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. ## License The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). ## Code of Conduct Everyone interacting with this project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/technicalpanda/awrence/blob/main/CODE_OF_CONDUCT.md).