diff --git a/lib/castle.rb b/lib/castle.rb index cd96d0c2..9fc42086 100644 --- a/lib/castle.rb +++ b/lib/castle.rb @@ -26,6 +26,7 @@ castle/context/prepare castle/commands/identify castle/commands/authenticate + castle/commands/get_device castle/commands/get_devices castle/commands/track castle/commands/review @@ -35,6 +36,7 @@ castle/api/track castle/api/review castle/api/impersonate + castle/api/get_device castle/api/get_devices castle/payload/prepare castle/configuration diff --git a/lib/castle/api/get_device.rb b/lib/castle/api/get_device.rb new file mode 100644 index 00000000..3bf888a8 --- /dev/null +++ b/lib/castle/api/get_device.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Castle + module API + # Sends GET devices/#{device_token} request + module GetDevice + class << self + # @param options [Hash] + # return [Hash] + def call(options = {}) + options = Castle::Utils::DeepSymbolizeKeys.call(options || {}) + + Castle::API.call( + Castle::Commands::GetDevice.build(options), + {}, + options[:http] + ) + end + end + end + end +end diff --git a/lib/castle/commands/get_device.rb b/lib/castle/commands/get_device.rb new file mode 100644 index 00000000..b22f2dc6 --- /dev/null +++ b/lib/castle/commands/get_device.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Castle + module Commands + # Generated the payload for the GET devices/#{device_token} request + class GetDevice + class << self + # @param options [Hash] + # @return [Castle::Command] + def build(options = {}) + Castle::Validators::Present.call(options, %i[device_token]) + Castle::Command.new( + "devices/#{options[:device_token]}", + nil, + :get + ) + end + end + end + end +end diff --git a/spec/lib/castle/api/get_device_spec.rb b/spec/lib/castle/api/get_device_spec.rb new file mode 100644 index 00000000..72583b26 --- /dev/null +++ b/spec/lib/castle/api/get_device_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +describe Castle::API::GetDevice do + before do + stub_request(:any, /api.castle.io/).with( + basic_auth: ['', 'secret'] + ).to_return(status: 200, body: '{}', headers: {}) + end + + describe '.call' do + subject(:retrieve) { described_class.call(device_token: device_token) } + + let(:device_token) { '1234' } + + before { retrieve } + + it { assert_requested :get, "https://api.castle.io/v1/devices/#{device_token}", times: 1 } + end +end diff --git a/spec/lib/castle/commands/get_device_spec.rb b/spec/lib/castle/commands/get_device_spec.rb new file mode 100644 index 00000000..f5947f9b --- /dev/null +++ b/spec/lib/castle/commands/get_device_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +describe Castle::Commands::GetDevice do + subject(:instance) { described_class } + + let(:context) { {} } + let(:device_token) { '1234' } + + describe '.build' do + subject(:command) { instance.build(device_token: device_token) } + + context 'without device_token' do + let(:device_token) { '' } + + it { expect { command }.to raise_error(Castle::InvalidParametersError) } + end + + context 'with device_token' do + it { expect(command.method).to be_eql(:get) } + it { expect(command.path).to be_eql("devices/#{device_token}") } + it { expect(command.data).to be_nil } + end + end +end