Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions lib/castle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,24 @@
castle/context/sanitize
castle/context/get_default
castle/context/prepare
castle/commands/identify
castle/commands/approve_device
castle/commands/authenticate
castle/commands/get_device
castle/commands/get_devices
castle/commands/track
castle/commands/review
castle/commands/identify
castle/commands/impersonate
castle/api/identify
castle/commands/report_device
castle/commands/review
castle/commands/track
castle/api/approve_device
castle/api/authenticate
castle/api/track
castle/api/review
castle/api/impersonate
castle/api/get_device
castle/api/get_devices
castle/api/identify
castle/api/impersonate
castle/api/report_device
castle/api/review
castle/api/track
castle/payload/prepare
castle/configuration
castle/logger
Expand Down
22 changes: 22 additions & 0 deletions lib/castle/api/approve_device.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Castle
module API
# Sends PUT devices/#{device_token}/approve request
module ApproveDevice
class << self
# @param options [Hash]
# return [Hash]
def call(options = {})
options = Castle::Utils::DeepSymbolizeKeys.call(options || {})

Castle::API.call(
Castle::Commands::ApproveDevice.build(options),
{},
options[:http]
)
end
end
end
end
end
22 changes: 22 additions & 0 deletions lib/castle/api/report_device.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Castle
module API
# Sends PUT devices/#{device_token}/report request
module ReportDevice
class << self
# @param options [Hash]
# return [Hash]
def call(options = {})
options = Castle::Utils::DeepSymbolizeKeys.call(options || {})

Castle::API.call(
Castle::Commands::ReportDevice.build(options),
{},
options[:http]
)
end
end
end
end
end
21 changes: 21 additions & 0 deletions lib/castle/commands/approve_device.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Castle
module Commands
# Generated the payload for the PUT devices/#{device_token}/approve request
class ApproveDevice
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]}/approve",
nil,
:put
)
end
end
end
end
end
21 changes: 21 additions & 0 deletions lib/castle/commands/report_device.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Castle
module Commands
# Generated the payload for the PUT devices/#{device_token}/report request
class ReportDevice
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]}/report",
nil,
:put
)
end
end
end
end
end
21 changes: 21 additions & 0 deletions spec/lib/castle/api/approve_device_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

describe Castle::API::ApproveDevice 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 do
assert_requested :put, "https://api.castle.io/v1/devices/#{device_token}/approve", times: 1
end
end
end
21 changes: 21 additions & 0 deletions spec/lib/castle/api/report_device_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

describe Castle::API::ReportDevice 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 do
assert_requested :put, "https://api.castle.io/v1/devices/#{device_token}/report", times: 1
end
end
end
24 changes: 24 additions & 0 deletions spec/lib/castle/commands/approve_device_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

describe Castle::Commands::ApproveDevice 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(:put) }
it { expect(command.path).to be_eql("devices/#{device_token}/approve") }
it { expect(command.data).to be_nil }
end
end
end
24 changes: 24 additions & 0 deletions spec/lib/castle/commands/report_device_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

describe Castle::Commands::ReportDevice 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(:put) }
it { expect(command.path).to be_eql("devices/#{device_token}/report") }
it { expect(command.data).to be_nil }
end
end
end