Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contributor show endpoint #10

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ gem 'sidekiq'
gem 'sidekiq-failures'
gem 'rails-i18n', '~> 6.0.0'
gem 'pundit'
gem 'active_model_serializers', '~> 0.10.0'

# Pagination
gem 'wor-paginate', '~> 0.1.10'
Expand Down
23 changes: 16 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ GEM
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
active_model_serializers (0.10.10)
actionpack (>= 4.1, < 6.1)
activemodel (>= 4.1, < 6.1)
case_transform (>= 0.2)
jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
activejob (6.0.2.2)
activesupport (= 6.0.2.2)
globalid (>= 0.3.6)
Expand Down Expand Up @@ -65,7 +70,7 @@ GEM
thread_safe (~> 0.3, >= 0.3.1)
bootsnap (1.4.6)
msgpack (~> 1.0)
brakeman (4.8.0)
brakeman (4.8.1)
builder (3.2.4)
bullet (6.1.0)
activesupport (>= 3.0.0)
Expand All @@ -74,6 +79,8 @@ GEM
bundler (>= 1.2.0, < 3)
thor (~> 0.18)
byebug (11.1.1)
case_transform (0.2)
activesupport
codeclimate-engine-rb (0.4.1)
virtus (~> 1.0)
coercible (1.0.0)
Expand All @@ -83,7 +90,7 @@ GEM
crack (0.4.3)
safe_yaml (~> 1.0.0)
crass (1.0.6)
database_cleaner (1.8.3)
database_cleaner (1.8.4)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
diff-lcs (1.3)
Expand Down Expand Up @@ -121,14 +128,15 @@ GEM
jaro_winkler (1.5.4)
json-schema (2.8.1)
addressable (>= 2.4)
jsonapi-renderer (0.2.2)
kwalify (0.7.2)
launchy (2.4.3)
addressable (~> 2.3)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
ruby_dep (~> 1.2)
loofah (2.4.0)
loofah (2.5.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.1)
Expand All @@ -145,12 +153,12 @@ GEM
nokogiri (1.10.9)
mini_portile2 (~> 2.4.0)
parallel (1.19.1)
parser (2.7.0.5)
parser (2.7.1.0)
ast (~> 2.4.0)
path_expander (1.1.0)
pg (1.2.3)
psych (3.1.0)
public_suffix (4.0.3)
public_suffix (4.0.4)
puma (3.12.4)
pundit (2.1.0)
activesupport (>= 3.0.0)
Expand Down Expand Up @@ -231,7 +239,7 @@ GEM
rexml
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 2.0)
rubocop-rails (2.5.0)
rubocop-rails (2.5.2)
activesupport
rack (>= 1.1)
rubocop (>= 0.72.0)
Expand Down Expand Up @@ -281,7 +289,7 @@ GEM
thor (0.20.3)
thread_safe (0.3.6)
tty-which (0.4.2)
tzinfo (1.2.6)
tzinfo (1.2.7)
thread_safe (~> 0.1)
unicode-display_width (1.7.0)
uniform_notifier (1.13.0)
Expand Down Expand Up @@ -309,6 +317,7 @@ PLATFORMS
ruby

DEPENDENCIES
active_model_serializers (~> 0.10.0)
bootsnap (>= 1.4.2)
brakeman
bullet
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/api/v1/contributors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Api
module V1
class ContributorsController < ApplicationController
include Wor::Paginate

def index
render_paginated Contributor, each_serializer: ContributorSerializer
end
end
end
end
3 changes: 3 additions & 0 deletions app/serializers/contributor_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ContributorSerializer < ActiveModel::Serializer
attributes :id, :name, :workload, :country, :admission_date
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

scope module: 'api/v1', path: 'api/v1' do
resources :projects, only: %i[index]
resources :contributors, only: [:index]
end
end
2 changes: 1 addition & 1 deletion doc/open_api/3.0.0/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"description": "when calling the endpoint without any params",
"content": {
"application/json": {
"example": "{\"page\":[{\"id\":11,\"name\":\"Flerovium\",\"created_at\":\"2020-04-01T14:04:59.438Z\",\"updated_at\":\"2020-04-01T14:04:59.438Z\"},{\"id\":12,\"name\":\"Lutetium\",\"created_at\":\"2020-04-01T14:04:59.440Z\",\"updated_at\":\"2020-04-01T14:04:59.440Z\"},{\"id\":13,\"name\":\"Technetium\",\"created_at\":\"2020-04-01T14:04:59.441Z\",\"updated_at\":\"2020-04-01T14:04:59.441Z\"},{\"id\":14,\"name\":\"Einsteinium\",\"created_at\":\"2020-04-01T14:04:59.443Z\",\"updated_at\":\"2020-04-01T14:04:59.443Z\"},{\"id\":15,\"name\":\"Yttrium\",\"created_at\":\"2020-04-01T14:04:59.444Z\",\"updated_at\":\"2020-04-01T14:04:59.444Z\"}],\"count\":5,\"total_pages\":1,\"total_count\":5,\"current_page\":1,\"previous_page\":null,\"next_page\":null}"
"example": "{\"page\":[{\"id\":11,\"name\":\"Dubnium\",\"created_at\":\"2020-04-14T20:34:03.114Z\",\"updated_at\":\"2020-04-14T20:34:03.114Z\"},{\"id\":12,\"name\":\"Actinium\",\"created_at\":\"2020-04-14T20:34:03.117Z\",\"updated_at\":\"2020-04-14T20:34:03.117Z\"},{\"id\":13,\"name\":\"Plutonium\",\"created_at\":\"2020-04-14T20:34:03.119Z\",\"updated_at\":\"2020-04-14T20:34:03.119Z\"},{\"id\":14,\"name\":\"Gold\",\"created_at\":\"2020-04-14T20:34:03.121Z\",\"updated_at\":\"2020-04-14T20:34:03.121Z\"},{\"id\":15,\"name\":\"Carbon\",\"created_at\":\"2020-04-14T20:34:03.123Z\",\"updated_at\":\"2020-04-14T20:34:03.123Z\"}],\"count\":5,\"total_pages\":1,\"total_count\":5,\"current_page\":1,\"previous_page\":null,\"next_page\":null}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is why I don't like using Faker in factories :(, but it can't be helped.

}
}
}
Expand Down
42 changes: 42 additions & 0 deletions spec/controllers/api/v1/contributors_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'rails_helper'

describe Api::V1::ContributorsController do
describe 'GET #index' do
subject(:http_request) do
get :index
end

context 'when found results' do
let(:contributors) { create_list(:contributor, 5) }

before do
contributors
http_request
end

it 'returns the contributors' do
expect(response_body['page']).to be_present
end

it 'responses with 200' do
expect(response).to have_http_status(:ok)
end

it 'matchs the projects index schema' do
expect(response).to match_response_schema('contributors/index')
end
end

context 'when there are no results' do
before { http_request }

it 'returns empty array' do
expect(response_body['page']).to be_empty
end

it 'responses with 200' do
expect(response).to have_http_status(:ok)
end
end
end
end
24 changes: 24 additions & 0 deletions spec/support/schemas/api/v1/contributors/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"type": "object",
"properties": {
"page": {
"type": "array, null",
"items": {
"type": "object",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"workload": { "type": "string" },
"country": { "type": "string" },
"admission_date": { "type": "date" }
}
}
},
"count": { "type": "integer" },
"total_pages": { "type": "integer" },
"total_count": { "type": "integer" },
"current_page": { "type": "integer" },
"next_page": { "type": "integer, null" },
"previous_page": { "type": "string, null" }
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EOL