Commit 0f29247cffbdd7d3d8155dc45de46c2325df54d1
1 parent
96149efa
add comfch and comfnd
Showing
12 changed files
with
261 additions
and
15 deletions
Show diff stats
lib/syspro.rb
... | ... | @@ -21,8 +21,12 @@ require "syspro/api_operations/request" |
21 | 21 | require "syspro/api_operations/query" |
22 | 22 | |
23 | 23 | require "syspro/business_objects/combrw" |
24 | +require "syspro/business_objects/comfch" | |
25 | +require "syspro/business_objects/comfnd" | |
24 | 26 | |
25 | 27 | require "syspro/business_objects/parsers/combrw_parser" |
28 | +require "syspro/business_objects/parsers/comfch_parser" | |
29 | +require "syspro/business_objects/parsers/comfnd_parser" | |
26 | 30 | |
27 | 31 | module Syspro |
28 | 32 | @api_base = "http://syspro.wildlandlabs.com:90/SYSPROWCFService/Rest" | ... | ... |
lib/syspro/api_operations/query.rb
... | ... | @@ -8,10 +8,12 @@ module Syspro |
8 | 8 | request(:get, "/Query/Browse", params) |
9 | 9 | end |
10 | 10 | |
11 | - def fetch | |
11 | + def fetch(params) | |
12 | + request(:get, "/Query/Fetch", params) | |
12 | 13 | end |
13 | 14 | |
14 | - def query | |
15 | + def query(params) | |
16 | + request(:get, "/Query/Query", params) | |
15 | 17 | end |
16 | 18 | |
17 | 19 | def find | ... | ... |
lib/syspro/business_objects/combrw.rb
1 | +require "syspro/business_objects/parsers/comfch_parser" | |
2 | +require "erb" | |
3 | + | |
4 | +module Syspro | |
5 | + module BusinessObjects | |
6 | + class ComFch < ApiResource | |
7 | + include Syspro::ApiOperations::Query | |
8 | + include Syspro::BusinessObjects::Parsers | |
9 | + | |
10 | + attr_accessor :table_name, :key, :optional_keys, :full_key_provided, | |
11 | + :default_type, :espresso_fetch | |
12 | + | |
13 | + def call(user_id) | |
14 | + xml_in = template.result(binding) | |
15 | + params = { "UserId" => user_id, "XmlIn" => xml_in } | |
16 | + resp = ComFch.fetch(params) | |
17 | + parse_response(resp) | |
18 | + end | |
19 | + | |
20 | + def template | |
21 | + ERB.new File.read(File.expand_path("schemas/comfch.xml.erb", File.dirname(__FILE__))), nil, "%" | |
22 | + end | |
23 | + | |
24 | + def parse_response(resp) | |
25 | + handle_errors(resp) | |
26 | + parser = ComFchParser.new(resp[0].data) | |
27 | + parser.parse | |
28 | + end | |
29 | + | |
30 | + def handle_errors(resp) | |
31 | + body = resp[0].http_body | |
32 | + if body.match(/^(ERROR)/) | |
33 | + raise SysproError, body | |
34 | + end | |
35 | + end | |
36 | + end | |
37 | + end | |
38 | +end | |
39 | + | ... | ... |
1 | +require "syspro/business_objects/parsers/comfnd_parser" | |
2 | +require "erb" | |
3 | + | |
4 | +module Syspro | |
5 | + module BusinessObjects | |
6 | + class ComFnd < ApiResource | |
7 | + include Syspro::ApiOperations::Query | |
8 | + include Syspro::BusinessObjects::Parsers | |
9 | + | |
10 | + attr_accessor :table_name, :return_rows, :columns, :expressions, | |
11 | + :order_by | |
12 | + | |
13 | + def call(user_id) | |
14 | + xml_in = template.result(binding) | |
15 | + business_object = "COMFND" | |
16 | + params = { "UserId" => user_id, "BusinessObject" => business_object, "XmlIn" => xml_in } | |
17 | + resp = ComFnd.query(params) | |
18 | + parse_response(resp) | |
19 | + end | |
20 | + | |
21 | + def template | |
22 | + ERB.new File.read(File.expand_path("schemas/comfnd.xml.erb", File.dirname(__FILE__))), nil, "%" | |
23 | + end | |
24 | + | |
25 | + def parse_response(resp) | |
26 | + handle_errors(resp) | |
27 | + parser = ComFndParser.new(resp[0].data) | |
28 | + parser.parse | |
29 | + end | |
30 | + | |
31 | + def handle_errors(resp) | |
32 | + body = resp[0].http_body | |
33 | + if body.match(/^(ERROR)/) | |
34 | + raise SysproError, body | |
35 | + end | |
36 | + end | |
37 | + end | |
38 | + end | |
39 | +end | |
40 | + | ... | ... |
lib/syspro/business_objects/parsers/combrw_parser.rb
... | ... | @@ -2,8 +2,13 @@ module Syspro |
2 | 2 | module BusinessObjects |
3 | 3 | module Parsers |
4 | 4 | class ComBrwParser |
5 | + attr_reader :doc | |
5 | 6 | |
6 | - def self.parse(doc) | |
7 | + def initialize(doc) | |
8 | + @doc = doc | |
9 | + end | |
10 | + | |
11 | + def parse | |
7 | 12 | next_prev_key = doc.first_element_child.xpath("NextPrevKey") |
8 | 13 | next_prev_key_obj = next_prev_key.children.map { |el| |
9 | 14 | if el.name == "text" |
... | ... | @@ -28,11 +33,11 @@ module Syspro |
28 | 33 | |
29 | 34 | rows = doc.first_element_child.xpath('Row') |
30 | 35 | rows_obj = rows.map { |el| |
31 | - el.elements.map { |el| | |
36 | + el.elements.map { |inner| | |
32 | 37 | { |
33 | - name: el.name, | |
34 | - value: el.xpath('Value').text, | |
35 | - data_type: el.xpath('DataType').text | |
38 | + name: inner.name, | |
39 | + value: inner.xpath('Value').text, | |
40 | + data_type: inner.xpath('DataType').text | |
36 | 41 | } |
37 | 42 | } |
38 | 43 | }.flatten(1).compact | ... | ... |
lib/syspro/business_objects/parsers/comfch_parser.rb
0 → 100644
1 | +module Syspro | |
2 | + module BusinessObjects | |
3 | + module Parsers | |
4 | + class ComFchParser | |
5 | + attr_reader :doc | |
6 | + | |
7 | + def initialize(doc) | |
8 | + @doc = doc | |
9 | + end | |
10 | + | |
11 | + def parse | |
12 | + table_name = doc.first_element_child.name | |
13 | + columns = doc.first_element_child.elements | |
14 | + columns_obj = columns.map { |el| | |
15 | + { name: el.name, value: el.children.text } | |
16 | + }.compact | |
17 | + | |
18 | + FetchObject.new( | |
19 | + table_name, | |
20 | + columns_obj | |
21 | + ) | |
22 | + end | |
23 | + | |
24 | + FetchObject = Struct.new(:table_name, :columns) | |
25 | + end | |
26 | + end | |
27 | + end | |
28 | +end | |
29 | + | ... | ... |
lib/syspro/business_objects/parsers/comfnd_parser.rb
0 → 100644
1 | +module Syspro | |
2 | + module BusinessObjects | |
3 | + module Parsers | |
4 | + class ComFndParser | |
5 | + attr_reader :doc | |
6 | + | |
7 | + def initialize(doc) | |
8 | + @doc = doc | |
9 | + end | |
10 | + | |
11 | + def parse | |
12 | + header_details = doc.first_element_child.xpath("HeaderDetails") | |
13 | + header_details_obj = header_details.children.map { |el| | |
14 | + if el.name == "text" | |
15 | + next | |
16 | + end | |
17 | + { | |
18 | + name: el.name, | |
19 | + text: el.text | |
20 | + } | |
21 | + }.compact | |
22 | + | |
23 | + rows = doc.first_element_child.xpath('Row') | |
24 | + rows_obj = rows.map { |el| | |
25 | + el.elements.map { |inner| | |
26 | + { | |
27 | + name: inner.name, | |
28 | + value: inner.children.text | |
29 | + } | |
30 | + } | |
31 | + }.flatten(1).compact | |
32 | + | |
33 | + FindObject.new( | |
34 | + header_details_obj, | |
35 | + rows_obj, | |
36 | + doc.first_element_child.xpath('//RowsReturned').text.to_i | |
37 | + ) | |
38 | + end | |
39 | + | |
40 | + FindObject = Struct.new(:header_details, :rows, :row_count) | |
41 | + end | |
42 | + end | |
43 | + end | |
44 | +end | |
45 | + | ... | ... |
lib/syspro/business_objects/schemas/combrw.xml.erb
1 | 1 | <?xml version="1.0" encoding="Windows-1252"?> |
2 | -<!-- Copyright 1994-2014 SYSPRO Ltd.--> | |
3 | -<!-- | |
4 | - Sample XML for the Generic Browse Object | |
5 | ---> | |
6 | 2 | <Browse xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="COMBRW.XSD"> |
7 | 3 | <BrowseName><%= @browse_name %></BrowseName> |
8 | 4 | <StartAtKey/> | ... | ... |
1 | +<?xml version="1.0" encoding="Windows-1252"?> | |
2 | +<Fetch xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="COMFCH.XSD"> | |
3 | + <TableName><%= @table_name %></TableName> | |
4 | + <Key><%= @key %></Key> | |
5 | + <% unless @optional_keys.empty? %> | |
6 | + <% @optional_keys.each_with_index do |key, i| %> | |
7 | + <<%= "OptionalKey#{ i + 1 }" %>><%= key[:value] %></<%= "OptionalKey#{ i + 1 }" %>> | |
8 | + <% end %> | |
9 | + <% end %> | |
10 | + <FullKeyProvided><%= @full_key_provided ? "N" : "Y" %></FullKeyProvided> | |
11 | + <DefaultType><%= @default_type %></DefaultType> | |
12 | + <EspressoFetch><%= @espresso_fetch ? "N" : "Y" %></EspressoFetch> | |
13 | +</Fetch> | |
14 | + | ... | ... |
1 | +<?xml version="1.0" encoding="Windows-1252"?> | |
2 | +<Query xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="COMFND.XSD"> | |
3 | + <TableName><%= @table_name %></TableName> | |
4 | + <ReturnRows><%= @return_rows %></ReturnRows> | |
5 | + <Columns> | |
6 | + <% for column in @columns %> | |
7 | + <Column><%= column[:name] %></Column> | |
8 | + <% end %> | |
9 | + </Columns> | |
10 | + <% unless @expressions.empty? %> | |
11 | + <Where> | |
12 | + <% for expression in @expressions %> | |
13 | + <Expression> | |
14 | + <OpenBracket>(</OpenBracket> | |
15 | + <% unless expression[:andor].nil? %> | |
16 | + <AndOr><%= expression[:andor] %></AndOr> | |
17 | + <% end %> | |
18 | + <Column><%= expression[:column] %></Column> | |
19 | + <Condition><%= expression[:condition] %></Condition> | |
20 | + <Value><%= expression[:value] %></Value> | |
21 | + <CloseBracket>)</CloseBracket> | |
22 | + </Expression> | |
23 | + <% end %> | |
24 | + </Where> | |
25 | + <% end %> | |
26 | + <OrderBy> | |
27 | + <Column><%= @order_by %></Column> | |
28 | + </OrderBy> | |
29 | +</Query> | ... | ... |
test/query_test.rb
1 | 1 | require "test_helper" |
2 | 2 | |
3 | 3 | class QueryTest < Minitest::Test |
4 | - def test_query | |
4 | + def test_query_browse | |
5 | 5 | user_id = Syspro::Logon.logon("wland", "piperita2016", "L", "") |
6 | 6 | |
7 | 7 | combrw = Syspro::BusinessObjects::ComBrw.new |
... | ... | @@ -15,8 +15,50 @@ class QueryTest < Minitest::Test |
15 | 15 | {name: "StockCode"} |
16 | 16 | ] |
17 | 17 | |
18 | - query_result = combrw.call(user_id.guid) | |
18 | + browse_result = combrw.call(user_id.guid) | |
19 | 19 | |
20 | - refute_nil query_result | |
20 | + refute_nil browse_result | |
21 | + end | |
22 | + | |
23 | + def test_query_query | |
24 | + user_id = Syspro::Logon.logon("wland", "piperita2016", "L", "") | |
25 | + | |
26 | + comfnd = Syspro::BusinessObjects::ComFnd.new | |
27 | + comfnd.table_name = "InvMaster" | |
28 | + comfnd.return_rows = 5 | |
29 | + comfnd.columns = [ | |
30 | + { | |
31 | + name: "StockCode" | |
32 | + } | |
33 | + ] | |
34 | + comfnd.expressions = [ | |
35 | + { | |
36 | + andor: "And", | |
37 | + column: "StockCode", | |
38 | + condition: "EQ", | |
39 | + value: "02" | |
40 | + } | |
41 | + ] | |
42 | + comfnd.order_by = "StockCode" | |
43 | + | |
44 | + find_result = comfnd.call(user_id.guid) | |
45 | + | |
46 | + refute_nil find_result | |
47 | + end | |
48 | + | |
49 | + def test_query_fetch | |
50 | + user_id = Syspro::Logon.logon("wland", "piperita2016", "L", "") | |
51 | + | |
52 | + comfch = Syspro::BusinessObjects::ComFch.new | |
53 | + comfch.table_name = "InvMaster" | |
54 | + comfch.key = "02" | |
55 | + comfch.optional_keys = [] | |
56 | + comfch.full_key_provided = false | |
57 | + comfch.default_type = "" | |
58 | + comfch.espresso_fetch = true | |
59 | + | |
60 | + fetch_result = comfch.call(user_id.guid) | |
61 | + | |
62 | + refute_nil fetch_result | |
21 | 63 | end |
22 | 64 | end | ... | ... |