-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from sanger/GPL-831-1-as-developers-we-need-to…
…-create-a-v2-api Gpl 831 1 [WIP] as developers we need to create a v2 api
- Loading branch information
Showing
6 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |