dynaccount 0.9.8 → 0.9.10

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: baf9f799dcdf1ef93d98008cad8bd878c6542463dea6853741141b440e6d02a1
4
- data.tar.gz: 1b8605d21cbac08eea7a5fbb345ec937f923cb41fe1435f3eeeffda423c005e5
3
+ metadata.gz: a26d67f18104d7a05a069ea9791dd57c5640edba5a58389e8ca6acd3712a5c0a
4
+ data.tar.gz: 26bccedbd940bf8427cfc1c1a53f1558075e2a1f594358ac3c7122262cc84f54
5
5
  SHA512:
6
- metadata.gz: ef973f5d915ebb8b7ecfb0a8b88b581c711260d9167e6473c38c787a328430a2be882e742ba4936ca87632cd26b3058688c1357afe5aaf46b04c89e68061d981
7
- data.tar.gz: c3c2c17adfa369fb4b54dabd85d5b05f005fc639f77fd99e2881833fe8848aba31d7ce5adcbac2d80ad37c05195203e360bd4661a1efcbf500cf515e2229c03b
6
+ metadata.gz: f30201ed6512c1e90f95d7584f0ec5f7e68f57986c91a1bbf3c7946b76eac13128e98f79d3acf040f5d8dd9827044cc47a586aba46306dc36c6a6ef905a071da
7
+ data.tar.gz: f8a8f9c8ce4bc1908ebaf30ca578c94fd43d5fef5ac14fc444a2d5a574682fc6f8734059a9f96ca86a174647f9f5f768ee80ae7a994e338a505d5542cd6358a1
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Dynaccount
2
2
  This dynaccount gem is created to create an ActiveRecord-like interaction to [Dynaccount](https://dynaccount.com), accounting system's API.
3
3
 
4
- The current API-version is `v5`.
4
+ The current API-version is `v6`.
5
5
 
6
6
  ## Usage
7
7
 
@@ -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: (debug || false)
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.body = "#{URI.encode_www_form(params)}#{'&' unless params.empty?}__api_hash=#{api_hash(request_url(url), params)}"
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
- # p "#{url}#{URI.encode_www_form(params)}#{api_secret}"
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
@@ -9,7 +9,7 @@ module Dynaccount
9
9
  end
10
10
 
11
11
  def self.url(action)
12
- "/v5/#{Dynaccount.api_id}/#{Dynaccount.api_key}/action/#{action}/json/"
12
+ "/v6/#{Dynaccount.api_id}/#{Dynaccount.api_key}/action/#{action}/"
13
13
  end
14
14
  end
15
15
  end
@@ -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)['result'].map { |res| new(res) }
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)['result'].map { |res| new(res) }
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 => e
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)['result'].map { |res| new(res) }
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 => e
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 = "/v5/#{Dynaccount.api_id}/#{Dynaccount.api_key}/#{action}/#{api_path}/json/#{"#{id}/" unless id.nil?}"
61
- url += "?" + params.map { |k,v| "#{k}=#{v}" }.join('&') if params.any?
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
@@ -1,3 +1,3 @@
1
1
  module Dynaccount
2
- VERSION = '0.9.8'.freeze
2
+ VERSION = '0.9.10'.freeze
3
3
  end
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.8
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-07 00:00:00.000000000 Z
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