diff --git a/lib/syspro.rb b/lib/syspro.rb index 78279bc..f572547 100644 --- a/lib/syspro.rb +++ b/lib/syspro.rb @@ -14,6 +14,7 @@ require "syspro/version" require "syspro/api_operations/get_version" require "syspro/api_operations/request" +require "syspro/api_operations/logon" module Syspro diff --git a/lib/syspro/api_operations/get_version.rb b/lib/syspro/api_operations/get_version.rb index e9a6194..a6c4047 100644 --- a/lib/syspro/api_operations/get_version.rb +++ b/lib/syspro/api_operations/get_version.rb @@ -5,9 +5,17 @@ module Syspro class GetVersion include ApiOperations::Request + def get_version + resp = self.request(:get, resource_url) + version = VersionObject.new(resp[0].http_body) + end + def resource_url "/GetVersion" end + + VersionObject = Struct.new(:version) do + end end end end diff --git a/lib/syspro/api_operations/logon.rb b/lib/syspro/api_operations/logon.rb new file mode 100644 index 0000000..f1f93f0 --- /dev/null +++ b/lib/syspro/api_operations/logon.rb @@ -0,0 +1,28 @@ +require_relative "request" + +module Syspro + module ApiOperations + class Logon + include ApiOperations::Request + + def logon(username, password, company_id, company_password = nil) + params = { + "Operator" => username, + "OperatorPassword" => password, + "CompanyId" => company_id, + "CompanyPassword" => company_password + } + resp = self.request(:get, resource_url, params) + user_id = UserIdObject.new(resp[0].http_body) + end + + def resource_url + "/Logon" + end + + UserIdObject = Struct.new(:guid) do + end + end + end +end + diff --git a/lib/syspro/syspro_client.rb b/lib/syspro/syspro_client.rb index 1756e13..26ad093 100644 --- a/lib/syspro/syspro_client.rb +++ b/lib/syspro/syspro_client.rb @@ -7,6 +7,16 @@ module Syspro @system_profiler = SystemProfiler.new end + def logon(username, password, company_id, company_password) + logon_getter = Syspro::ApiOperations::Logon.new + logon_getter.logon(username, password, company_id, company_password) + end + + def get_syspro_version + version_getter = Syspro::ApiOperations::GetVersion.new + version_getter.get_version + end + def self.active_client Thread.current[:syspro_client] || default_client end @@ -15,11 +25,6 @@ module Syspro Thread.current[:syspro_client_default_client] ||= SysproClient.new(default_conn) end - def get_syspro_version - version_getter = Syspro::ApiOperations::GetVersion.new - version_getter.request(:get, version_getter.resource_url) - end - # A default Faraday connection to be used when one isn't configured. This # object should never be mutated, and instead instantiating your own # connection and wrapping it in a SysproClient object should be preferred. diff --git a/lib/syspro/util.rb b/lib/syspro/util.rb index b86d2ad..150baa6 100644 --- a/lib/syspro/util.rb +++ b/lib/syspro/util.rb @@ -125,6 +125,13 @@ module Syspro end end + def self.url_encode(key) + CGI.escape(key.to_s). + # Don't use strict form encoding by changing the square bracket control + # characters back to their literals. This is fine by the server, and + # makes these parameter strings easier to read. + gsub("%5B", "[").gsub("%5D", "]") + end end end diff --git a/test/client_test.rb b/test/client_test.rb index 721ed8a..75537b0 100644 --- a/test/client_test.rb +++ b/test/client_test.rb @@ -3,6 +3,6 @@ require "test_helper" class SysproClientTest < Minitest::Test def test_get_syspro_version client = ::Syspro::SysproClient.new - assert_match /(\d+\.)?(\d+\.)?(\d+\.)?(\d+)/, client.get_syspro_version[0].http_body + assert_match /(\d+\.)?(\d+\.)?(\d+\.)?(\d+)/, client.get_syspro_version.version end end diff --git a/test/logon_test.rb b/test/logon_test.rb new file mode 100644 index 0000000..4248053 --- /dev/null +++ b/test/logon_test.rb @@ -0,0 +1,14 @@ +require "test_helper" + +class LogonTest < Minitest::Test + def test_logon + username = "wland" + password = "piperita2016" + company = "L" + company_password = "" + client = ::Syspro::SysproClient.new + + assert_match /([A-Z0-9]{33})\w/, client.logon(username, password, company, company_password).guid + end +end + -- libgit2 0.21.4