awrence-1.1.1/0000755000175000017500000000000013750471770012175 5ustar pravipraviawrence-1.1.1/VERSION0000644000175000017500000000000613750471770013241 0ustar pravipravi1.1.1 awrence-1.1.1/lib/0000755000175000017500000000000013750471770012743 5ustar pravipraviawrence-1.1.1/lib/awrence.rb0000644000175000017500000000040113750471770014707 0ustar pravipravi# 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-1.1.1/lib/awrence/0000755000175000017500000000000013750471770014367 5ustar pravipraviawrence-1.1.1/lib/awrence/methods.rb0000644000175000017500000000325013750471770016357 0ustar pravipravi# 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) case value when Array value.map { |v| to_camelback_keys(v) } when Hash Hash[value.map { |k, v| [camelize_key(k, false), to_camelback_keys(v)] }] else value end 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) case value when Array value.map { |v| to_camel_keys(v) } when Hash Hash[value.map { |k, v| [camelize_key(k), to_camel_keys(v)] }] else value end end private def camelize_key(key, first_upper = true) if key.is_a? Symbol camelize(key.to_s, first_upper).to_sym elsif key.is_a? String camelize(key, 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]+)/) str = gsubbed(str, %r{/([^/]*)}, "::") str 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 = str.gsub(pattern) do extra + (Awrence.acronyms[Regexp.last_match(1)] || Regexp.last_match(1).capitalize) end str end end end awrence-1.1.1/lib/awrence/ext/0000755000175000017500000000000013750471770015167 5ustar pravipraviawrence-1.1.1/lib/awrence/ext/array/0000755000175000017500000000000013750471770016305 5ustar pravipraviawrence-1.1.1/lib/awrence/ext/array/to_camel_keys.rb0000644000175000017500000000011213750471770021442 0ustar pravipravi# frozen_string_literal: true class Array include Awrence::Methods end awrence-1.1.1/lib/awrence/ext/hash/0000755000175000017500000000000013750471770016112 5ustar pravipraviawrence-1.1.1/lib/awrence/ext/hash/to_camel_keys.rb0000644000175000017500000000011113750471770021246 0ustar pravipravi# frozen_string_literal: true class Hash include Awrence::Methods end awrence-1.1.1/lib/awrence/version.rb0000644000175000017500000000025413750471770016402 0ustar pravipravi# frozen_string_literal: true module Awrence version_file = File.join(File.dirname(__FILE__), "../../VERSION") VERSION = File.read(version_file).split("\n").first end awrence-1.1.1/awrence.gemspec0000644000175000017500000000460413750471770015172 0ustar pravipravi######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- # stub: awrence 1.1.1 ruby lib Gem::Specification.new do |s| s.name = "awrence".freeze s.version = "1.1.1" s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.require_paths = ["lib".freeze] s.authors = ["Dave Hrycyszyn".freeze, "Stuart Chinery".freeze] s.date = "2020-02-01" 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 = ["dhrycyszyn@zonedigital.com".freeze, "stuart.chinery@gmail.com".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/futurechimp/awrence".freeze s.licenses = ["MIT".freeze] s.rubygems_version = "3.2.0.rc.1".freeze s.summary = "Camelize your snake keys when working with JSON APIs".freeze if s.respond_to? :specification_version then s.specification_version = 4 end if s.respond_to? :add_runtime_dependency then s.add_development_dependency(%q.freeze, ["~> 11.1"]) s.add_development_dependency(%q.freeze, ["~> 5.14"]) s.add_development_dependency(%q.freeze, ["~> 0.1"]) s.add_development_dependency(%q.freeze, ["~> 0.3"]) s.add_development_dependency(%q.freeze, ["~> 1.4"]) s.add_development_dependency(%q.freeze, ["~> 13.0"]) s.add_development_dependency(%q.freeze, ["~> 0.79"]) else s.add_dependency(%q.freeze, ["~> 11.1"]) s.add_dependency(%q.freeze, ["~> 5.14"]) s.add_dependency(%q.freeze, ["~> 0.1"]) s.add_dependency(%q.freeze, ["~> 0.3"]) s.add_dependency(%q.freeze, ["~> 1.4"]) s.add_dependency(%q.freeze, ["~> 13.0"]) s.add_dependency(%q.freeze, ["~> 0.79"]) end end awrence-1.1.1/MIT-LICENSE0000644000175000017500000000204213750471770013627 0ustar pravipraviCopyright (c) 2020 Dave Hrycyszyn 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-1.1.1/README.md0000644000175000017500000000515313750471770013460 0ustar pravipravi# Awrence [![Gem Version](https://badge.fury.io/rb/awrence.svg)](https://badge.fury.io/rb/awrence) 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. ## 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/futurechimp/plissken) gem. ## Contributing to awrence * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet. * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it. * Fork the project. * Start a feature/bugfix branch. * Commit and push until you are happy with your contribution. * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally. * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it. # Copyright Copyright (c) 2017 Dave Hrycyszyn. See LICENSE.txt for further details. awrence-1.1.1/Rakefile0000644000175000017500000000112113750471770013635 0ustar pravipravi# frozen_string_literal: true begin require "bundler/setup" rescue LoadError puts "You must `gem install bundler` and `bundle install` to run rake tasks" end require "rdoc/task" RDoc::Task.new(:rdoc) do |rdoc| rdoc.rdoc_dir = "rdoc" rdoc.title = "Awrence" rdoc.options << "--line-numbers" rdoc.rdoc_files.include("README.md") rdoc.rdoc_files.include("lib/**/*.rb") end require "bundler/gem_tasks" require "rake/testtask" Rake::TestTask.new(:test) do |t| t.libs << "lib" t.libs << "test" t.pattern = "test/**/*_test.rb" t.verbose = false end task default: :test