economic-rest 0.0.1 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cd65880ff0fbfa02445672cd4352802a5e157193
4
- data.tar.gz: afaf4212c16e263681cc93f59bca3b0ba069b7ff
3
+ metadata.gz: a583a4c00f68b2bd538c7245d7a36505fe49d3a8
4
+ data.tar.gz: fc97557df7f768a0e1ea2cd68d6e6d7a53ba274e
5
5
  SHA512:
6
- metadata.gz: 39de349d00f5eb3a31e794f6679d118aa460f00a6d0e5367cfe1295efab6ced17762fc90639b4cc5e74ef77e159b10ee1c7270be523b16165db84481b93ceae0
7
- data.tar.gz: 727091c95d96385175069e9c3639e5f6f0bc517b30287d3ac39c66481d2f5b7e261a6d042469575f7ee8fab9ec7f637802c6e3c7093867662c7f97f56fe7c718
6
+ metadata.gz: ee6f2b2949418ed8cbe3b176e88a60780133e8efa571f24418d4740d3c4d29b95e4bbcf0566b3014f439dea67a16584053dedf8cbde613ce77642870fe867747
7
+ data.tar.gz: 593d9e1a4cf8a4d393cf51a12f5bf9598fc28f315578476b91f6f1c88d835e4afc2ff849e5a1dd1e870650b75bee09b4b3c358a8aec10fdd0d9f8baf916429e7
data/Gemfile CHANGED
@@ -4,3 +4,5 @@ git_source(:bitbucket) { |repo_name| 'https://bitbucket.org/traels/economic-rest
4
4
 
5
5
  # Specify your gem's dependencies in economic-rest.gemspec
6
6
  gemspec
7
+
8
+ gem "webmock", "~> 3.5"
data/Gemfile.lock CHANGED
@@ -7,8 +7,13 @@ PATH
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
+ addressable (2.5.2)
11
+ public_suffix (>= 2.0.2, < 4.0)
12
+ crack (0.4.3)
13
+ safe_yaml (~> 1.0.0)
10
14
  domain_name (0.5.20180417)
11
15
  unf (>= 0.0.5, < 1.0.0)
16
+ hashdiff (0.3.8)
12
17
  http-cookie (1.0.3)
13
18
  domain_name (~> 0.5)
14
19
  m (1.5.1)
@@ -20,14 +25,20 @@ GEM
20
25
  mime-types-data (3.2018.0812)
21
26
  minitest (5.11.3)
22
27
  netrc (0.11.0)
28
+ public_suffix (3.0.3)
23
29
  rake (10.5.0)
24
30
  rest-client (2.0.2)
25
31
  http-cookie (>= 1.0.2, < 2.0)
26
32
  mime-types (>= 1.16, < 4.0)
27
33
  netrc (~> 0.8)
34
+ safe_yaml (1.0.4)
28
35
  unf (0.1.4)
29
36
  unf_ext
30
37
  unf_ext (0.0.7.5)
38
+ webmock (3.5.1)
39
+ addressable (>= 2.3.6)
40
+ crack (>= 0.3.2)
41
+ hashdiff
31
42
 
32
43
  PLATFORMS
33
44
  ruby
@@ -38,6 +49,7 @@ DEPENDENCIES
38
49
  m
39
50
  minitest (~> 5.0)
40
51
  rake (~> 10.0)
52
+ webmock (~> 3.5)
41
53
 
42
54
  BUNDLED WITH
43
55
  1.17.1
data/README.md CHANGED
@@ -25,7 +25,9 @@ Or install it yourself as:
25
25
 
26
26
  ## Usage
27
27
 
28
- TODO: Write usage instructions here
28
+ require 'economic/rest'
29
+
30
+ Economic::Demo.hello
29
31
 
30
32
  ## Development
31
33
 
@@ -0,0 +1,87 @@
1
+ module Economic
2
+ class Base
3
+ def initialize(hash)
4
+ values_based_on_hash(hash)
5
+ end
6
+
7
+ def self.attributes
8
+ @attributes
9
+ end
10
+
11
+ def self.add_attribute(name)
12
+ (@attributes ||= []).push(name)
13
+ end
14
+
15
+ def self.field(name, id: false)
16
+ economic_cased_attibute_name = name.to_s
17
+ attr_accessor economic_cased_attibute_name
18
+ alias_method snake_case(economic_cased_attibute_name), economic_cased_attibute_name
19
+ alias_method "#{snake_case(economic_cased_attibute_name)}=", "#{economic_cased_attibute_name}="
20
+ alias_method 'id_key', economic_cased_attibute_name if id
21
+ add_attribute economic_cased_attibute_name
22
+ end
23
+
24
+ def values_based_on_hash(hash)
25
+ Hash.class_eval { include ExtraMethods }
26
+ @internal_hash = hash
27
+ @internal_hash.each do |k, v|
28
+ k = k.to_s
29
+ if self.class.attributes.include? k
30
+ if v.class == Hash
31
+ v.keys.count.times do |i|
32
+ v.alias!(Base.snake_case(v.keys[i]), v.keys[i])
33
+ end
34
+ end
35
+ send("#{k}=", v)
36
+ else
37
+ warn "unassigned k #{k} in #{self.class.name}" unless %w[layout self soap metaData].include? k
38
+ end
39
+ end
40
+ end
41
+
42
+ def to_h
43
+ self.class.attributes.each do |attribute|
44
+ @internal_hash[attribute.to_sym] = send(attribute) unless send(attribute).nil?
45
+ end
46
+ @internal_hash
47
+ end
48
+
49
+ def dirty?
50
+ self.class.attributes.each do |attribute|
51
+ return true unless send(attribute) == @internal_hash[attribute]
52
+ end
53
+ false
54
+ end
55
+
56
+ def save
57
+ if dirty?
58
+ response = repo.save(self)
59
+ values_based_on_hash(JSON.parse(response.body))
60
+ end
61
+ end
62
+
63
+ def self.snake_case(camel_cased)
64
+ camel_cased.to_s.gsub(/::/, '/')
65
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
66
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
67
+ .tr('-', '_')
68
+ .downcase
69
+ end
70
+
71
+ def self.low_camel_case(snake_cased)
72
+ camel = snake_cased.split('_').collect(&:capitalize).join
73
+ camel[0, 1].downcase + camel[1..-1]
74
+ end
75
+
76
+ def repo
77
+ Object.const_get("#{self.class}Repo")
78
+ end
79
+ end
80
+ end
81
+
82
+ module ExtraMethods
83
+ def alias!(newkey, oldkey)
84
+ self[newkey] = self[oldkey] if self.has_key?(oldkey)
85
+ self
86
+ end
87
+ end
@@ -0,0 +1,82 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+ require 'economic/session'
4
+
5
+ module Economic
6
+ class BaseRepo
7
+ URL = 'https://restapi.e-conomic.com/'.freeze
8
+ class << self
9
+ def headers
10
+ { 'X-AppSecretToken': Session.app_secret_token, 'X-AgreementGrantToken': Session.agreement_grant_token, 'Content-Type': 'application/json' }
11
+ end
12
+ def fetch(endpoint:, page_or_id: nil, pageindex: 0)
13
+ url = ''
14
+ url << URL
15
+ url << endpoint.to_s if endpoint
16
+ url << if page_or_id.nil? || page_or_id.to_s.empty?
17
+ "?skippages=#{pageindex}&pagesize=1000"
18
+ else
19
+ "/#{page_or_id}"
20
+ end
21
+ response = RestClient.get(url, headers)
22
+ response
23
+ end
24
+
25
+ def save(model)
26
+ url = ''
27
+ url << URL
28
+ url << endpoint_name.to_s if endpoint_name
29
+ url << "/#{model.id_key}"
30
+ if model.id_key
31
+ response = RestClient.put(url, model.to_h.to_json, headers)
32
+ else
33
+ response = RestClient.post(url, model.to_h.to_json, headers)
34
+ end
35
+ response
36
+ end
37
+
38
+ def all
39
+ pagination = {}
40
+ pageindex = 0
41
+ entries = []
42
+
43
+ # Loop until last page, last page does not have a 'nextPage'
44
+ while pagination['nextPage'] || pageindex.zero?
45
+ response = fetch(endpoint: endpoint_name, pageindex: pageindex)
46
+
47
+ hash = JSON.parse(response.body)
48
+ hash['collection'].each do |entry_hash|
49
+ entries.push model.new(entry_hash)
50
+ end
51
+
52
+ pagination = hash['pagination']
53
+ pageindex += 1
54
+ end
55
+ entries
56
+ end
57
+
58
+ def find(entry_number)
59
+ response = fetch(endpoint: endpoint_name, page_or_id: entry_number)
60
+ entry_hash = JSON.parse(response.body)
61
+ model.new(entry_hash)
62
+ end
63
+
64
+ def model
65
+ scopes = name.split('::')
66
+ scopes[1] = scopes[1][0...-1] if scopes.count == 3
67
+ Object.const_get("#{scopes[0]}::#{scopes[1].sub('Repo', '')}")
68
+ end
69
+
70
+ def endpoint_name
71
+ end_p = name.sub('Economic::', '')
72
+ if end_p.include?('::')
73
+ end_p = end_p.gsub('Repo', '')
74
+ end_p = end_p.gsub('::', '/')
75
+ else
76
+ end_p = end_p.gsub('Repo', 's')
77
+ end
78
+ end_p.downcase
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,39 @@
1
+ module Economic
2
+ class Customer < Base
3
+ field :address
4
+ field :balance
5
+ field :barred
6
+ field :city
7
+ field :contacts
8
+ field :corporateIdentificationNumber
9
+ field :country
10
+ field :creditLimit
11
+ field :currency
12
+ field :customerNumber, id: true
13
+ field :deliveryLocations
14
+ field :dueAmount
15
+ field :ean
16
+ field :email
17
+ field :lastUpdated
18
+ field :name
19
+ field :mobilePhone
20
+ field :pNumber
21
+ field :publicEntryNumber
22
+ field :telephoneAndFaxNumber
23
+ field :vatNumber
24
+ field :website
25
+ field :zip
26
+
27
+ field :attention
28
+ field :customerContact
29
+ field :customerGroup
30
+ field :defaultDeliveryLocation
31
+ field :invoices
32
+ field :layout
33
+ field :paymentTerms
34
+ field :salesPerson
35
+ field :templates
36
+ field :totals
37
+ field :vatZone
38
+ end
39
+ end
@@ -0,0 +1,4 @@
1
+ module Economic
2
+ class CustomerRepo < Economic::BaseRepo
3
+ end
4
+ end
@@ -0,0 +1,30 @@
1
+ module Economic
2
+ class Order < Base
3
+ field :attachment
4
+ field :costPriceInBaseCurrency
5
+ field :currency
6
+ field :date
7
+ field :dueDate
8
+ field :exchangeRate
9
+ field :grossAmount
10
+ field :grossAmountInBaseCurrency
11
+ field :lines
12
+ field :marginInBaseCurrency
13
+ field :marginPercentage
14
+ field :netAmount
15
+ field :netAmountInBaseCurrency
16
+ field :orderNumber, id: true
17
+ field :roundingAmount
18
+ field :vatAmount
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
29
+ end
30
+ end
@@ -0,0 +1,6 @@
1
+ module Economic
2
+ module Orders
3
+ class ArchivedRepo < Economic::Orders::Repo
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,14 @@
1
+ module Economic
2
+ module Orders
3
+ class DraftsRepo < Economic::Orders::Repo
4
+ def self.send(model)
5
+ url = ''
6
+ url << URL
7
+ url << '/orders'
8
+ url << '/sent'
9
+ response = RestClient.post(url, model.to_h.to_json, headers)
10
+ response
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ module Economic
2
+ module Orders
3
+ class Repo < Economic::BaseRepo
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,14 @@
1
+ module Economic
2
+ module Orders
3
+ class SentRepo < Economic::Orders::Repo
4
+ def self.send(model)
5
+ url = ''
6
+ url << URL
7
+ url << '/orders'
8
+ url << '/drafts'
9
+ response = RestClient.post(url, model.to_h.to_json, headers)
10
+ response
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ module Economic
2
+ class Product < Base
3
+ field :barCode
4
+ field :barred
5
+ field :costPrice
6
+ field :description
7
+ field :lastUpdated
8
+ field :name
9
+ field :productNumber, id: true
10
+ field :recommendedPrice
11
+ field :salesPrice
12
+
13
+ field :departmentalDistribution
14
+ field :inventory
15
+ field :invoices
16
+ field :pricing
17
+ field :productGroup
18
+ field :unit
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ module Economic
2
+ class ProductRepo < Economic::BaseRepo
3
+ end
4
+ end
data/lib/economic/rest.rb CHANGED
@@ -1,15 +1,26 @@
1
1
  require 'economic/rest/version'
2
2
  require 'rest-client'
3
3
 
4
+ require 'economic/base_repo'
5
+ require 'economic/base'
6
+ require 'economic/customer_repo'
7
+ require 'economic/customer'
8
+ require 'economic/product_repo'
9
+ require 'economic/product'
10
+
11
+ require 'economic/orders/repo'
12
+ require 'economic/order'
13
+ require 'economic/orders/archived_repo'
14
+ require 'economic/orders/drafts_repo'
15
+ require 'economic/orders/sent_repo'
16
+
4
17
  module Economic
5
- module Rest
6
- class Demo
7
- def self.hello
8
- RestClient.get('https://restapi.e-conomic.com/',
9
- 'X-AppSecretToken': 'Demo',
10
- 'X-AgreementGrantToken': 'Demo',
11
- 'Content-Type': 'application/json')
12
- end
18
+ class Demo
19
+ def self.hello
20
+ RestClient.get('https://restapi.e-conomic.com/',
21
+ 'X-AppSecretToken': 'Demo',
22
+ 'X-AgreementGrantToken': 'Demo',
23
+ 'Content-Type': 'application/json')
13
24
  end
14
25
  end
15
26
  end
@@ -1,5 +1,5 @@
1
1
  module Economic
2
2
  module Rest
3
- VERSION = '0.0.1'.freeze
3
+ VERSION = '0.1.0'.freeze
4
4
  end
5
5
  end
@@ -0,0 +1,22 @@
1
+ module Economic
2
+ # The Economic::Session contains details and behaviors for a current
3
+ # connection to the API endpoint.
4
+ class Session
5
+ def self.authentication(private_app_id, access_id)
6
+ @private_app_id = private_app_id
7
+ @access_id = access_id
8
+ end
9
+
10
+ def self.app_secret_token
11
+ raise ArgumentError, 'Authentication tokens not set, Call Session.authentication' if @private_app_id.nil?
12
+
13
+ @private_app_id
14
+ end
15
+
16
+ def self.agreement_grant_token
17
+ raise ArgumentError, 'Authentication tokens not set, Call Session.authentication' if @access_id.nil?
18
+
19
+ @access_id
20
+ end
21
+ end
22
+ 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.0.1
4
+ version: 0.1.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-01-15 00:00:00.000000000 Z
11
+ date: 2019-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -97,8 +97,20 @@ files:
97
97
  - bin/console
98
98
  - bin/setup
99
99
  - economic-rest.gemspec
100
+ - lib/economic/base.rb
101
+ - lib/economic/base_repo.rb
102
+ - lib/economic/customer.rb
103
+ - lib/economic/customer_repo.rb
104
+ - lib/economic/order.rb
105
+ - lib/economic/orders/archived_repo.rb
106
+ - lib/economic/orders/drafts_repo.rb
107
+ - lib/economic/orders/repo.rb
108
+ - lib/economic/orders/sent_repo.rb
109
+ - lib/economic/product.rb
110
+ - lib/economic/product_repo.rb
100
111
  - lib/economic/rest.rb
101
112
  - lib/economic/rest/version.rb
113
+ - lib/economic/session.rb
102
114
  homepage: https://bitbucket.org/traels/economic-rest
103
115
  licenses:
104
116
  - MIT