dynaccount 0.9.8 → 0.9.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/dynaccount.rb +6 -5
- data/lib/dynaccount/action.rb +1 -1
- data/lib/dynaccount/dynaccount_object.rb +27 -7
- data/lib/dynaccount/query_builder.rb +54 -0
- data/lib/dynaccount/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a26d67f18104d7a05a069ea9791dd57c5640edba5a58389e8ca6acd3712a5c0a
|
4
|
+
data.tar.gz: 26bccedbd940bf8427cfc1c1a53f1558075e2a1f594358ac3c7122262cc84f54
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f30201ed6512c1e90f95d7584f0ec5f7e68f57986c91a1bbf3c7946b76eac13128e98f79d3acf040f5d8dd9827044cc47a586aba46306dc36c6a6ef905a071da
|
7
|
+
data.tar.gz: f8a8f9c8ce4bc1908ebaf30ca578c94fd43d5fef5ac14fc444a2d5a574682fc6f8734059a9f96ca86a174647f9f5f768ee80ae7a994e338a505d5542cd6358a1
|
data/README.md
CHANGED
data/lib/dynaccount.rb
CHANGED
@@ -72,6 +72,8 @@ require 'dynaccount/vatcode'
|
|
72
72
|
require 'dynaccount/year'
|
73
73
|
require 'dynaccount/year_period'
|
74
74
|
|
75
|
+
require 'dynaccount/query_builder'
|
76
|
+
|
75
77
|
module Dynaccount
|
76
78
|
@base_url = 'api.dynaccount.com'
|
77
79
|
|
@@ -83,13 +85,14 @@ module Dynaccount
|
|
83
85
|
faraday.request :url_encoded
|
84
86
|
logger = Logger.new STDOUT
|
85
87
|
logger.level = debug ? Logger::DEBUG : Logger::INFO
|
86
|
-
faraday.response :logger, logger, bodies:
|
88
|
+
faraday.response :logger, logger, bodies: true
|
87
89
|
faraday.adapter :net_http_persistent
|
88
90
|
end
|
89
91
|
|
90
92
|
@api_connection.post do |req|
|
91
93
|
req.url url
|
92
|
-
req.
|
94
|
+
req.headers['X-Hash'] = api_hash(request_url(url), params)
|
95
|
+
req.body = URI.encode_www_form(params).to_s
|
93
96
|
end
|
94
97
|
end
|
95
98
|
|
@@ -98,9 +101,7 @@ module Dynaccount
|
|
98
101
|
end
|
99
102
|
|
100
103
|
def api_hash(url, params = {})
|
101
|
-
|
102
|
-
(Digest::SHA1.new << "#{url}#{URI.encode_www_form(params)}#{api_secret}")
|
103
|
-
.to_s
|
104
|
+
(Digest::SHA1.new << "#{url}#{URI.encode_www_form(params)}#{api_secret}").to_s
|
104
105
|
end
|
105
106
|
end
|
106
107
|
end
|
data/lib/dynaccount/action.rb
CHANGED
@@ -32,7 +32,7 @@ module Dynaccount
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def self.create(attributes = {})
|
35
|
-
req = JSON.parse(Dynaccount.request(url(nil, 'put'), attributes, :post).body)
|
35
|
+
req = JSON.parse(Dynaccount.request(url(nil, 'put'), attributes, :post).body).fetch('result', []).map { |res| new(res) }
|
36
36
|
req[0]
|
37
37
|
end
|
38
38
|
|
@@ -41,24 +41,44 @@ module Dynaccount
|
|
41
41
|
end
|
42
42
|
|
43
43
|
def self.find(id)
|
44
|
-
req = JSON.parse(Dynaccount.request(url(id, 'get'), {}, :post).body)
|
44
|
+
req = JSON.parse(Dynaccount.request(url(id, 'get'), {}, :post).body).fetch('result', []).map { |res| new(res) }
|
45
45
|
return req[0] if req.size == 1
|
46
46
|
req
|
47
|
-
rescue JSON::ParserError =>
|
47
|
+
rescue JSON::ParserError => _e
|
48
48
|
return nil
|
49
49
|
end
|
50
50
|
|
51
51
|
def self.find_by(params = {})
|
52
|
-
req = JSON.parse(Dynaccount.request(url(nil, 'get', params), {}, :post).body)
|
52
|
+
req = JSON.parse(Dynaccount.request(url(nil, 'get', params), {}, :post).body).fetch('result', []).map { |res| new(res) }
|
53
53
|
return req[0] if req.size == 1
|
54
54
|
req
|
55
|
-
rescue JSON::ParserError =>
|
55
|
+
rescue JSON::ParserError => _e
|
56
56
|
return nil
|
57
57
|
end
|
58
58
|
|
59
|
+
def self.run_query(select: [], where: {}, limit: nil, offset: nil, order: [])
|
60
|
+
params = {}
|
61
|
+
params.merge!(select: select.join('%2C')) if select&.any?
|
62
|
+
params.merge!(where.tap {|w| w&.delete(:select) }) if where
|
63
|
+
params.merge!(order: "#{order[0]}+#{order[1]}") if order&.count == 2
|
64
|
+
params.merge!(limit: "#{[offset, limit].select {|i| i.is_a?(Integer) }.join('%2C')}") if [offset, limit].select {|i| i.is_a?(Integer) }.any?
|
65
|
+
|
66
|
+
JSON.parse(
|
67
|
+
Dynaccount.request(url(nil, 'get', params), {}, :post).body
|
68
|
+
).fetch('result', []).map { |res| new(res) }
|
69
|
+
rescue JSON::ParserError => _e
|
70
|
+
return nil
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.query
|
74
|
+
Dynaccount::QueryBuilder.new(self)
|
75
|
+
end
|
76
|
+
|
59
77
|
def self.url(id, action, params = {})
|
60
|
-
url = "/
|
61
|
-
url += "?" + params.map
|
78
|
+
url = "/v6/#{Dynaccount.api_id}/#{Dynaccount.api_key}/#{action}/#{api_path}/#{"#{id}/" unless id.nil?}"
|
79
|
+
url += "?" + params.map do |k,v|
|
80
|
+
"#{k}=#{v.to_s.gsub(/[^a-zA-Z0-9_\-.]/n) { sprintf("%%%02X", $&.unpack("C")[0]) }.encode('utf-8')}"
|
81
|
+
end.sort.join('&') if params.any?
|
62
82
|
url
|
63
83
|
end
|
64
84
|
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Dynaccount
|
2
|
+
class QueryBuilder
|
3
|
+
attr_accessor :klass, :selects, :wheres, :limits, :offsets, :orders
|
4
|
+
|
5
|
+
def initialize(klass)
|
6
|
+
self.klass = klass
|
7
|
+
end
|
8
|
+
|
9
|
+
def order(o)
|
10
|
+
if o.is_a?(Hash)
|
11
|
+
unless [:DESC, :ASC].include?(o[o.keys.first].to_sym)
|
12
|
+
raise ArgumentError.new("Wrong argument #{o[o.keys.first]}. Should be :ASC or :DESC")
|
13
|
+
end
|
14
|
+
|
15
|
+
self.orders = [o.keys.first, o[o.keys.first].to_sym]
|
16
|
+
elsif [String, Symbol].include?(o.class)
|
17
|
+
self.orders = [o.to_s, :asc]
|
18
|
+
else
|
19
|
+
self.orders = nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def select(*args)
|
24
|
+
self.selects = args.map(&:to_s)
|
25
|
+
rescue NoMethodError
|
26
|
+
raise ArgumentError.new("Wrong argument #{args}. All should be castable to string")
|
27
|
+
end
|
28
|
+
|
29
|
+
def where(**args)
|
30
|
+
self.wheres = args
|
31
|
+
rescue NoMethodError
|
32
|
+
raise ArgumentError.new("Wrong argument #{args}. All should be castable to string")
|
33
|
+
end
|
34
|
+
|
35
|
+
def limit(l)
|
36
|
+
self.limits = l
|
37
|
+
end
|
38
|
+
|
39
|
+
def offset(o)
|
40
|
+
self.offsets = o
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
def execute
|
45
|
+
klass.run_query(
|
46
|
+
select: self.selects,
|
47
|
+
where: self.wheres,
|
48
|
+
limit: self.limits,
|
49
|
+
offset: self.offsets,
|
50
|
+
order: self.orders
|
51
|
+
)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/lib/dynaccount/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynaccount
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Frederik Spang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-12-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -131,6 +131,7 @@ files:
|
|
131
131
|
- lib/dynaccount/product_list.rb
|
132
132
|
- lib/dynaccount/product_list_product.rb
|
133
133
|
- lib/dynaccount/product_supplier.rb
|
134
|
+
- lib/dynaccount/query_builder.rb
|
134
135
|
- lib/dynaccount/stock_adjustment.rb
|
135
136
|
- lib/dynaccount/unit.rb
|
136
137
|
- lib/dynaccount/vatcode.rb
|