-
-
Save leandromoreira/e262c0758caab6d8c5650676cda171ff to your computer and use it in GitHub Desktop.
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
// _ is an instance of Ramda | |
// $ is an instance of jQuery | |
// This is an app extracted from the book | |
// mostly-adequate-guide/content/ch6.html | |
// it's query the flickr API's and mount | |
// a lots of images on the page | |
// IMPURE functions ahead | |
// takes a function (callback) and returns another | |
// function which takes an url | |
var getJSON = _.curry(function(callback, url) { | |
$.getJSON(url, callback); | |
}) | |
// takes a selector and returns a functions | |
// which you can pass an html | |
var setHtml = _.curry(function(sel, html) { | |
$(sel).html(html); | |
}) | |
// PURE, good and reliable functions ahead | |
// just create an image for a given url | |
var img = function(url) { | |
return $('<img />', { | |
src: url, | |
}); | |
}; | |
// just create an url for a given query | |
var url = function(t) { | |
return 'http://api.flickr.com/services/feeds/photos_public.gne?tags=' + | |
t + '&format=json&jsoncallback=?'; | |
}; | |
// _.prop is a curried function which takes | |
// a property name and then an object | |
// ex: var getPrice = _.prop('price') | |
// getPrice({price: 3, x: "y"}) // returns 3 | |
// takes an object extracts its media and | |
// then from the media it extracts the m property | |
var mediaUrl = _.compose(_.prop('m'), _.prop('media')); | |
// it takes a media url and create an image | |
var mediaToImg = _.compose(img, mediaUrl); | |
//it takes an object and extracts items from it | |
// map each one to an image | |
var images = _.compose(_.map(mediaToImg), _.prop('items')); | |
// it takes images and set | |
// them on the document.body | |
var renderImages = _.compose(setHtml('body'), images); | |
// given a string, it'll create an url and | |
// gets the json and render the images | |
var app = _.compose(getJSON(renderImages), url); | |
// given a query it'll create a page | |
app('cats'); | |
// I strongly recommend you to read again from app to mediaUrl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment