Skip to content

Commit

Permalink
Merge pull request #66 from contentful/parse-nested-locales-in-assets
Browse files Browse the repository at this point in the history
Parse nested locales in `AssetFields`
  • Loading branch information
pxlpnk committed Aug 25, 2015
2 parents eec9550 + 63c9c1c commit bf8d57d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
17 changes: 17 additions & 0 deletions lib/contentful/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions lib/contentful/resource/asset_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 1 addition & 14 deletions lib/contentful/resource/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions spec/sync_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bf8d57d

Please sign in to comment.