From f0cd2561b6672ccab771004a2312cc1b6885cb74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boris=20Bu=CC=88gling?= Date: Mon, 24 Aug 2015 17:28:55 +0200 Subject: [PATCH 1/3] Parse nested locales in `AssetFields` Fixes #63 --- lib/contentful/resource/asset_fields.rb | 19 ++++++++++++++++--- spec/sync_spec.rb | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lib/contentful/resource/asset_fields.rb b/lib/contentful/resource/asset_fields.rb index 8142948..9fe0dfe 100644 --- a/lib/contentful/resource/asset_fields.rb +++ b/lib/contentful/resource/asset_fields.rb @@ -12,14 +12,27 @@ 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 + + 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 inspect(info = nil) 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 From 45c2e39ce5f1f3350d2382d1f4aaeae29bf4112a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boris=20Bu=CC=88gling?= Date: Mon, 24 Aug 2015 17:32:13 +0200 Subject: [PATCH 2/3] Changelog entry for #66 --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) 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) From 63c9c1c589ee8eed9f1ba2be97fd6ce58959eb5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boris=20Bu=CC=88gling?= Date: Tue, 25 Aug 2015 11:48:37 +0200 Subject: [PATCH 3/3] Deduplicate code for handling localized resources --- lib/contentful/resource.rb | 17 +++++++++++++++++ lib/contentful/resource/asset_fields.rb | 16 ++-------------- lib/contentful/resource/fields.rb | 15 +-------------- 3 files changed, 20 insertions(+), 28 deletions(-) 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 9fe0dfe..669c594 100644 --- a/lib/contentful/resource/asset_fields.rb +++ b/lib/contentful/resource/asset_fields.rb @@ -19,20 +19,8 @@ def fields(wanted_locale = default_locale) def initialize(object, *) super - @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 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