Skip to content

Commit 9c0b980

Browse files
committed
Intial commit
1 parent 7613c4e commit 9c0b980

File tree

11 files changed

+188
-0
lines changed

11 files changed

+188
-0
lines changed

Gemfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
4+
5+
ruby '>= 3.0'
6+
7+
gem 'faraday'
8+
gem 'sinatra'
9+
gem 'webrick'

Gemfile.lock

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
faraday (2.7.4)
5+
faraday-net_http (>= 2.0, < 3.1)
6+
ruby2_keywords (>= 0.0.4)
7+
faraday-net_http (3.0.2)
8+
mustermann (3.0.0)
9+
ruby2_keywords (~> 0.0.1)
10+
rack (2.2.7)
11+
rack-protection (3.0.6)
12+
rack
13+
ruby2_keywords (0.0.5)
14+
sinatra (3.0.6)
15+
mustermann (~> 3.0)
16+
rack (~> 2.2, >= 2.2.4)
17+
rack-protection (= 3.0.6)
18+
tilt (~> 2.0)
19+
tilt (2.1.0)
20+
webrick (1.8.1)
21+
22+
PLATFORMS
23+
arm64-darwin-22
24+
25+
DEPENDENCIES
26+
faraday
27+
sinatra
28+
webrick
29+
30+
RUBY VERSION
31+
ruby 3.2.2p53
32+
33+
BUNDLED WITH
34+
2.4.13

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# RPF apprentice software engineer challenge
2+
3+
This is a little application, written in [Ruby](https://ruby-lang.org), using the [Sinatra](https://sinatrarb.com/) framework. It allows users to see the whereabouts of the International Space Station, using the [OpenNotify APIs](http://api.open-notify.org/).
4+
5+
## What we're expecting
6+
7+
* You should spend 2-3 hours on this challenge, and attempt all three tasks.
8+
* Please send back your code by the end of Sunday 4th June, either as a link to a Github/Gitlab repo, or a zip/tar of your code.
9+
10+
We understand that you will have other commitments and time constraints, please let us know as soon as possible if you will be unable to complete this task by 4th June, so we are able to make allowances.
11+
12+
It is important to attempt the tasks, even if you don't finish them.
13+
14+
If you need help with the task, for whatever reason, please drop us an email and we will do our best to assist.
15+
16+
## Tasks
17+
18+
1. Build a page to show the current astronauts that are in space, using the `OpenNotify#astros` method.
19+
2. Style the application using HTML and CSS to allow the information to be presented clearly.
20+
3. Add new endpoints, or modify the existing ones, to return the ISS position data as JSON, instead of an HTML page.
21+
e.g. `http://localhost:4567/iss_position.json` should return JSON in the following format:
22+
23+
```json
24+
{"iss_position": {"longitude": "-3.4941", "latitude": "-37.5113"}, "timestamp": 1684502291, "message": "success"}
25+
```
26+
27+
## Getting started
28+
29+
Firstly you'll want to download and unzip the code into a directory or folder of your choice.
30+
31+
Next if you've not already got Ruby, you'll need to [install it](https://www.ruby-lang.org/en/documentation/installation/). There are a number of different ways to do it depending on your operating system. **NB** Apple Macs already have it installed, but the version is quite old, so we'd recommend installing a more up-to-date one.
32+
33+
Once you've installed ruby, you can install the dependencies for this project in a terminal. You should open a terminal in the directory or folder where your code has been checked out.
34+
35+
```shell
36+
bundle config path vendor/bundle
37+
bundle install
38+
```
39+
40+
Then to start it:
41+
42+
```shell
43+
bundle exec ruby app.rb
44+
```
45+
46+
and you should be able to see it at http://localhost:4567. **NB** When you make changes to your code, you'll need to stop the app, using `ctrl+c` and then restart it using the same command above.
47+
48+
## Layout
49+
50+
* `app.rb` - Our Sinatra application
51+
* `open_notify.rb` - OpenNotify library
52+
* `Gemfile` - list of Ruby libraries (gems) that our app depends on.
53+
* `Gemfile.lock` - automatically generated "lockfile" for our dependencies.
54+
* `public/` - directory where "static" assets, e.g. images, CSS files can be kept.
55+
* `data/` - directory with static data from OpenNotify to allow development without internet access.
56+
* `views/` - directory containg the views (templates) for use by Sinatra. These are all using `erb`.
57+
58+
## Useful documentation
59+
60+
* [Getting started with Sinatra](https://sinatrarb.com/intro.html)
61+
* Various useful Ruby classes and modules:
62+
* [Array](https://ruby-doc.org/3.2.2/Array.html)
63+
* [Hash](https://ruby-doc.org/3.2.2/Hash.html)
64+
* [String](https://ruby-doc.org/3.2.2/String.html)
65+
* [Comparable](https://ruby-doc.org/3.2.2/Comparable.html) (handy if you're sorting things)
66+
* [Enumerable](https://ruby-doc.org/3.2.2/Enumerable.html) (full of useful methods that both Hash and Array can use)
67+
* [ERB Templating](https://ruby-doc.org/3.2.2/stdlibs/erb/ERB.html)
68+

app.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
# myapp.rb
4+
require 'sinatra'
5+
require_relative 'open_notify'
6+
7+
# Allow our templates in views/ to end in `.html.erb`
8+
Tilt.register Tilt::ERBTemplate, 'html.erb'
9+
10+
set :layout, 'views/layout.html.erb'
11+
12+
get '/' do
13+
erb :index
14+
end
15+
16+
get '/position' do
17+
iss_now = OpenNotify.iss_now
18+
19+
erb :position, locals: { data: iss_now }
20+
end

data/astros.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"message": "success", "number": 10, "people": [{"craft": "ISS", "name": "Sergey Prokopyev"}, {"craft": "ISS", "name": "Dmitry Petelin"}, {"craft": "ISS", "name": "Frank Rubio"}, {"craft": "Shenzhou 15", "name": "Fei Junlong"}, {"craft": "Shenzhou 15", "name": "Deng Qingming"}, {"craft": "Shenzhou 15", "name": "Zhang Lu"}, {"craft": "ISS", "name": "Stephen Bowen"}, {"craft": "ISS", "name": "Warren Hoburg"}, {"craft": "ISS", "name": "Sultan Alneyadi"}, {"craft": "ISS", "name": "Andrey Fedyaev"}]}

data/iss-now.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"iss_position": {"longitude": "-3.4941", "latitude": "-37.5113"}, "timestamp": 1684502291, "message": "success"}

open_notify.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
require 'json'
4+
require 'faraday'
5+
6+
# Fetch data from the OpenNotify API service at
7+
# http://api.open-notify.org/
8+
#
9+
# To allow this to work without internet access, the read data method just
10+
# loads and parses the data
11+
module OpenNotify
12+
BASE_DIR = __dir__
13+
14+
def iss_now
15+
fetch_data(api: 'iss-now')
16+
end
17+
18+
def astros
19+
fetch_data(api: 'astros')
20+
end
21+
22+
# This method would really fetch data from the live API, but for our
23+
# purposes, a static file makes sense.
24+
def fetch_data(api:)
25+
if ENV['USE_LIVE_DATA'] == 'yes'
26+
conn = Faraday.new('http://api.open-notify.org/') do |f|
27+
f.response :json
28+
end
29+
30+
conn.get("#{api}.json").body
31+
else
32+
filepath = File.join(BASE_DIR, 'data', "#{api}.json")
33+
JSON.parse(File.read(filepath))
34+
end
35+
end
36+
37+
module_function :iss_now, :astros, :fetch_data
38+
end

public/favicon.ico

49.9 KB
Binary file not shown.

views/index.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<h1>RPF software engineer apprentice challenge 🚀</h1>
2+
<ul>
3+
<li><a href='position'>Where is the ISS? 🛰️</a></li>
4+
</ul>

views/layout.html.erb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>RPF apprenticeship technical task</title>
5+
<link rel="shortcut icon" href="/favicon.ico">
6+
</head>
7+
<body>
8+
<%= yield %>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)