From fe5d1143b612d5ace9498b034882cf061f1d9be4 Mon Sep 17 00:00:00 2001 From: Justin Case Date: Sun, 18 Jun 2023 22:27:22 +0200 Subject: [PATCH] Add Terminals API https://docs.mollie.com/reference/v2/terminals-api/overview (beta) --- examples/terminals/get.rb | 1 + examples/terminals/list.rb | 1 + lib/mollie.rb | 1 + lib/mollie/terminal.rb | 59 +++++++++++++++++++++++++++++++ test/fixtures/terminals/get.json | 23 ++++++++++++ test/fixtures/terminals/list.json | 34 ++++++++++++++++++ test/mollie/terminal_test.rb | 54 ++++++++++++++++++++++++++++ 7 files changed, 173 insertions(+) create mode 100644 examples/terminals/get.rb create mode 100644 examples/terminals/list.rb create mode 100644 lib/mollie/terminal.rb create mode 100644 test/fixtures/terminals/get.json create mode 100644 test/fixtures/terminals/list.json create mode 100644 test/mollie/terminal_test.rb diff --git a/examples/terminals/get.rb b/examples/terminals/get.rb new file mode 100644 index 0000000..c4a01dc --- /dev/null +++ b/examples/terminals/get.rb @@ -0,0 +1 @@ +terminal = Mollie::Terminal.get("term_7MgL4wea46qkRcoTZjWEH") diff --git a/examples/terminals/list.rb b/examples/terminals/list.rb new file mode 100644 index 0000000..12294b7 --- /dev/null +++ b/examples/terminals/list.rb @@ -0,0 +1 @@ +terminals = Mollie::Terminal.all diff --git a/lib/mollie.rb b/lib/mollie.rb index a8c8924..b7dd461 100644 --- a/lib/mollie.rb +++ b/lib/mollie.rb @@ -28,6 +28,7 @@ module Mollie require 'mollie/refund' require 'mollie/settlement' require 'mollie/subscription' +require 'mollie/terminal' require 'mollie/customer/mandate' require 'mollie/customer/payment' diff --git a/lib/mollie/terminal.rb b/lib/mollie/terminal.rb new file mode 100644 index 0000000..f29ecd8 --- /dev/null +++ b/lib/mollie/terminal.rb @@ -0,0 +1,59 @@ +module Mollie + class Terminal < Base + + STATUS_PENDING = "pending".freeze + STATUS_ACTIVE = "active".freeze + STATUS_INACTIVE = "inactive".freeze + + attr_accessor :id, + :profile_id, + :status, + :brand, + :model, + :serial_number, + :currency, + :description, + :created_at, + :updated_at, + :deactivated_at, + :_links + + alias links _links + + def pending? + status == STATUS_PENDING + end + + def active? + status == STATUS_ACTIVE + end + + def inactive? + status == STATUS_INACTIVE + end + + def created_at=(created_at) + @created_at = begin + Time.parse(created_at.to_s) + rescue StandardError + nil + end + end + + def updated_at=(updated_at) + @updated_at = begin + Time.parse(updated_at.to_s) + rescue StandardError + nil + end + end + + def deactivated_at=(deactivated_at) + @deactivated_at = begin + Time.parse(deactivated_at.to_s) + rescue StandardError + nil + end + end + end +end diff --git a/test/fixtures/terminals/get.json b/test/fixtures/terminals/get.json new file mode 100644 index 0000000..e290a08 --- /dev/null +++ b/test/fixtures/terminals/get.json @@ -0,0 +1,23 @@ +{ + "id": "term_7MgL4wea46qkRcoTZjWEH", + "profileId": "pfl_QkEhN94Ba", + "status": "active", + "brand": "PAX", + "model": "A920", + "serialNumber": "1234567890", + "currency": "EUR", + "description": "Terminal #12345", + "createdAt": "2022-02-12T11:58:35.0Z", + "updatedAt": "2022-11-15T13:32:11+00:00", + "deactivatedAt": "2022-02-12T12:13:35.0Z", + "_links": { + "self": { + "href": "https://api.mollie.com/v2/terminals/term_7MgL4wea46qkRcoTZjWEH", + "type": "application/hal+json" + }, + "documentation": { + "href": "https://docs.mollie.com/reference/v2/terminals-api/get-terminal", + "type": "text/html" + } + } +} diff --git a/test/fixtures/terminals/list.json b/test/fixtures/terminals/list.json new file mode 100644 index 0000000..5d34419 --- /dev/null +++ b/test/fixtures/terminals/list.json @@ -0,0 +1,34 @@ +{ + "count": 3, + "_embedded": { + "terminals": [ + { + "resource": "terminal", + "id": "terminal_one" + }, + { + "resource": "terminal", + "id": "terminal_two" + }, + { + "resource": "terminal", + "id": "terminal_three" + } + ] + }, + "_links": { + "self": { + "href": "https://api.mollie.com/v2/terminals", + "type": "application/hal+json" + }, + "previous": null, + "next": { + "href": "https://api.mollie.com/v2/terminals?from=term_7MgL4wea46qkRcoTZjWEH", + "type": "application/hal+json" + }, + "documentation": { + "href": "https://docs.mollie.com/reference/v2/terminals-api/list-terminals", + "type": "text/html" + } + } +} diff --git a/test/mollie/terminal_test.rb b/test/mollie/terminal_test.rb new file mode 100644 index 0000000..c937fac --- /dev/null +++ b/test/mollie/terminal_test.rb @@ -0,0 +1,54 @@ +require 'helper' + +module Mollie + class TerminalTest < Test::Unit::TestCase + GET_TERMINAL = read_fixture('terminals/get.json') + LIST_TERMINALS = read_fixture('terminals/list.json') + + def test_get_terminal + stub_request(:get, "https://api.mollie.com/v2/terminals/term_7MgL4wea46qkRcoTZjWEH") + .to_return(status: 200, body: GET_TERMINAL, headers: {}) + + terminal = Terminal.get("term_7MgL4wea46qkRcoTZjWEH") + + assert_equal "term_7MgL4wea46qkRcoTZjWEH", terminal.id + assert_equal "pfl_QkEhN94Ba", terminal.profile_id + assert_equal "active", terminal.status + assert_equal "PAX", terminal.brand + assert_equal "A920", terminal.model + assert_equal "1234567890", terminal.serial_number + assert_equal "EUR", terminal.currency + assert_equal "Terminal #12345", terminal.description + assert_equal Time.parse("2022-02-12T11:58:35.0Z"), terminal.created_at + assert_equal Time.parse("2022-11-15T13:32:11+00:00"), terminal.updated_at + assert_equal Time.parse("2022-02-12T12:13:35.0Z"), terminal.deactivated_at + end + + def test_status_pending + assert Terminal.new(status: Terminal::STATUS_PENDING).pending? + assert !Terminal.new(status: "not-pending").pending? + end + + def test_status_active + assert Terminal.new(status: Terminal::STATUS_ACTIVE).active? + assert !Terminal.new(status: "not-active").active? + end + + def test_status_inactive + assert Terminal.new(status: Terminal::STATUS_INACTIVE).inactive? + assert !Terminal.new(status: "not-inactive").inactive? + end + + def test_list_terminals + stub_request(:get, "https://api.mollie.com/v2/terminals") + .to_return(status: 200, body: LIST_TERMINALS, headers: {}) + + terminals = Terminal.all + + assert_equal 3, terminals.size + assert_equal "terminal_one", terminals[0].id + assert_equal "terminal_two", terminals[1].id + assert_equal "terminal_three", terminals[2].id + end + end +end