horizon_client 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +45 -4
- data/horizon_client.gemspec +1 -2
- data/lib/horizon_client/collection.rb +30 -0
- data/lib/horizon_client/entity.rb +45 -0
- data/lib/horizon_client/request/encode_xml.rb +14 -0
- data/lib/horizon_client/resource.rb +25 -0
- data/lib/horizon_client/response/parse_xml.rb +17 -0
- data/lib/horizon_client/version.rb +1 -1
- data/lib/horizon_client.rb +10 -24
- metadata +10 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 06b84f8009e74940b022cfb6c7feb583e068d51c
|
4
|
+
data.tar.gz: b2670e249a5646c5b68c23343ff62b0f2beaf6d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 00ca8cad1ed53bf68bb309a446adcaff7dfddd124b572b3c8a6dbbb38e496ba9fdd984cb7036419fa79f2a8562ecc257ca88d52bce774835c830d8124dba47d7
|
7
|
+
data.tar.gz: 1cd29b3ef83dfd52158d679dd1693cd84a14cc296499ac7320ff89415959e36715fbcd078b609d2e8e645b456b4cc696d486bd42291730100caaba13332cb0d8
|
data/README.md
CHANGED
@@ -7,12 +7,53 @@ Client to use with Horizon REST xml API.
|
|
7
7
|
```ruby
|
8
8
|
client = HorizonClient.new
|
9
9
|
|
10
|
-
|
10
|
+
# incoming xml:
|
11
|
+
# <?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
12
|
+
# <resource>
|
13
|
+
# <entity>
|
14
|
+
# <name>Jack</name>
|
15
|
+
# <foo><bar>Jill</bar></foo>
|
16
|
+
# </entity>
|
17
|
+
# <collection>
|
18
|
+
# <row>
|
19
|
+
# </row>
|
20
|
+
# <row>
|
21
|
+
# </row>
|
22
|
+
# </collection>
|
23
|
+
# </resource>
|
11
24
|
|
12
|
-
|
13
|
-
|
25
|
+
# returns and expects HorizonClient::Resource object
|
26
|
+
resource = client.get('path')
|
27
|
+
return_resource = client.post('path', resource)
|
28
|
+
|
29
|
+
# get entity:
|
30
|
+
entity = resource.entity
|
31
|
+
|
32
|
+
# get and set object values
|
33
|
+
entity['name'] # => 'Jack'
|
34
|
+
entity['name'] = 'Jane'
|
35
|
+
entity['foo/bar'] # => 'Jill'
|
36
|
+
entity['foo/baz'] = 'Joe' # => <foo><bar>Jill</bar><baz>Joe</baz></foo>
|
14
37
|
|
15
|
-
|
38
|
+
# get or set collection with specified name inside entity
|
39
|
+
col = entity.get_collection('col')
|
40
|
+
row = col.build # => #<HorizonClient::Entity>
|
41
|
+
# result:
|
42
|
+
# <entity>
|
43
|
+
# ...
|
44
|
+
# <col>
|
45
|
+
# <row></row>
|
46
|
+
# </col>
|
47
|
+
# </entity>
|
48
|
+
|
49
|
+
# get first level collection.
|
50
|
+
collection = resource.collection
|
51
|
+
entity_array = collection.rows # => [ #<HorizonClient::Entity> ]
|
52
|
+
entity_array.each do |entity|
|
53
|
+
entity['foo'] = 'test'
|
54
|
+
end
|
55
|
+
|
56
|
+
```
|
16
57
|
|
17
58
|
|
18
59
|
### Necessary environment variables
|
data/horizon_client.gemspec
CHANGED
@@ -28,6 +28,5 @@ Gem::Specification.new do |spec|
|
|
28
28
|
spec.add_development_dependency "webmock", "~> 2.0"
|
29
29
|
|
30
30
|
spec.add_dependency 'faraday', '~> 0.11'
|
31
|
-
spec.add_dependency '
|
32
|
-
spec.add_dependency 'faraday_middleware', '~> 0.11'
|
31
|
+
spec.add_dependency 'ox', '~> 2.4'
|
33
32
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module HorizonClient
|
2
|
+
class Collection
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
attr_reader :node
|
6
|
+
|
7
|
+
def initialize(node)
|
8
|
+
@node = node
|
9
|
+
@rows = @node.locate('row').map do |row|
|
10
|
+
Entity.new(row)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def build
|
15
|
+
row = Ox::Element.new('row')
|
16
|
+
node << row
|
17
|
+
entity = Entity.new(row)
|
18
|
+
@rows.push(entity)
|
19
|
+
entity
|
20
|
+
end
|
21
|
+
|
22
|
+
def rows
|
23
|
+
@rows
|
24
|
+
end
|
25
|
+
|
26
|
+
def each(&block)
|
27
|
+
@rows.each(&block)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module HorizonClient
|
2
|
+
class Entity
|
3
|
+
attr_reader :node
|
4
|
+
|
5
|
+
def initialize(node)
|
6
|
+
@node = node
|
7
|
+
end
|
8
|
+
|
9
|
+
def [](name)
|
10
|
+
attr_node = node.locate(name).first
|
11
|
+
get_value attr_node
|
12
|
+
end
|
13
|
+
|
14
|
+
def []=(name, value)
|
15
|
+
elem = find_or_build_attribute(name.split('/'), node)
|
16
|
+
elem.replace_text(value)
|
17
|
+
end
|
18
|
+
|
19
|
+
def get_collection(name)
|
20
|
+
collection_node = find_or_build_attribute(name.split('/'), node)
|
21
|
+
Collection.new(collection_node)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def find_or_build_attribute(path, parent)
|
27
|
+
name = path.shift
|
28
|
+
unless child = parent.locate(name).first
|
29
|
+
child = Ox::Element.new(name)
|
30
|
+
parent << child
|
31
|
+
end
|
32
|
+
find_or_build_attribute(path, child) unless path.empty?
|
33
|
+
|
34
|
+
child
|
35
|
+
end
|
36
|
+
|
37
|
+
def get_value(node)
|
38
|
+
if node.respond_to?('href')
|
39
|
+
node.href.text
|
40
|
+
else
|
41
|
+
node.text
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module HorizonClient
|
2
|
+
module Request
|
3
|
+
class EncodeXml < Faraday::Middleware
|
4
|
+
def call(env)
|
5
|
+
env[:body] = encode env[:body]
|
6
|
+
@app.call env
|
7
|
+
end
|
8
|
+
|
9
|
+
def encode(payload)
|
10
|
+
payload.is_a?(Resource) ? Ox.dump(payload.document, with_xml: true) : payload
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module HorizonClient
|
2
|
+
class Resource
|
3
|
+
attr_reader :document
|
4
|
+
|
5
|
+
def initialize(xml)
|
6
|
+
@document = Ox.parse(xml)
|
7
|
+
end
|
8
|
+
|
9
|
+
def entity
|
10
|
+
node = @document.resource.entity
|
11
|
+
Entity.new(node)
|
12
|
+
end
|
13
|
+
|
14
|
+
def collection
|
15
|
+
node = @document.resource.collection
|
16
|
+
@collection ||= Collection.new(node)
|
17
|
+
end
|
18
|
+
|
19
|
+
def error
|
20
|
+
if document.respond_to?('error')
|
21
|
+
document.error.message.text
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module HorizonClient
|
2
|
+
module Response
|
3
|
+
|
4
|
+
class ResourceError < StandardError
|
5
|
+
end
|
6
|
+
|
7
|
+
class ParseXml < Faraday::Response::Middleware
|
8
|
+
def parse(body)
|
9
|
+
resource = Resource.new(body)
|
10
|
+
|
11
|
+
raise ResourceError.new(resource.error) if resource.error
|
12
|
+
|
13
|
+
resource
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/horizon_client.rb
CHANGED
@@ -1,36 +1,27 @@
|
|
1
1
|
require "horizon_client/version"
|
2
2
|
require "faraday"
|
3
|
-
require "
|
4
|
-
|
3
|
+
require "ox"
|
4
|
+
|
5
|
+
require 'horizon_client/response/parse_xml'
|
6
|
+
require 'horizon_client/request/encode_xml'
|
7
|
+
|
8
|
+
require "horizon_client/resource"
|
9
|
+
require "horizon_client/collection"
|
10
|
+
require "horizon_client/entity"
|
5
11
|
|
6
12
|
module HorizonClient
|
7
13
|
def self.new(*args)
|
8
14
|
Connection.new(*args)
|
9
15
|
end
|
10
16
|
|
11
|
-
class ClientError < Faraday::ClientError
|
12
|
-
def initialize(e)
|
13
|
-
message = e.message
|
14
|
-
if e.response.is_a?(Hash)
|
15
|
-
body = e.response[:body]
|
16
|
-
if body.is_a?(Hash)
|
17
|
-
error = body['error']
|
18
|
-
if error.is_a?(Hash)
|
19
|
-
message += ": #{error['message']}"
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
super message
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
17
|
class Connection
|
28
18
|
def initialize(url = nil)
|
29
19
|
url ||= ENV['HORIZON_REST_URL']
|
30
20
|
|
31
21
|
@connection = Faraday.new url do |conn|
|
32
22
|
conn.response :raise_error
|
33
|
-
conn.
|
23
|
+
conn.use HorizonClient::Response::ParseXml
|
24
|
+
conn.use HorizonClient::Request::EncodeXml
|
34
25
|
|
35
26
|
conn.adapter Faraday.default_adapter
|
36
27
|
end
|
@@ -43,9 +34,6 @@ module HorizonClient
|
|
43
34
|
def get(path = '', params = {})
|
44
35
|
response = @connection.get path, params
|
45
36
|
response.body
|
46
|
-
|
47
|
-
rescue Faraday::ClientError => e
|
48
|
-
raise ClientError.new(e)
|
49
37
|
end
|
50
38
|
|
51
39
|
def post(path = '', body)
|
@@ -56,8 +44,6 @@ module HorizonClient
|
|
56
44
|
end
|
57
45
|
|
58
46
|
response.body
|
59
|
-
rescue Faraday::ClientError => e
|
60
|
-
raise ClientError.new(e)
|
61
47
|
end
|
62
48
|
end
|
63
49
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: horizon_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- MAK IT
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-03-
|
11
|
+
date: 2017-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -81,33 +81,19 @@ dependencies:
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0.11'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: ox
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
89
|
+
version: '2.4'
|
90
90
|
type: :runtime
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: faraday_middleware
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- - "~>"
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: '0.11'
|
104
|
-
type: :runtime
|
105
|
-
prerelease: false
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
107
|
-
requirements:
|
108
|
-
- - "~>"
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: '0.11'
|
96
|
+
version: '2.4'
|
111
97
|
description: Client for Horizon by VISMA accounting REST xml API.
|
112
98
|
email:
|
113
99
|
- martins.lapsa@makit.lv
|
@@ -125,6 +111,11 @@ files:
|
|
125
111
|
- bin/setup
|
126
112
|
- horizon_client.gemspec
|
127
113
|
- lib/horizon_client.rb
|
114
|
+
- lib/horizon_client/collection.rb
|
115
|
+
- lib/horizon_client/entity.rb
|
116
|
+
- lib/horizon_client/request/encode_xml.rb
|
117
|
+
- lib/horizon_client/resource.rb
|
118
|
+
- lib/horizon_client/response/parse_xml.rb
|
128
119
|
- lib/horizon_client/version.rb
|
129
120
|
homepage: http://rubygems.org/gems/horizon_client
|
130
121
|
licenses:
|