diff --git a/app/lib/resources/preprocessor.rb b/app/lib/resources/preprocessor.rb index b1e259d61..427996771 100644 --- a/app/lib/resources/preprocessor.rb +++ b/app/lib/resources/preprocessor.rb @@ -8,7 +8,7 @@ module Resources # # ["bot", "device_3", "resources_v0", "destroy", "Sequence", "2", "xyz"] segments = delivery_info.routing_key.split(".") - _, device_name, _, action, resource, resource_id, uuid = segments + _, device_name, _, action, resource, uuid, resource_id = segments run!(device_name: device_name, action: action, resource: resource, diff --git a/app/lib/resources/service.rb b/app/lib/resources/service.rb index e66d88733..fa5054ae6 100644 --- a/app/lib/resources/service.rb +++ b/app/lib/resources/service.rb @@ -1,6 +1,15 @@ module Resources + MQTT_CHAN = "from_api" + CHANNEL_TPL = + "bot.device_%{device_id}.resources_v0.%{action}.%{klass}.%{uuid}.%{id}" + INDEX_OF_USERNAME = 1 + INDEX_OF_OP = 3 + INDEX_OF_KIND = 4 + INDEX_OF_UUID = 5 + INDEX_OF_ID = 6 + class Service - MQTT_CHAN = "from_api" + def self.ok(uuid) { kind: "rpc_ok", args: { label: uuid } }.to_json end @@ -21,8 +30,8 @@ module Resources rescue Mutations::ValidationException => q Rollbar.error(q) raw_chan = delivery_info&.routing_key&.split(".") || [] - id = raw_chan[1]&.gsub("device_", "")&.to_i - uuid = (raw_chan.last || "NONE") + id = raw_chan[INDEX_OF_USERNAME]&.gsub("device_", "")&.to_i + uuid = raw_chan[INDEX_OF_UUID] || "NONE" Transport.current.amqp_send(rpc_err(uuid, q), id, MQTT_CHAN) if id nil end diff --git a/batch_updates.md b/batch_updates.md index f5844e76d..aa9c92943 100644 --- a/batch_updates.md +++ b/batch_updates.md @@ -25,7 +25,7 @@ Not all resources support the experimental resource API. Send an MQTT message in the format of: ``` -bot/device_/resources_v0//// +bot/device_/resources_v0//// ``` Example 1-1: diff --git a/spec/lib/resources/resources_preprocessor_spec.rb b/spec/lib/resources/resources_preprocessor_spec.rb index c26ac0f68..76ad43a19 100644 --- a/spec/lib/resources/resources_preprocessor_spec.rb +++ b/spec/lib/resources/resources_preprocessor_spec.rb @@ -2,8 +2,6 @@ require "spec_helper" describe Resources::PreProcessor do DeliveryInfoShim = Struct.new(:routing_key) - CHANNEL_TPL = - "bot.device_%{device_id}.resources_v0.%{action}.%{klass}.%{id}.%{uuid}" let(:pb) { FactoryBot.create(:pin_binding) } @@ -17,7 +15,7 @@ describe Resources::PreProcessor do let(:preprocessed) do body = {}.to_json - chan = CHANNEL_TPL % props + chan = Resources::CHANNEL_TPL % props Resources::PreProcessor.from_amqp(DeliveryInfoShim.new(chan), body) end @@ -32,7 +30,7 @@ describe Resources::PreProcessor do it "handles bad JSON" do body = "}{" - chan = CHANNEL_TPL % props + chan = Resources::CHANNEL_TPL % props expect do Resources::PreProcessor.from_amqp(DeliveryInfoShim.new(chan), body) end.to raise_error(Mutations::ValidationException, "body must be a JSON object") @@ -41,7 +39,7 @@ describe Resources::PreProcessor do describe Resources::Service do it "handles syntax errors using step1" do body = "[]" - chan = CHANNEL_TPL % props + chan = Resources::CHANNEL_TPL % props shim = DeliveryInfoShim.new(chan) Resources::Service.process(shim, body) result = Transport.current.connection @@ -88,7 +86,7 @@ describe Resources::PreProcessor do it "processes resources" do body = {}.to_json - chan = CHANNEL_TPL % props + chan = Resources::CHANNEL_TPL % props before = PinBinding.count result = Resources::Service.process(DeliveryInfoShim.new(chan), body) expect(PinBinding.count).to be < before