completed SMS gateway project
This commit is contained in:
53
app/controllers/api/v1/gateway/registrations_controller.rb
Normal file
53
app/controllers/api/v1/gateway/registrations_controller.rb
Normal file
@@ -0,0 +1,53 @@
|
||||
module Api
|
||||
module V1
|
||||
module Gateway
|
||||
class RegistrationsController < ApplicationController
|
||||
skip_before_action :authenticate_api_key!, only: [:create]
|
||||
|
||||
def create
|
||||
device_id = params.require(:device_id)
|
||||
name = params[:name] || "Gateway #{device_id[0..7]}"
|
||||
|
||||
# Check if gateway already exists
|
||||
gateway = ::Gateway.find_by(device_id: device_id)
|
||||
|
||||
if gateway
|
||||
render json: {
|
||||
success: false,
|
||||
error: "Gateway already registered"
|
||||
}, status: :conflict
|
||||
return
|
||||
end
|
||||
|
||||
# Create new gateway
|
||||
gateway = ::Gateway.new(
|
||||
device_id: device_id,
|
||||
name: name,
|
||||
status: "offline"
|
||||
)
|
||||
|
||||
raw_key = gateway.generate_api_key!
|
||||
|
||||
render json: {
|
||||
success: true,
|
||||
api_key: raw_key,
|
||||
device_id: gateway.device_id,
|
||||
websocket_url: websocket_url
|
||||
}, status: :created
|
||||
rescue ActionController::ParameterMissing => e
|
||||
render json: { error: e.message }, status: :bad_request
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def websocket_url
|
||||
if Rails.env.production?
|
||||
"wss://#{request.host}/cable"
|
||||
else
|
||||
"ws://#{request.host}:#{request.port}/cable"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user