📜 ⬆️ ⬇️

Ruby on Rails and Yandex. Photos API: show the latest photos on the home page

image
For a couple of years I have been using the Yandex. Photos service for storing all my photos, and recently there was a desire on my homepage to display the last n photos uploaded to Yandex. Photos, thereby killing several birds with one stone:


No sooner said than done. It turned out that the Photos provide a fairly convenient API via the AtomPub protocol. Because I planned to make a page on the Ruby on Rails platform, after a brief search I found a good library for working with feedzirra . The library was installed in a completely standard way:
gem sources -a gems.github.com gem install pauldix-feedzirra
  1. gem sources -a gems.github.com gem install pauldix-feedzirra
  2. gem sources -a gems.github.com gem install pauldix-feedzirra

and further in ruby ​​it is enough to register its use:
  1. require 'feedzirra'

It's time to explore the API provided.

Having studied the API provided by Yandex, it turned out that the list of the photos recently published by the user can be obtained at the following URL:
api-fotki.yandex.ru/api/users < _ > /photos/published/?limit= < __ >

besides authorization for this operation is not required. The documentation was an example of the output XML, but it turned out that it does not quite correspond to reality. The method of close debag problem was solved.

So I created some kind of wrapper class for the image.
  1. class ImageBundle
  2. attr_accessor: url,: album_link,: title,: xxl_url
  3. end

here url is the address of a preview of a square-shaped picture, album_link is a link to an album, title is the name of a picture, xxl_url is an address of a large-format picture.
')
Further in the controller I filled the collection of the received data.
  1. feed = Feedzirra :: Feed.fetch_and_parse ( "http://api-fotki.yandex.ru/api/users/ligrimp/photos/published/?limit=35" )
  2. @images = Array. new
  3. for image in feed.entries
  4. image_url = image.links [3]
  5. i = ImageBundle. new
  6. i.xxl_url = image_url.clone
  7. image_url [ "XL" ] = "XXS"
  8. i.url = image_url
  9. i.album_link = image.links [2]
  10. i.title = image.title
  11. @images << i
  12. end


The output to the UI turned out to be a matter of technique, using HAML for markup and iLoad , I got something like the following:
  1. - for image in @images
  2. % a {: href => image.xxl_url,: target => "_blank" ,: rel => "iLoad :: Yandex.fotki",: title => image.title}
  3. % img {: src => image.url,: title => image.title}

Source: https://habr.com/ru/post/88511/


All Articles