diff --git a/CHANGELOG.md b/CHANGELOG.md index 2847ef5..401e1dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Change Log ## Unreleased +### Fixed +* Parse nested locales in `AssetFields` [#66](https://github.com/contentful/contentful.rb/pull/66) + ### Other * Update http.rb dependency to v0.8.0 * Fix typo in service unavailable error message [#61](https://github.com/contentful/contentful.rb/pull/61) diff --git a/lib/contentful/resource.rb b/lib/contentful/resource.rb index 6cf2050..0372c01 100644 --- a/lib/contentful/resource.rb +++ b/lib/contentful/resource.rb @@ -69,6 +69,23 @@ def reload private + def initialize_fields_for_localized_resource(object) + @fields = {} + + if nested_locale_fields? + object['fields'].each do |field_name, nested_child_object| + nested_child_object.each do |object_locale, real_child_object| + @fields[object_locale] ||= {} + @fields[object_locale].merge! extract_from_object( + { field_name => real_child_object }, :fields + ) + end + end + else + @fields[locale] = extract_from_object object['fields'], :fields + end + end + def extract_from_object(object, namespace, keys = nil) if object keys ||= object.keys diff --git a/lib/contentful/resource/asset_fields.rb b/lib/contentful/resource/asset_fields.rb index 8142948..669c594 100644 --- a/lib/contentful/resource/asset_fields.rb +++ b/lib/contentful/resource/asset_fields.rb @@ -12,14 +12,15 @@ module AssetFields file: File } - def fields - @fields[locale] + # Returns all fields of the asset + def fields(wanted_locale = default_locale) + @fields[locale || wanted_locale] end def initialize(object, *) super - @fields = {} - @fields[locale] = extract_from_object object['fields'], :fields + + initialize_fields_for_localized_resource(object) end def inspect(info = nil) diff --git a/lib/contentful/resource/fields.rb b/lib/contentful/resource/fields.rb index 686e13d..0f1bbb6 100644 --- a/lib/contentful/resource/fields.rb +++ b/lib/contentful/resource/fields.rb @@ -26,20 +26,7 @@ def inspect(info = nil) private def extract_fields_from_object!(object) - @fields = {} - - if nested_locale_fields? - object['fields'].each do |field_name, nested_child_object| - nested_child_object.each do |object_locale, real_child_object| - @fields[object_locale] ||= {} - @fields[object_locale].merge! extract_from_object( - { field_name => real_child_object }, :fields - ) - end - end - else - @fields[locale] = extract_from_object object['fields'], :fields - end + initialize_fields_for_localized_resource(object) end module ClassMethods diff --git a/spec/sync_spec.rb b/spec/sync_spec.rb index 7c366da..7a0e63e 100644 --- a/spec/sync_spec.rb +++ b/spec/sync_spec.rb @@ -81,4 +81,20 @@ }} end end + + describe 'Resource parsing' do + it 'will correctly parse the `file` field of an asset' do + sync = create_client.sync(initial: true) + vcr('sync_page') { + asset = sync.first_page.items.select { |item| item.is_a?(Contentful::Asset) }.first + + expect(asset.file.properties[:fileName]).to eq 'doge.jpg' + expect(asset.file.properties[:contentType]).to eq 'image/jpeg' + expect(asset.file.properties[:details]['image']['width']).to eq 5800 + expect(asset.file.properties[:details]['image']['height']).to eq 4350 + expect(asset.file.properties[:details]['size']).to eq 522943 + expect(asset.file.properties[:url]).to eq '//images.contentful.com/cfexampleapi/1x0xpXu4pSGS4OukSyWGUK/cc1239c6385428ef26f4180190532818/doge.jpg' + } + end + end end