Skip to content

Commit

Permalink
Merge pull request #67 from sanger/GPL-831-1-as-developers-we-need-to…
Browse files Browse the repository at this point in the history
…-create-a-v2-api

Gpl 831 1 [WIP] as developers we need to create a v2 api
  • Loading branch information
harrietc52 authored Jan 27, 2021
2 parents 44d859f + cc4194b commit c28e32f
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
21 changes: 21 additions & 0 deletions app/controllers/v2/print_jobs_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module V2
# PrintJobsController
class PrintJobsController < ApplicationController
def create
print_job_wrapper = PrintJobWrapper.new(print_job_params)
if print_job_wrapper.valid?
render json: { message: 'labels successfully printed' }
else
render_error print_job_wrapper
end
end

private

def print_job_params
params.require(:print_job).permit(:printer_name, :label_template_name)
end
end
end
10 changes: 10 additions & 0 deletions app/controllers/v2/printers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module V2
# PrintersController
class PrintersController < ApplicationController
def index
render json: { printers: Printer.all }
end
end
end
20 changes: 20 additions & 0 deletions app/models/print_job_wrapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

# PrintJobWrapper
class PrintJobWrapper
include ActiveModel::Model

attr_reader :printer_name, :label_template_name

validate :check_attributes

def initialize(params = {})
@printer_name = params['printer_name']
@label_template_name = params['label_template_name']
end

def check_attributes
errors.add(:printer_name, 'does not exist') if printer_name.nil?
errors.add(:label_template_name, 'does not exist') if label_template_name.nil?
end
end
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@
get 'docs', to: 'docs#index'
end

namespace :v2 do
resources :print_jobs, only: [:create]
resources :printers, only: [:index]
end

match 'v1/test_exception_notifier', controller: 'application', action: 'test_exception_notifier', via: :get
end
28 changes: 28 additions & 0 deletions spec/v2/print_jobs_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe V2::PrintJobsController, type: :request, helpers: true do

let!(:printer) { create(:printer) }
let!(:label_template) { create(:label_template) }

it 'should print a valid label' do
post v2_print_jobs_path, params: { print_job: { printer_name: printer.name, label_template_name: label_template.name } }
expect(response).to be_successful
expect(response).to have_http_status(:success)
json = ActiveSupport::JSON.decode(response.body)
expect(json['message']).to eq('labels successfully printed')
end

it 'should return an error if request provides incorrect parameters' do
post v2_print_jobs_path, params: { print_job: { printer_name: printer.name } }
expect(response).to_not be_successful
expect(response).to have_http_status(:unprocessable_entity)

json = ActiveSupport::JSON.decode(response.body)

expect(json['errors']).not_to be_empty
expect(find_attribute_error_details(json, 'label_template')).to include('does not exist')
end
end
14 changes: 14 additions & 0 deletions spec/v2/printers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe V2::PrintersController, type: :request do

it 'should allow retrieval of all printers' do
printers = create_list(:printer, 5)
get v2_printers_path
expect(response).to be_successful
expect(ActiveSupport::JSON.decode(response.body)['printers'].length).to eq(printers.length)
end

end

0 comments on commit c28e32f

Please sign in to comment.