Move point serializers to API namespace

This commit is contained in:
Eugene Burmakin 2024-10-02 21:58:19 +02:00
parent df430851ce
commit 9d4cc7a4cf
7 changed files with 50 additions and 4 deletions

View file

@ -31,6 +31,6 @@ class Api::V1::PointsController < ApiController
private
def point_serializer
params[:slim] == 'true' ? SlimPointSerializer : PointSerializer
params[:slim] == 'true' ? Api::SlimPointSerializer : Api::PointSerializer
end
end

View file

@ -0,0 +1,9 @@
# frozen_string_literal: true
class Api::PointSerializer < PointSerializer
EXCLUDED_ATTRIBUTES = %w[created_at updated_at visit_id import_id user_id raw_data].freeze
def call
point.attributes.except(*EXCLUDED_ATTRIBUTES)
end
end

View file

@ -1,6 +1,6 @@
# frozen_string_literal: true
class SlimPointSerializer
class Api::SlimPointSerializer
def initialize(point)
@point = point
end

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
class PointSerializer
EXCLUDED_ATTRIBUTES = %w[created_at updated_at visit_id import_id user_id raw_data].freeze
EXCLUDED_ATTRIBUTES = %w[created_at updated_at visit_id id import_id user_id raw_data].freeze
def initialize(point)
@point = point

View file

@ -72,8 +72,9 @@ class Exports::Create
end
def create_export_file(data)
dir_path = Rails.root.join('public', 'exports')
dir_path = Rails.root.join('public/exports')
Dir.mkdir(dir_path) unless Dir.exist?(dir_path)
file_path = dir_path.join("#{export.name}.#{file_format}")
File.open(file_path, 'w') { |file| file.write(data) }

View file

@ -0,0 +1,20 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Api::PointSerializer do
describe '#call' do
subject(:serializer) { described_class.new(point).call }
let(:point) { create(:point) }
let(:expected_json) { point.attributes.except(*Api::PointSerializer::EXCLUDED_ATTRIBUTES) }
it 'returns JSON with correct attributes' do
expect(serializer.to_json).to eq(expected_json.to_json)
end
it 'does not include excluded attributes' do
expect(serializer).not_to include(*Api::PointSerializer::EXCLUDED_ATTRIBUTES)
end
end
end

View file

@ -0,0 +1,16 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Api::SlimPointSerializer do
describe '#call' do
subject(:serializer) { described_class.new(point).call }
let(:point) { create(:point) }
let(:expected_json) { point.attributes.slice('id', 'latitude', 'longitude', 'timestamp') }
it 'returns JSON with correct attributes' do
expect(serializer.to_json).to eq(expected_json.to_json)
end
end
end