economic-rest 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d5bb0d48dd081895b21a673c69be152779d24bb7c69573d3700a059242410dca
4
- data.tar.gz: 381297d78b417851357fee249ab2c86af0f9cba81c1d3adc7cceb2026d08bc29
3
+ metadata.gz: 708eea9e22edcc7c7ebdb0d619bcc7a61e088209ee0da3db606db8aeda83cd75
4
+ data.tar.gz: e7acc224d6e5f1dada3be67e7cc9f505b2a90c057140a3eb1f6b150d22367114
5
5
  SHA512:
6
- metadata.gz: bc1637c6c5f36d3a47639635c12c1d91e80b04334808e4644e59fecacd6e011d1ca5821650af46700fef5eeef101b168fc5f3fabde3a7301ece1b12d8ec80f51
7
- data.tar.gz: e85e9e5e8bc902af85a57dd9d92f85976bfe9c4ec02b99b30df0cdc478f26058ed7f4b6e18ad8be48052e16801b6a97c565a68c00207c7948d2034c518cec6db
6
+ metadata.gz: b968674ace73f8c88f64d066e55819aa79800a2fba25bc600c5b490bc9ad6773ac32ba985635c5fecf4903d7239fd0d4497b1b979bfbb72a571455dcefb6b7a7
7
+ data.tar.gz: ebaef0843aabde82c6160a84565a440181a95f4d3115b711bdeb449f11598df4dee4a404a7eef6a3d9b677a96c32f4f33fab0801e5340248ce410e7fa350f513
data/.gitignore CHANGED
@@ -3,3 +3,5 @@
3
3
  # If you find yourself ignoring temporary files generated by your text editor
4
4
  # or operating system, you probably want to add a global ignore instead:
5
5
  # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ /pkg*
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- economic-rest (0.1.8)
4
+ economic-rest (0.1.9)
5
5
  rest-client
6
6
 
7
7
  GEM
data/lib/economic/base.rb CHANGED
@@ -5,13 +5,17 @@ module Economic
5
5
  end
6
6
 
7
7
  class << self
8
- attr_reader :attributes
8
+ attr_reader :attributes, :relations
9
9
  end
10
10
 
11
11
  def self.add_attribute(name)
12
12
  (@attributes ||= []).push(name)
13
13
  end
14
14
 
15
+ def self.add_relation(name, fields)
16
+ (@relations ||= []).push(name: name, fields: fields)
17
+ end
18
+
15
19
  def self.field(name, id: false, model: nil)
16
20
  economic_cased_attibute_name = name.to_s
17
21
  attr_accessor economic_cased_attibute_name
@@ -27,48 +31,49 @@ module Economic
27
31
  end
28
32
 
29
33
  def self.relation(name, fields:)
30
- related_model = Object.const_get('Economic::' + name.slice(0, 1).capitalize + name.slice(1..-1)).new({})
31
- define_method(name) do
32
- @internal_hash[name] = related_model.to_h(fields: fields)
33
- related_model
34
- end
35
- alias_method snake_case(name), name
34
+ economic_cased_attibute_name = name.to_s
35
+ add_relation economic_cased_attibute_name, fields
36
+ attr_reader economic_cased_attibute_name
37
+ alias_method snake_case(economic_cased_attibute_name), economic_cased_attibute_name
36
38
  end
37
39
 
38
40
  def values_based_on_hash(hash)
39
- Hash.class_eval { include ExtraMethods }
40
- @internal_hash = hash
41
- @internal_hash.each do |k, v|
42
- k = k.to_s
43
- if self.class.attributes.include? k
44
- if v.class == Hash
45
- v.keys.count.times do |i|
46
- v.alias!(Base.snake_case(v.keys[i]), v.keys[i])
47
- end
48
- end
49
- send("#{k}=", v)
50
- else
51
- warn "unassigned k #{k} in #{self.class.name}" unless %w[layout self soap metaData].include? k
41
+ @internal_hash = hash || {}
42
+
43
+ self.class.attributes.each do |field_name|
44
+ public_send("#{field_name}=", @internal_hash[field_name])
45
+ end
46
+
47
+ if self.class.relations
48
+ self.class.relations.each do |relation_hash|
49
+ name = relation_hash[:name]
50
+ related_model = model_class(name).new(@internal_hash[name])
51
+
52
+ instance_variable_set("@#{name}", related_model)
52
53
  end
53
54
  end
54
55
  end
55
56
 
56
- def to_h(fields: [])
57
- self.class.attributes.each do |attribute|
58
- @internal_hash[attribute.to_sym] = send(attribute) unless send(attribute).nil?
59
- end
60
- @internal_hash.each do |k, _v|
61
- send(k) if respond_to? k
57
+ def to_h(only_fields: [])
58
+ return_hash = {}
59
+
60
+ self.class.attributes.each do |field_name|
61
+ next if only_fields.any? && !only_fields.include?(field_name.to_sym)
62
+
63
+ return_hash[field_name] = public_send(field_name) if public_send(field_name)
62
64
  end
63
- unless fields.empty?
64
- # because we have field we should only return those values from the hash
65
- limited_hash = {}
66
- fields.each do |field|
67
- limited_hash[field.to_sym] = send(field) unless send(field).nil?
65
+
66
+ if self.class.relations
67
+ self.class.relations.each do |relation_hash|
68
+ relation_name = relation_hash[:name]
69
+ relation_fields = relation_hash[:fields]
70
+
71
+ relation_data = public_send(relation_name).to_h(only_fields: relation_fields)
72
+
73
+ return_hash[relation_name] = relation_data unless relation_data.empty?
68
74
  end
69
- return limited_hash
70
75
  end
71
- @internal_hash
76
+ return_hash
72
77
  end
73
78
 
74
79
  def dirty?
@@ -78,13 +83,6 @@ module Economic
78
83
  false
79
84
  end
80
85
 
81
- def save
82
- if dirty?
83
- response = repo.save(self)
84
- values_based_on_hash(JSON.parse(response.body))
85
- end
86
- end
87
-
88
86
  def self.snake_case(camel_cased)
89
87
  camel_cased.to_s.gsub(/::/, '/')
90
88
  .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
@@ -101,12 +99,9 @@ module Economic
101
99
  def repo
102
100
  Object.const_get("#{self.class}Repo")
103
101
  end
104
- end
105
- end
106
102
 
107
- module ExtraMethods
108
- def alias!(newkey, oldkey)
109
- self[newkey] = self[oldkey] if key?(oldkey)
110
- self
103
+ def model_class(name)
104
+ Object.const_get('Economic::' + name.slice(0, 1).capitalize + name.slice(1..-1))
105
+ end
111
106
  end
112
107
  end
@@ -0,0 +1,10 @@
1
+ module Economic
2
+ class Delivery < Base
3
+ field :address
4
+ field :city
5
+ field :country
6
+ field :deliveryDate
7
+ field :deliveryTerms
8
+ field :zip
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ module Economic
2
+ class Layout < Base
3
+ field :layoutNumber
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ module Economic
2
+ class Notes < Base
3
+ field :heading
4
+ field :textLine1
5
+ field :textLine2
6
+ end
7
+ end
@@ -17,14 +17,14 @@ module Economic
17
17
  field :roundingAmount
18
18
  field :vatAmount
19
19
 
20
- field :customer
21
- field :delivery
22
- field :notes
23
- field :paymentTerms
24
- field :pdf
25
- field :project
26
- field :recipient
27
- field :references
28
- field :templates
20
+ relation :customer, fields: [:customerNumber]
21
+ # relation :delivery, fields: []
22
+ # relation :notes, fields: []
23
+ relation :paymentTerms, fields: [:paymentTermsNumber]
24
+ # relation :pdf, fields: []
25
+ # relation :project, fields: []
26
+ relation :recipient, fields: [:name]
27
+ # relation :references, fields: []
28
+ relation :layout, fields: [:layoutNumber]
29
29
  end
30
30
  end
@@ -4,10 +4,12 @@ module Economic
4
4
  def self.send(model)
5
5
  url = ''
6
6
  url << URL
7
- url << '/orders'
8
- url << '/sent'
9
- response = RestClient.post(url, model.to_h.to_json, headers)
10
- response
7
+ url << 'orders'
8
+ url << '/drafts'
9
+ response = RestClient.post(url, model.to_h.to_json, headers) do |response, _request, _result|
10
+ response
11
+ end
12
+ test_response(response)
11
13
  end
12
14
  end
13
15
  end
@@ -4,10 +4,12 @@ module Economic
4
4
  def self.send(model)
5
5
  url = ''
6
6
  url << URL
7
- url << '/orders'
8
- url << '/drafts'
9
- response = RestClient.post(url, model.to_h.to_json, headers)
10
- response
7
+ url << 'orders'
8
+ url << '/sent'
9
+ response = RestClient.post(url, model.to_h.to_json, headers) do |response, _request, _result|
10
+ response
11
+ end
12
+ test_response(response)
11
13
  end
12
14
  end
13
15
  end
@@ -0,0 +1,8 @@
1
+ module Economic
2
+ class PaymentTerms < Base
3
+ field :daysOfCredit
4
+ field :name
5
+ field :paymentTermsNumber
6
+ field :paymentTermsType
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ module Economic
2
+ class Pdf < Base
3
+ field :self
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Economic
2
+ class Project < Base
3
+ field :projectNumber
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ module Economic
2
+ class Recipient < Base
3
+ field :address
4
+ field :name
5
+
6
+ relation :vatZone, fields: []
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ module Economic
2
+ class References < Base
3
+ field :other
4
+ end
5
+ end
data/lib/economic/rest.rb CHANGED
@@ -3,6 +3,18 @@ require 'rest-client'
3
3
 
4
4
  require 'economic/base_repo'
5
5
  require 'economic/base'
6
+
7
+ require 'economic/vat_zone'
8
+
9
+ require 'economic/delivery'
10
+ require 'economic/layout'
11
+ require 'economic/notes'
12
+ require 'economic/payment_terms'
13
+ require 'economic/pdf'
14
+ require 'economic/project'
15
+ require 'economic/recipient'
16
+ require 'economic/references'
17
+
6
18
  require 'economic/customer_repo'
7
19
  require 'economic/customer'
8
20
  require 'economic/product_repo'
@@ -1,5 +1,5 @@
1
1
  module Economic
2
2
  module Rest
3
- VERSION = '0.1.9'.freeze
3
+ VERSION = '0.2.0'.freeze
4
4
  end
5
5
  end
@@ -0,0 +1,5 @@
1
+ module Economic
2
+ class VatZone < Base
3
+ field :vatZoneNumber
4
+ end
5
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: economic-rest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Klogborg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-04 00:00:00.000000000 Z
11
+ date: 2019-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -102,18 +102,27 @@ files:
102
102
  - lib/economic/base_repo.rb
103
103
  - lib/economic/customer.rb
104
104
  - lib/economic/customer_repo.rb
105
+ - lib/economic/delivery.rb
105
106
  - lib/economic/journal.rb
106
107
  - lib/economic/journal_repo.rb
108
+ - lib/economic/layout.rb
109
+ - lib/economic/notes.rb
107
110
  - lib/economic/order.rb
108
111
  - lib/economic/orders/archived_repo.rb
109
112
  - lib/economic/orders/drafts_repo.rb
110
113
  - lib/economic/orders/repo.rb
111
114
  - lib/economic/orders/sent_repo.rb
115
+ - lib/economic/payment_terms.rb
116
+ - lib/economic/pdf.rb
112
117
  - lib/economic/product.rb
113
118
  - lib/economic/product_repo.rb
119
+ - lib/economic/project.rb
120
+ - lib/economic/recipient.rb
121
+ - lib/economic/references.rb
114
122
  - lib/economic/rest.rb
115
123
  - lib/economic/rest/version.rb
116
124
  - lib/economic/session.rb
125
+ - lib/economic/vat_zone.rb
117
126
  - lib/economic/voucher.rb
118
127
  homepage: https://bitbucket.org/traels/economic-rest
119
128
  licenses: