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

Parse nested locales in AssetFields #66

Merged
merged 3 commits into from
Aug 25, 2015
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
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