Skip to content

Commit

Permalink
Parse nested locales in AssetFields
Browse files Browse the repository at this point in the history
Fixes #63
  • Loading branch information
neonichu committed Aug 24, 2015
1 parent eec9550 commit f0cd256
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/contentful/resource/asset_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 f0cd256

Please sign in to comment.