-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ce831d
commit 4bdfdb5
Showing
11 changed files
with
216 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
module Mailosaur | ||
class Devices | ||
# | ||
# Creates and initializes a new instance of the Devices class. | ||
# @param client connection. | ||
# | ||
def initialize(conn, handle_http_error) | ||
@conn = conn | ||
@handle_http_error = handle_http_error | ||
end | ||
|
||
# @return [Connection] the client connection. | ||
attr_reader :conn | ||
|
||
# | ||
# List all devices | ||
# | ||
# Returns a list of your virtual security devices. | ||
# | ||
# @return [DeviceListResult] operation results. | ||
# | ||
def list | ||
response = conn.get 'api/devices' | ||
@handle_http_error.call(response) unless response.status == 200 | ||
model = JSON.parse(response.body) | ||
Mailosaur::Models::DeviceListResult.new(model) | ||
end | ||
|
||
# | ||
# Create a device | ||
# | ||
# Creates a new virtual security device and returns it. | ||
# | ||
# @param device_create_options [DeviceCreateOptions] | ||
# | ||
# @return [Device] operation results. | ||
# | ||
def create(device_create_options) | ||
response = conn.post 'api/devices', device_create_options.to_json | ||
@handle_http_error.call(response) unless response.status == 200 | ||
model = JSON.parse(response.body) | ||
Mailosaur::Models::Device.new(model) | ||
end | ||
|
||
# | ||
# Retrieve OTP | ||
# | ||
# Retrieves the current one-time password for a saved device, or given base32-encoded shared secret. | ||
# | ||
# @param query [String] Either the unique identifier of the device, or a base32-encoded shared secret. | ||
# | ||
# @return [OtpResult] operation results. | ||
# | ||
def otp(query) | ||
if query.include? '-' | ||
response = conn.get "api/devices/#{query}/otp" | ||
@handle_http_error.call(response) unless response.status == 200 | ||
model = JSON.parse(response.body) | ||
return Mailosaur::Models::OtpResult.new(model) | ||
end | ||
|
||
options = Mailosaur::Models::DeviceCreateOptions.new | ||
options.shared_secret = query | ||
response = conn.post 'api/devices/otp', options.to_json | ||
@handle_http_error.call(response) unless response.status == 200 | ||
model = JSON.parse(response.body) | ||
Mailosaur::Models::OtpResult.new(model) | ||
end | ||
|
||
# | ||
# Delete a device | ||
# | ||
# Permanently deletes a device. This operation cannot be undone. | ||
# | ||
# @param id [String] The identifier of the device to be deleted. | ||
# | ||
def delete(id) | ||
response = conn.delete "api/devices/#{id}" | ||
@handle_http_error.call(response) unless response.status == 204 | ||
nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Mailosaur | ||
module Models | ||
class Device < BaseModel | ||
def initialize(data = {}) | ||
@id = data['id'] | ||
@name = data['name'] | ||
end | ||
|
||
# @return [String] Unique identifier for the device. | ||
attr_accessor :id | ||
|
||
# @return [String] The name of the device. | ||
attr_accessor :name | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Mailosaur | ||
module Models | ||
class DeviceCreateOptions < BaseModel | ||
def initialize(data = {}) | ||
@name = data['name'] | ||
@shared_secret = data['shared_secret'] | ||
end | ||
|
||
# @return [String] A name used to identify the device. | ||
attr_accessor :name | ||
|
||
# @return [String] The base32-encoded shared secret for this device. | ||
attr_accessor :shared_secret | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Mailosaur | ||
module Models | ||
class DeviceListResult < BaseModel | ||
def initialize(data = {}) | ||
@items = [] | ||
(data['items'] || []).each { |i| @items << Mailosaur::Models::Device.new(i) } | ||
end | ||
|
||
# @return [Array<Device>] The individual devices forming the result. | ||
attr_accessor :items | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Mailosaur | ||
module Models | ||
class OtpResult < BaseModel | ||
def initialize(data = {}) | ||
@code = data['code'] | ||
@expires = data['expires'] | ||
end | ||
|
||
# @return [String] The current one-time password. | ||
attr_accessor :code | ||
|
||
# @return [String] The expiry date/time of the current one-time password. | ||
attr_accessor :expires | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'mailosaur' | ||
require 'test/unit' | ||
require 'shoulda/context' | ||
|
||
module Mailosaur | ||
class DevicesTest < Test::Unit::TestCase | ||
setup do | ||
api_key = ENV['MAILOSAUR_API_KEY'] | ||
base_url = ENV['MAILOSAUR_BASE_URL'] | ||
|
||
raise ArgumentError, 'Missing necessary environment variables - refer to README.md' if api_key.nil? | ||
|
||
@client = MailosaurClient.new(api_key, base_url) | ||
end | ||
|
||
should 'perform CRUD operations' do | ||
device_name = 'My test' | ||
shared_secret = 'ONSWG4TFOQYTEMY=' | ||
|
||
# Create a new device | ||
create_options = Mailosaur::Models::DeviceCreateOptions.new | ||
create_options.name = device_name | ||
create_options.shared_secret = shared_secret | ||
|
||
created_device = @client.devices.create(create_options) | ||
assert_not_nil(created_device.id) | ||
assert_equal(device_name, created_device.name) | ||
|
||
# Retrieve an otp via device ID | ||
otp_result = @client.devices.otp(created_device.id) | ||
assert_equal(6, otp_result.code.length) | ||
|
||
list_result = @client.devices.list | ||
assert_equal(1, list_result.items.length) | ||
@client.devices.delete(created_device.id) | ||
list_result = @client.devices.list | ||
assert_equal(0, list_result.items.length) | ||
end | ||
|
||
should 'return OTP given via a shared secret' do | ||
shared_secret = 'ONSWG4TFOQYTEMY=' | ||
|
||
otp_result = @client.devices.otp(shared_secret) | ||
assert_equal(6, otp_result.code.length) | ||
end | ||
end | ||
end |