Commit 497165875fc1cd7744493ee82fc00917215531ed

Authored by Isaac Lewis
1 parent db76748d

refactor object structs; logon endpoint

@@ -14,6 +14,7 @@ require "syspro/version" @@ -14,6 +14,7 @@ require "syspro/version"
14 14
15 require "syspro/api_operations/get_version" 15 require "syspro/api_operations/get_version"
16 require "syspro/api_operations/request" 16 require "syspro/api_operations/request"
  17 +require "syspro/api_operations/logon"
17 18
18 19
19 module Syspro 20 module Syspro
lib/syspro/api_operations/get_version.rb
@@ -5,9 +5,17 @@ module Syspro @@ -5,9 +5,17 @@ module Syspro
5 class GetVersion 5 class GetVersion
6 include ApiOperations::Request 6 include ApiOperations::Request
7 7
  8 + def get_version
  9 + resp = self.request(:get, resource_url)
  10 + version = VersionObject.new(resp[0].http_body)
  11 + end
  12 +
8 def resource_url 13 def resource_url
9 "/GetVersion" 14 "/GetVersion"
10 end 15 end
  16 +
  17 + VersionObject = Struct.new(:version) do
  18 + end
11 end 19 end
12 end 20 end
13 end 21 end
lib/syspro/api_operations/logon.rb 0 → 100644
  1 +require_relative "request"
  2 +
  3 +module Syspro
  4 + module ApiOperations
  5 + class Logon
  6 + include ApiOperations::Request
  7 +
  8 + def logon(username, password, company_id, company_password = nil)
  9 + params = {
  10 + "Operator" => username,
  11 + "OperatorPassword" => password,
  12 + "CompanyId" => company_id,
  13 + "CompanyPassword" => company_password
  14 + }
  15 + resp = self.request(:get, resource_url, params)
  16 + user_id = UserIdObject.new(resp[0].http_body)
  17 + end
  18 +
  19 + def resource_url
  20 + "/Logon"
  21 + end
  22 +
  23 + UserIdObject = Struct.new(:guid) do
  24 + end
  25 + end
  26 + end
  27 +end
  28 +
lib/syspro/syspro_client.rb
@@ -7,6 +7,16 @@ module Syspro @@ -7,6 +7,16 @@ module Syspro
7 @system_profiler = SystemProfiler.new 7 @system_profiler = SystemProfiler.new
8 end 8 end
9 9
  10 + def logon(username, password, company_id, company_password)
  11 + logon_getter = Syspro::ApiOperations::Logon.new
  12 + logon_getter.logon(username, password, company_id, company_password)
  13 + end
  14 +
  15 + def get_syspro_version
  16 + version_getter = Syspro::ApiOperations::GetVersion.new
  17 + version_getter.get_version
  18 + end
  19 +
10 def self.active_client 20 def self.active_client
11 Thread.current[:syspro_client] || default_client 21 Thread.current[:syspro_client] || default_client
12 end 22 end
@@ -15,11 +25,6 @@ module Syspro @@ -15,11 +25,6 @@ module Syspro
15 Thread.current[:syspro_client_default_client] ||= SysproClient.new(default_conn) 25 Thread.current[:syspro_client_default_client] ||= SysproClient.new(default_conn)
16 end 26 end
17 27
18 - def get_syspro_version  
19 - version_getter = Syspro::ApiOperations::GetVersion.new  
20 - version_getter.request(:get, version_getter.resource_url)  
21 - end  
22 -  
23 # A default Faraday connection to be used when one isn't configured. This 28 # A default Faraday connection to be used when one isn't configured. This
24 # object should never be mutated, and instead instantiating your own 29 # object should never be mutated, and instead instantiating your own
25 # connection and wrapping it in a SysproClient object should be preferred. 30 # connection and wrapping it in a SysproClient object should be preferred.
lib/syspro/util.rb
@@ -125,6 +125,13 @@ module Syspro @@ -125,6 +125,13 @@ module Syspro
125 end 125 end
126 end 126 end
127 127
  128 + def self.url_encode(key)
  129 + CGI.escape(key.to_s).
  130 + # Don't use strict form encoding by changing the square bracket control
  131 + # characters back to their literals. This is fine by the server, and
  132 + # makes these parameter strings easier to read.
  133 + gsub("%5B", "[").gsub("%5D", "]")
  134 + end
128 end 135 end
129 end 136 end
130 137
test/client_test.rb
@@ -3,6 +3,6 @@ require "test_helper" @@ -3,6 +3,6 @@ require "test_helper"
3 class SysproClientTest < Minitest::Test 3 class SysproClientTest < Minitest::Test
4 def test_get_syspro_version 4 def test_get_syspro_version
5 client = ::Syspro::SysproClient.new 5 client = ::Syspro::SysproClient.new
6 - assert_match /(\d+\.)?(\d+\.)?(\d+\.)?(\d+)/, client.get_syspro_version[0].http_body 6 + assert_match /(\d+\.)?(\d+\.)?(\d+\.)?(\d+)/, client.get_syspro_version.version
7 end 7 end
8 end 8 end
test/logon_test.rb 0 → 100644
  1 +require "test_helper"
  2 +
  3 +class LogonTest < Minitest::Test
  4 + def test_logon
  5 + username = "wland"
  6 + password = "piperita2016"
  7 + company = "L"
  8 + company_password = ""
  9 + client = ::Syspro::SysproClient.new
  10 +
  11 + assert_match /([A-Z0-9]{33})\w/, client.logon(username, password, company, company_password).guid
  12 + end
  13 +end
  14 +