Skip to content

Instantly share code, notes, and snippets.

@CedrikHeusser
Last active April 24, 2024 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save CedrikHeusser/3470dd2f183cda9a2e5218fdd006eec0 to your computer and use it in GitHub Desktop.
Save CedrikHeusser/3470dd2f183cda9a2e5218fdd006eec0 to your computer and use it in GitHub Desktop.
Practice

anynines worksample exercises


Please use git for version control

Create a local git repo and commit every exercise to this repo.

0. Warmup

If necessary install ruby and then:

  • Write a Hello World! example
  • Write the Hello World example using a class. Use a class method (instead of an instance method) greeting to output Hello, World!:
[classname].greeting
-> "Hello, World!"

1. Download a post

JSONPlaceholder - Fake online REST API for developers is a free online REST API that you can use whenever you need some fake data.

Write a Ruby class to download a single post from JSONPlaceholder. The usage of your class should look like this:

ruby_downloader = RubyDownloader.new
ruby_downloader.show_post(3)

The commands above should output the post to stdout in the following format:

<post-title>

<post-body>

2. Save the post

Refactor your class so that it saves the downloaded post to a specific directory. It should be possible to use it like this:

ruby_downloader = RubyDownloader.new('/tmp')
ruby_downloader.download_post(3)

After the execution, there should be a post3.txt file in the /tmp directory.

Note: It doesn't have to output the post in stdout, instead it should write the same string into the file.

3. Make it executable in bash

Refactor the script to be usable in bash. It should be possible to call it to show a post and to download a post.

$ ./ruby_downloader.rb show 3
<post-title>

<post-body>
$ ./ruby_downloader.rb download 3 "tmp"
$ Saved post to /tmp/post3.txt !

4. Working with bash

Write a bash script that uses your ruby script to download a range of posts to ./posts.

./download.sh 1 100

Also generate with the bash script a index.csv file, that contains the post ID, the post title, the post body and the full path to the file.

1;<post-title>;<post-body>;/Users/.../posts/post1.txt
...
100;<post-title>;<post-body>;/Users/.../posts/post100.txt

5. Retroperspective

Think about your implementation.

Let's say you had more time and we were in a production environment ...

What would you improve or change ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment