Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add use_camel_case option #149

Merged
merged 2 commits into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Change Log

## Unreleased
### Added
* Added `:use_camel_case` option to client. [#142](https://github.com/contentful/contentful.rb/issues/142)

## 2.1.3
### Fixed
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ tree is allowed to resolved before falling back to `Link` objects). This include
on resources with circular dependencies. Defaults to 20. _Note_: If you're using something like `Rails::cache` it's advisable to considerably lower this value
(around 5 has proven to be a good compromise - but keep it higher or equal than your maximum API-level include parameter if you need the entire tree resolution).

### :use_camel_case

When doing the v2 upgrade, all keys and accessors were changed to always use `snake_case`. This option introduces the ability to use `camelCase` for keys and method
accessors. This is very useful for isomorphic applications.

### Proxy example

```ruby
Expand Down
4 changes: 2 additions & 2 deletions lib/contentful/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ def create_files!
is_localized = file_json.keys.none? { |f| %w(fileName contentType details url).include? f }
if is_localized
locales.each do |locale|
@fields[locale][:file] = ::Contentful::File.new(file_json[locale.to_s] || {})
@fields[locale][:file] = ::Contentful::File.new(file_json[locale.to_s] || {}, @configuration)
end
else
@fields[internal_resource_locale][:file] = ::Contentful::File.new(file_json)
@fields[internal_resource_locale][:file] = ::Contentful::File.new(file_json, @configuration)
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/contentful/base_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def initialize(item, configuration = {}, _localized = false, _includes = [], dep
@raw = item
@default_locale = configuration[:default_locale]
@depth = depth
@sys = hydrate_sys
@configuration = configuration
@sys = hydrate_sys

define_sys_methods!
end
Expand Down Expand Up @@ -69,7 +69,7 @@ def hydrate_sys
elsif %w(createdAt updatedAt deletedAt).include?(k)
v = DateTime.parse(v)
end
result[Support.snakify(k).to_sym] = v
result[Support.snakify(k, @configuration[:use_camel_case]).to_sym] = v
end
result
end
Expand All @@ -86,7 +86,7 @@ def internal_resource_locale

def build_link(item)
require_relative 'link'
::Contentful::Link.new(item)
::Contentful::Link.new(item, @configuration)
end
end
end
2 changes: 2 additions & 0 deletions lib/contentful/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Client
max_rate_limit_retries: 1,
max_rate_limit_wait: 60,
max_include_resolution_depth: 20,
use_camel_case: false,
application_name: nil,
application_version: nil,
integration_name: nil,
Expand Down Expand Up @@ -70,6 +71,7 @@ def self.get_http(url, query, headers = {}, proxy = {})
# @option given_configuration [Number] :max_rate_limit_retries
# @option given_configuration [Number] :max_rate_limit_wait
# @option given_configuration [Number] :max_include_resolution_depth
# @option given_configuration [Boolean] :use_camel_case
# @option given_configuration [Boolean] :gzip_encoded
# @option given_configuration [Boolean] :raw_mode
# @option given_configuration [false, ::Logger] :logger
Expand Down
2 changes: 2 additions & 0 deletions lib/contentful/content_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def field_for(field_id)
fields.detect { |f| Support.snakify(f.id) == Support.snakify(field_id) }
end

alias displayField display_field

protected

def repr_name
Expand Down
6 changes: 4 additions & 2 deletions lib/contentful/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def coerce(field_id, value, includes)
return build_nested_resource(value, includes) if Support.link?(value)
return coerce_link_array(value, includes) if Support.link_array?(value)

content_type = ContentTypeCache.cache_get(sys[:space].id, sys[:content_type].id)
content_type_key = Support.snakify('contentType', @configuration[:use_camel_case])
content_type = ContentTypeCache.cache_get(sys[:space].id, sys[content_type_key.to_sym].id)

unless content_type.nil?
content_type_field = content_type.field_for(field_id)
Expand Down Expand Up @@ -76,7 +77,8 @@ def known_contentful_object?(object)
protected

def repr_name
"#{super}[#{sys[:content_type].id}]"
content_type_key = Support.snakify('contentType', @configuration[:use_camel_case]).to_sym
"#{super}[#{sys[content_type_key].id}]"
end
end
end
13 changes: 8 additions & 5 deletions lib/contentful/fields_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def raw_with_links
links = fields.keys.select { |property| known_link?(property) }
processed_raw = raw.clone
raw['fields'].each do |k, v|
processed_raw['fields'][k] = links.include?(Support.snakify(k).to_sym) ? send(Support.snakify(k)) : v
links_key = Support.snakify(k, @configuration[:use_camel_case])
processed_raw['fields'][k] = links.include?(links_key.to_sym) ? send(links_key) : v
end

processed_raw
Expand All @@ -91,17 +92,19 @@ def hydrate_fields(includes)
raw['fields'].each do |name, locales|
locales.each do |loc, value|
result[loc] ||= {}
result[loc][Support.snakify(name).to_sym] = coerce(
Support.snakify(name),
name = Support.snakify(name, @configuration[:use_camel_case])
result[loc][name.to_sym] = coerce(
name,
value,
includes
)
end
end
else
raw['fields'].each do |name, value|
result[locale][Support.snakify(name).to_sym] = coerce(
Support.snakify(name),
name = Support.snakify(name, @configuration[:use_camel_case])
result[locale][name.to_sym] = coerce(
name,
value,
includes
)
Expand Down
20 changes: 14 additions & 6 deletions lib/contentful/file.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
module Contentful
# An Assets's file info
class File
attr_reader :file_name, :content_type, :details, :url
def initialize(json)
@file_name = json.fetch('fileName', nil)
@content_type = json.fetch('contentType', nil)
@details = json.fetch('details', nil)
@url = json.fetch('url', nil)
def initialize(json, configuration)
@configuration = configuration

define_fields!(json)
end

private

def define_fields!(json)
json.each do |k, v|
define_singleton_method Support.snakify(k, @configuration[:use_camel_case]) do
v
end
end
end
end
end
5 changes: 4 additions & 1 deletion lib/contentful/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ class << self
# Transforms CamelCase into snake_case (taken from zucker)
#
# @param [String] object camelCaseName
# @param [Boolean] skip if true, skips returns original object
#
# @return [String] snake_case_name
def snakify(object)
def snakify(object, skip = false)
return object if skip

String(object)
.gsub(/::/, '/')
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
Expand Down
9 changes: 9 additions & 0 deletions spec/asset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,13 @@
}
end
end

describe 'camelCase' do
it 'properties now are accessed with camelcase' do
vcr('asset') {
asset = create_client(use_camel_case: true).asset('nyancat')
expect(asset.file.fileName).to eq 'Nyan_cat_250px_frame.png'
}
end
end
end
10 changes: 10 additions & 0 deletions spec/content_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@
expect(content_type).to respond_to :display_field
end
end

describe 'camel case' do
it 'supports camel case' do
vcr('content_type') {
content_type = create_client(use_camel_case: true).content_type 'cat'

expect(content_type.displayField).to eq 'name'
}
end
end
end
10 changes: 10 additions & 0 deletions spec/deleted_asset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@
expect(deleted_asset.created_at).to be_a DateTime
end
end

describe 'camel case' do
it 'supports camel case' do
vcr('sync_deleted_asset') {
deleted_asset = create_client(use_camel_case: true).sync(initial: true, type: 'DeletedAsset').first_page.items[0]

expect(deleted_asset.createdAt).to be_a DateTime
}
end
end
end
10 changes: 10 additions & 0 deletions spec/deleted_entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@
expect(deleted_entry.created_at).to be_a DateTime
end
end

describe 'camel case' do
it 'supports camel case' do
vcr('sync_deleted_entry') {
deleted_entry = create_client(use_camel_case: true).sync(initial: true, type: 'DeletedEntry').first_page.items[0]

expect(deleted_entry.createdAt).to be_a DateTime
}
end
end
end
11 changes: 11 additions & 0 deletions spec/entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,15 @@ def test_dump(nyancat)
}
end
end

describe 'camel case' do
it 'supports camel case' do
vcr('entry') {
entry = create_client(use_camel_case: true).entry 'nyancat'

expect(entry.bestFriend.name).to eq 'Happy Cat'
expect(entry.createdAt).to be_a DateTime
}
end
end
end
10 changes: 10 additions & 0 deletions spec/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@
expect(file.details).to be_instance_of Hash
end
end

describe 'camel case' do
it 'supports camel case' do
vcr('asset') {
file = create_client(use_camel_case: true).asset('nyancat').file
expect(file.contentType).to eq 'image/png'
expect(file.fileName).to eq 'Nyan_cat_250px_frame.png'
}
end
end
end
9 changes: 9 additions & 0 deletions spec/link_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,13 @@
end
end
end

describe 'camel case' do
it 'supports camel case' do
vcr('entry') {
space_link = create_client(use_camel_case: true).entry('nyancat').space
expect(space_link.linkType).to eq 'Space'
}
end
end
end