Skip to content

Instantly share code, notes, and snippets.

@neonichu
Last active March 15, 2017 20:47
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neonichu/17a987aeeb256d4bf6f3 to your computer and use it in GitHub Desktop.
Save neonichu/17a987aeeb256d4bf6f3 to your computer and use it in GitHub Desktop.
How to parse JSON from Contentful webhook responses with contentful.rb
{
"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"}}
}
}
{
"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
}
}
{"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"}}}
#!/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
@neonichu
Copy link
Author

Parsing assets doesn't completely work, yet, see contentful/contentful.rb#63

@edtjones
Copy link

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
Copy link

sebbean commented Mar 15, 2017

@neonichu and / or @edtjones about to dive into the solving proper caching for the current version of contentful_model / contentful_rails. any idea if this stuff still works?

@neonichu
Copy link
Author

@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