From 0f29247cffbdd7d3d8155dc45de46c2325df54d1 Mon Sep 17 00:00:00 2001 From: Isaac Lewis Date: Thu, 5 Apr 2018 17:24:59 -0700 Subject: [PATCH] add comfch and comfnd --- lib/syspro.rb | 4 ++++ lib/syspro/api_operations/query.rb | 6 ++++-- lib/syspro/business_objects/combrw.rb | 3 ++- lib/syspro/business_objects/comfch.rb | 39 +++++++++++++++++++++++++++++++++++++++ lib/syspro/business_objects/comfnd.rb | 40 ++++++++++++++++++++++++++++++++++++++++ lib/syspro/business_objects/parsers/combrw_parser.rb | 15 ++++++++++----- lib/syspro/business_objects/parsers/comfch_parser.rb | 29 +++++++++++++++++++++++++++++ lib/syspro/business_objects/parsers/comfnd_parser.rb | 45 +++++++++++++++++++++++++++++++++++++++++++++ lib/syspro/business_objects/schemas/combrw.xml.erb | 4 ---- lib/syspro/business_objects/schemas/comfch.xml.erb | 14 ++++++++++++++ lib/syspro/business_objects/schemas/comfnd.xml.erb | 29 +++++++++++++++++++++++++++++ test/query_test.rb | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 12 files changed, 261 insertions(+), 15 deletions(-) create mode 100644 lib/syspro/business_objects/comfch.rb create mode 100644 lib/syspro/business_objects/comfnd.rb create mode 100644 lib/syspro/business_objects/parsers/comfch_parser.rb create mode 100644 lib/syspro/business_objects/parsers/comfnd_parser.rb create mode 100644 lib/syspro/business_objects/schemas/comfch.xml.erb create mode 100644 lib/syspro/business_objects/schemas/comfnd.xml.erb diff --git a/lib/syspro.rb b/lib/syspro.rb index f5ffb1e..0065e21 100644 --- a/lib/syspro.rb +++ b/lib/syspro.rb @@ -21,8 +21,12 @@ require "syspro/api_operations/request" require "syspro/api_operations/query" require "syspro/business_objects/combrw" +require "syspro/business_objects/comfch" +require "syspro/business_objects/comfnd" require "syspro/business_objects/parsers/combrw_parser" +require "syspro/business_objects/parsers/comfch_parser" +require "syspro/business_objects/parsers/comfnd_parser" module Syspro @api_base = "http://syspro.wildlandlabs.com:90/SYSPROWCFService/Rest" diff --git a/lib/syspro/api_operations/query.rb b/lib/syspro/api_operations/query.rb index 31c221d..d2522d7 100644 --- a/lib/syspro/api_operations/query.rb +++ b/lib/syspro/api_operations/query.rb @@ -8,10 +8,12 @@ module Syspro request(:get, "/Query/Browse", params) end - def fetch + def fetch(params) + request(:get, "/Query/Fetch", params) end - def query + def query(params) + request(:get, "/Query/Query", params) end def find diff --git a/lib/syspro/business_objects/combrw.rb b/lib/syspro/business_objects/combrw.rb index 752c6ad..83360dc 100644 --- a/lib/syspro/business_objects/combrw.rb +++ b/lib/syspro/business_objects/combrw.rb @@ -23,7 +23,8 @@ module Syspro def parse_response(resp) handle_errors(resp) - ComBrwParser.parse(resp[0].data) + parser = ComBrwParser.new(resp[0].data) + parser.parse end def handle_errors(resp) diff --git a/lib/syspro/business_objects/comfch.rb b/lib/syspro/business_objects/comfch.rb new file mode 100644 index 0000000..a6e4e1e --- /dev/null +++ b/lib/syspro/business_objects/comfch.rb @@ -0,0 +1,39 @@ +require "syspro/business_objects/parsers/comfch_parser" +require "erb" + +module Syspro + module BusinessObjects + class ComFch < ApiResource + include Syspro::ApiOperations::Query + include Syspro::BusinessObjects::Parsers + + attr_accessor :table_name, :key, :optional_keys, :full_key_provided, + :default_type, :espresso_fetch + + def call(user_id) + xml_in = template.result(binding) + params = { "UserId" => user_id, "XmlIn" => xml_in } + resp = ComFch.fetch(params) + parse_response(resp) + end + + def template + ERB.new File.read(File.expand_path("schemas/comfch.xml.erb", File.dirname(__FILE__))), nil, "%" + end + + def parse_response(resp) + handle_errors(resp) + parser = ComFchParser.new(resp[0].data) + parser.parse + end + + def handle_errors(resp) + body = resp[0].http_body + if body.match(/^(ERROR)/) + raise SysproError, body + end + end + end + end +end + diff --git a/lib/syspro/business_objects/comfnd.rb b/lib/syspro/business_objects/comfnd.rb new file mode 100644 index 0000000..81bf8c7 --- /dev/null +++ b/lib/syspro/business_objects/comfnd.rb @@ -0,0 +1,40 @@ +require "syspro/business_objects/parsers/comfnd_parser" +require "erb" + +module Syspro + module BusinessObjects + class ComFnd < ApiResource + include Syspro::ApiOperations::Query + include Syspro::BusinessObjects::Parsers + + attr_accessor :table_name, :return_rows, :columns, :expressions, + :order_by + + def call(user_id) + xml_in = template.result(binding) + business_object = "COMFND" + params = { "UserId" => user_id, "BusinessObject" => business_object, "XmlIn" => xml_in } + resp = ComFnd.query(params) + parse_response(resp) + end + + def template + ERB.new File.read(File.expand_path("schemas/comfnd.xml.erb", File.dirname(__FILE__))), nil, "%" + end + + def parse_response(resp) + handle_errors(resp) + parser = ComFndParser.new(resp[0].data) + parser.parse + end + + def handle_errors(resp) + body = resp[0].http_body + if body.match(/^(ERROR)/) + raise SysproError, body + end + end + end + end +end + diff --git a/lib/syspro/business_objects/parsers/combrw_parser.rb b/lib/syspro/business_objects/parsers/combrw_parser.rb index 92713d7..b651a1e 100644 --- a/lib/syspro/business_objects/parsers/combrw_parser.rb +++ b/lib/syspro/business_objects/parsers/combrw_parser.rb @@ -2,8 +2,13 @@ module Syspro module BusinessObjects module Parsers class ComBrwParser + attr_reader :doc - def self.parse(doc) + def initialize(doc) + @doc = doc + end + + def parse next_prev_key = doc.first_element_child.xpath("NextPrevKey") next_prev_key_obj = next_prev_key.children.map { |el| if el.name == "text" @@ -28,11 +33,11 @@ module Syspro rows = doc.first_element_child.xpath('Row') rows_obj = rows.map { |el| - el.elements.map { |el| + el.elements.map { |inner| { - name: el.name, - value: el.xpath('Value').text, - data_type: el.xpath('DataType').text + name: inner.name, + value: inner.xpath('Value').text, + data_type: inner.xpath('DataType').text } } }.flatten(1).compact diff --git a/lib/syspro/business_objects/parsers/comfch_parser.rb b/lib/syspro/business_objects/parsers/comfch_parser.rb new file mode 100644 index 0000000..0bad7a0 --- /dev/null +++ b/lib/syspro/business_objects/parsers/comfch_parser.rb @@ -0,0 +1,29 @@ +module Syspro + module BusinessObjects + module Parsers + class ComFchParser + attr_reader :doc + + def initialize(doc) + @doc = doc + end + + def parse + table_name = doc.first_element_child.name + columns = doc.first_element_child.elements + columns_obj = columns.map { |el| + { name: el.name, value: el.children.text } + }.compact + + FetchObject.new( + table_name, + columns_obj + ) + end + + FetchObject = Struct.new(:table_name, :columns) + end + end + end +end + diff --git a/lib/syspro/business_objects/parsers/comfnd_parser.rb b/lib/syspro/business_objects/parsers/comfnd_parser.rb new file mode 100644 index 0000000..62715fb --- /dev/null +++ b/lib/syspro/business_objects/parsers/comfnd_parser.rb @@ -0,0 +1,45 @@ +module Syspro + module BusinessObjects + module Parsers + class ComFndParser + attr_reader :doc + + def initialize(doc) + @doc = doc + end + + def parse + header_details = doc.first_element_child.xpath("HeaderDetails") + header_details_obj = header_details.children.map { |el| + if el.name == "text" + next + end + { + name: el.name, + text: el.text + } + }.compact + + rows = doc.first_element_child.xpath('Row') + rows_obj = rows.map { |el| + el.elements.map { |inner| + { + name: inner.name, + value: inner.children.text + } + } + }.flatten(1).compact + + FindObject.new( + header_details_obj, + rows_obj, + doc.first_element_child.xpath('//RowsReturned').text.to_i + ) + end + + FindObject = Struct.new(:header_details, :rows, :row_count) + end + end + end +end + diff --git a/lib/syspro/business_objects/schemas/combrw.xml.erb b/lib/syspro/business_objects/schemas/combrw.xml.erb index bd413eb..1d7d8c0 100644 --- a/lib/syspro/business_objects/schemas/combrw.xml.erb +++ b/lib/syspro/business_objects/schemas/combrw.xml.erb @@ -1,8 +1,4 @@ - - <%= @browse_name %> diff --git a/lib/syspro/business_objects/schemas/comfch.xml.erb b/lib/syspro/business_objects/schemas/comfch.xml.erb new file mode 100644 index 0000000..8ee6231 --- /dev/null +++ b/lib/syspro/business_objects/schemas/comfch.xml.erb @@ -0,0 +1,14 @@ + + + <%= @table_name %> + <%= @key %> + <% unless @optional_keys.empty? %> + <% @optional_keys.each_with_index do |key, i| %> + <<%= "OptionalKey#{ i + 1 }" %>><%= key[:value] %>> + <% end %> + <% end %> + <%= @full_key_provided ? "N" : "Y" %> + <%= @default_type %> + <%= @espresso_fetch ? "N" : "Y" %> + + diff --git a/lib/syspro/business_objects/schemas/comfnd.xml.erb b/lib/syspro/business_objects/schemas/comfnd.xml.erb new file mode 100644 index 0000000..9aabe06 --- /dev/null +++ b/lib/syspro/business_objects/schemas/comfnd.xml.erb @@ -0,0 +1,29 @@ + + + <%= @table_name %> + <%= @return_rows %> + + <% for column in @columns %> + <%= column[:name] %> + <% end %> + + <% unless @expressions.empty? %> + + <% for expression in @expressions %> + + ( + <% unless expression[:andor].nil? %> + <%= expression[:andor] %> + <% end %> + <%= expression[:column] %> + <%= expression[:condition] %> + <%= expression[:value] %> + ) + + <% end %> + + <% end %> + + <%= @order_by %> + + diff --git a/test/query_test.rb b/test/query_test.rb index d818553..725d309 100644 --- a/test/query_test.rb +++ b/test/query_test.rb @@ -1,7 +1,7 @@ require "test_helper" class QueryTest < Minitest::Test - def test_query + def test_query_browse user_id = Syspro::Logon.logon("wland", "piperita2016", "L", "") combrw = Syspro::BusinessObjects::ComBrw.new @@ -15,8 +15,50 @@ class QueryTest < Minitest::Test {name: "StockCode"} ] - query_result = combrw.call(user_id.guid) + browse_result = combrw.call(user_id.guid) - refute_nil query_result + refute_nil browse_result + end + + def test_query_query + user_id = Syspro::Logon.logon("wland", "piperita2016", "L", "") + + comfnd = Syspro::BusinessObjects::ComFnd.new + comfnd.table_name = "InvMaster" + comfnd.return_rows = 5 + comfnd.columns = [ + { + name: "StockCode" + } + ] + comfnd.expressions = [ + { + andor: "And", + column: "StockCode", + condition: "EQ", + value: "02" + } + ] + comfnd.order_by = "StockCode" + + find_result = comfnd.call(user_id.guid) + + refute_nil find_result + end + + def test_query_fetch + user_id = Syspro::Logon.logon("wland", "piperita2016", "L", "") + + comfch = Syspro::BusinessObjects::ComFch.new + comfch.table_name = "InvMaster" + comfch.key = "02" + comfch.optional_keys = [] + comfch.full_key_provided = false + comfch.default_type = "" + comfch.espresso_fetch = true + + fetch_result = comfch.call(user_id.guid) + + refute_nil fetch_result end end -- libgit2 0.21.4