-
-
Save neonichu/17a987aeeb256d4bf6f3 to your computer and use it in GitHub Desktop.
How to parse JSON from Contentful webhook responses with contentful.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"sys": { | |
"type": "Entry", | |
"id": "cat", | |
"space": {"sys": {"type": "Link", "linkType": "Space", "id": "example"}}, | |
"contentType": {"sys": {"type": "Link", "linkType": "ContentType", "id": "cat"}}, | |
"createdAt": "2013-03-26T00:13:37.123Z", | |
"updatedAt": "2013-03-26T00:13:37.123Z", | |
"revision": 1 | |
}, | |
"fields": { | |
"name": {"en-US": "Nyan cat"}, | |
"color": {"en-US": "Rainbow"}, | |
"nyan": {"en-US": true}, | |
"birthday": {"en-US": "2011-04-02T00:00:00.000Z"}, | |
"diary": {"en-US": "Nyan cat has an epic rainbow trail."}, | |
"likes": {"en-US": ["rainbows", "fish"]}, | |
"bestFriend": {"en-US": {"type": "Link", "linkType": "Entry", "id": "happycat"}} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"sys": { | |
"type": "DeletedEntry", | |
"id": "cat", | |
"space": {"sys": {"type": "Link", "linkType": "Space", "id": "example"}}, | |
"createdAt": "2013-03-26T00:13:37.123Z", | |
"updatedAt": "2013-03-26T00:13:37.123Z", | |
"revision": 1 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"sys":{"space":{"sys":{"type":"Link","linkType":"Space","id":"rrtpaxmsx84m"}},"type":"Asset","id":"6h6kWCpTq08GaAoqWkuGEO","revision":1,"createdAt":"2015-07-28T13:12:20.591Z","updatedAt":"2015-07-28T13:12:20.591Z"},"fields":{"file":{"en-US":{"fileName":"partners-top-11da21b6e1b4196ba8b690edd1a8a279.jpg","contentType":"image/jpeg","details":{"image":{"width":1733,"height":1155},"size":295655},"url":"//images.contentful.com/rrtpaxmsx84m/6h6kWCpTq08GaAoqWkuGEO/b57b39d5ae5080631ba0f71fbfa1427d/partners-top-11da21b6e1b4196ba8b690edd1a8a279.jpg"}},"title":{"en-US":"partners-top-11da21b6e1b4196ba8b690edd1a8a279"},"description":{"en-US":"f"}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'contentful' | |
class DummyResponse | |
def initialize(data) | |
@data = data | |
end | |
def headers | |
{} | |
end | |
def status | |
200 | |
end | |
def to_s | |
@data | |
end | |
end | |
def parse_response(json) | |
Contentful::Response.new(DummyResponse.new(json)) | |
end | |
#p parse_response(File.read('cf-1.json')).object | |
#p parse_response(File.read('cf-2.json')).object | |
client = Contentful::Client.new( | |
access_token: 'b4c0n73n7fu1', | |
space: 'cfexampleapi' | |
) | |
#entry = parse_response(File.read('cf-1.json')) | |
#builder = Contentful::ResourceBuilder.new(client, entry) | |
#builder.instance_variable_set(:@nested_locales, true) | |
#p builder.run | |
asset = parse_response(File.read('cf-asset-1.json')) | |
builder = Contentful::ResourceBuilder.new(client, asset) | |
#builder.instance_variable_set(:@nested_locales, true) | |
p builder.run.file.url |
Nice. So basically DummyResponse
mimics an API response so you can use the normal Contentful response / resource methods? This seems like the most expedient solution for proper caching.
@sebbean Sorry, I don't work at Contentful anymore at this point and can't say if this still works. Probably best to email support.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Parsing assets doesn't completely work, yet, see contentful/contentful.rb#63