Skip to content

Commit

Permalink
Now negotiate content type via http Accept request header, though sti…
Browse files Browse the repository at this point in the history
…ll only text/html and application/json.

Merge the two previous routes into one and test Sinatra/Rack.accept for preferred type falling back to html.
Close #2
  • Loading branch information
rob-murray committed Jan 31, 2014
1 parent bf34aba commit 8f27a55
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 34 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The only Ruby gems required are... wait... and... applause;

How to use this utility:

Using this could not be simpler, that is the idea... (soon to be even simpler!)
Using this could not be simpler, that is the idea.

1) Obtain an copy of the app file `src/ferver.rb` via `git clone`, `wget` or however you want.

Expand All @@ -63,7 +63,7 @@ ruby run.rb ./
```


** Identify the port used (Hint: see line 4 below) and connect eg: http://localhost:4567/files.html
** Identify the port used (Hint: see line 4 below) and connect eg: http://localhost:4567/files

```bash
Puma 1.6.3 starting...
Expand All @@ -77,9 +77,17 @@ Puma 1.6.3 starting...

Here is how to access the files and file list.

* http://host:port/files.html - will display list of files as html
* http://host:port/files.json - will return list of files in json
* http://host:port/files/:id e.g. http://localhost:4567/files/2 - will initiate download of file id
* http://localhost:4567/files - will display list of files as html
* Passing the header `Accept: application/json` - will return the list of files as json

```bash
curl -i -H "Accept: application/json" http://localhost:4567/files
```

* http://localhost:4567/files/:id e.g. http://localhost:4567/files/2 - will initiate download of file id


**!** Check out the `spec` for how it works really.



Expand Down
32 changes: 20 additions & 12 deletions spec/ferver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
follow_redirect!

expect(last_response).to be_ok
expect(last_request.url).to eq('http://example.org/files.html')
expect(last_request.url).to eq('http://example.org/files')
# this 'http://example.org/' appears to be what Rack test inserts? do I care - im only interested in the /files.html

end
Expand All @@ -40,7 +40,7 @@
File.stubs(:expand_path).returns('/a/path/to/ferver')

Dir.expects(:foreach).with('/a/path/to/ferver').returns([])
get '/files.html'
get '/files'

end

Expand All @@ -49,7 +49,7 @@
Ferver.set :ferver_path, '/foo'

Dir.expects(:foreach).with('/foo').returns([])
get '/files.html'
get '/files'

end

Expand All @@ -65,7 +65,7 @@

it 'will return empty list as html' do

get '/files.html'
get '/files'
expect(last_response).to be_ok

expect(last_response.body).to have_selector("li", :count => 0)
Expand All @@ -76,8 +76,10 @@

it 'will return empty list as json' do

get '/files.json'
get '/files', {}, {"HTTP_ACCEPT" => "application/json" }

expect(last_response).to be_ok
expect(last_response.content_type).to include('application/json')

list = JSON.parse last_response.body

Expand All @@ -99,7 +101,7 @@

it 'will return a list as html' do

get '/files.html'
get '/files'
expect(last_response).to be_ok

expect(last_response.body).to have_selector("li", :count => 2)
Expand All @@ -110,8 +112,10 @@

it 'will return a list as json' do

get '/files.json'
get '/files', {}, {"HTTP_ACCEPT" => "application/json" }

expect(last_response).to be_ok
expect(last_response.content_type).to include('application/json')

list = JSON.parse last_response.body
expect(list.count).to eq(2)
Expand All @@ -121,7 +125,7 @@

it 'will return display filenames in html' do

get '/files.html'
get '/files'

expect(last_response.body).to have_selector("li") do |node|

Expand All @@ -143,7 +147,7 @@

it 'will return only files as html' do

get '/files.html'
get '/files'
expect(last_response).to be_ok

expect(last_response.body).to have_selector("li", :count => 1)
Expand All @@ -155,8 +159,10 @@

it 'will return only files as json' do

get '/files.json'
get '/files', {}, {"HTTP_ACCEPT" => "application/json" }

expect(last_response).to be_ok
expect(last_response.content_type).to include('application/json')

list = JSON.parse last_response.body
expect(list.count).to eq(1)
Expand All @@ -177,7 +183,7 @@

it 'will only list files as html' do

get '/files.html'
get '/files'
expect(last_response).to be_ok

expect(last_response.body).to have_selector("li", :count => 1)
Expand All @@ -188,8 +194,10 @@

it 'will only list files as json' do

get '/files.json'
get '/files', {}, {"HTTP_ACCEPT" => "application/json" }

expect(last_response).to be_ok
expect(last_response.content_type).to include('application/json')

list = JSON.parse last_response.body
expect(list.count).to eq(1)
Expand Down
32 changes: 15 additions & 17 deletions src/ferver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,28 @@ class Ferver < Sinatra::Base
# /
get '/' do

redirect to('/files.html')
redirect to('/files')

end


# list files
# /files.html
get '/files.html' do
# list files; repond as html or json
# /files
get '/files' do

@file_count = @file_list.size
@ferver_path = get_current_ferver_path
if request.preferred_type.to_s == "application/json"

erb :file_list_view

end
content_type :json
@file_list.to_json

else

# list files
# /files.json
get '/files.json' do

content_type :json

@file_list.to_json
@file_count = @file_list.size
@ferver_path = get_current_ferver_path

erb :file_list_view

end

end

Expand All @@ -75,7 +73,7 @@ class Ferver < Sinatra::Base


# Find all files in `Ferver` directory.
# Called before each response.
# !Called before each response.
#
before do

Expand Down

0 comments on commit 8f27a55

Please sign in to comment.