business-central 1.0.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.
Files changed (32) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +120 -0
  4. data/lib/business_central.rb +22 -0
  5. data/lib/business_central/client.rb +94 -0
  6. data/lib/business_central/exceptions.rb +55 -0
  7. data/lib/business_central/object/account.rb +11 -0
  8. data/lib/business_central/object/base.rb +105 -0
  9. data/lib/business_central/object/company.rb +11 -0
  10. data/lib/business_central/object/helper.rb +15 -0
  11. data/lib/business_central/object/item.rb +50 -0
  12. data/lib/business_central/object/purchase_invoice.rb +77 -0
  13. data/lib/business_central/object/purchase_invoice_line.rb +41 -0
  14. data/lib/business_central/object/request.rb +80 -0
  15. data/lib/business_central/object/response.rb +60 -0
  16. data/lib/business_central/object/validation.rb +54 -0
  17. data/lib/business_central/object/vendor.rb +45 -0
  18. data/lib/business_central/version.rb +3 -0
  19. data/lib/core_ext/string.rb +25 -0
  20. data/test/business_central/client_test.rb +77 -0
  21. data/test/business_central/object/account_test.rb +48 -0
  22. data/test/business_central/object/company_test.rb +47 -0
  23. data/test/business_central/object/item_test.rb +128 -0
  24. data/test/business_central/object/purchase_invoice_line_test.rb +129 -0
  25. data/test/business_central/object/purchase_invoice_test.rb +125 -0
  26. data/test/business_central/object/request_test.rb +82 -0
  27. data/test/business_central/object/response_test.rb +26 -0
  28. data/test/business_central/object/validation_test.rb +61 -0
  29. data/test/business_central/object/vendor_test.rb +125 -0
  30. data/test/business_central_test.rb +7 -0
  31. data/test/test_helper.rb +11 -0
  32. metadata +190 -0
@@ -0,0 +1,77 @@
1
+ require "test_helper"
2
+ # rake test TEST=test/business_central/client_test.rb
3
+
4
+ class BusinessCentral::ClientTest < Minitest::Test
5
+ def setup
6
+ @client = BusinessCentral::Client.new
7
+ end
8
+
9
+ def test_authorize_client
10
+ test_redirect_url = 'www.example.com'
11
+ response = @client.authorize(oauth_authorize_callback: test_redirect_url)
12
+ assert_match /oauth2\/authorize?/, response
13
+ assert_match /redirect_uri=#{test_redirect_url}/, response
14
+ end
15
+
16
+ def test_request_client_token
17
+ test_redirect_url = 'www.example.com'
18
+ test_access_token = '123'
19
+
20
+ stub_request(:post, /login.windows.net/)
21
+ .to_return(
22
+ status: 200,
23
+ headers: {
24
+ 'Content-Type': 'application/json'
25
+ },
26
+ body: {
27
+ access_token: test_access_token,
28
+ refresh_token: '456',
29
+ expires_in: (Time.now + 3600).to_i,
30
+ token_type: 'Bearer'
31
+ }.to_json
32
+ )
33
+
34
+ response = @client.request_token('code123', oauth_token_callback: test_redirect_url)
35
+ assert_equal test_access_token,response.token
36
+ end
37
+
38
+ def test_authorize_client_from_token
39
+ test_access_token = '123'
40
+ @client.authorize_from_token(
41
+ token: test_access_token,
42
+ refresh_token: '456',
43
+ expires_at: Time.now + 3600,
44
+ expires_in: 3600
45
+ )
46
+ assert_equal test_access_token, @client.access_token.token
47
+ end
48
+
49
+ def test_refresh_token
50
+ test_access_token = '789'
51
+
52
+ @client.authorize_from_token(
53
+ token: '123',
54
+ refresh_token: '456',
55
+ expires_at: Time.now + 3600,
56
+ expires_in: 3600
57
+ )
58
+
59
+ stub_request(:post, /login.windows.net/)
60
+ .to_return(
61
+ status: 200,
62
+ headers: {
63
+ 'Content-Type': 'application/json'
64
+ },
65
+ body: {
66
+ access_token: test_access_token,
67
+ refresh_token: '101112',
68
+ expires_in: (Time.now + 3600).to_i,
69
+ token_type: 'Bearer'
70
+ }.to_json
71
+ )
72
+
73
+ response = @client.refresh_token
74
+ assert_equal test_access_token, response.token
75
+ end
76
+
77
+ end
@@ -0,0 +1,48 @@
1
+ require "test_helper"
2
+ # rake test TEST=test/business_central/object/account_test.rb
3
+
4
+ class BusinessCentral::Object::AccountTest < Minitest::Test
5
+ def setup
6
+ @company_id = '123456'
7
+ @client = BusinessCentral::Client.new
8
+ @client.authorize_from_token(
9
+ token: '123',
10
+ refresh_token: '456',
11
+ expires_at: Time.now + 3600,
12
+ expires_in: 3600
13
+ )
14
+ @account = @client.account(company_id: @company_id)
15
+ end
16
+
17
+ def test_find_all
18
+ stub_request(:get, /accounts/)
19
+ .to_return(
20
+ status: 200,
21
+ body: {
22
+ 'value': [
23
+ {
24
+ displayName: 'account1'
25
+ }
26
+ ]
27
+ }.to_json
28
+ )
29
+
30
+
31
+ response = @account.find_all
32
+ assert_equal response.first[:display_name], 'account1'
33
+ end
34
+
35
+ def test_find_by_id
36
+ test_account_id = '123'
37
+ stub_request(:get, /accounts\(#{test_account_id}\)/)
38
+ .to_return(
39
+ status: 200,
40
+ body: {
41
+ displayName: 'account2'
42
+ }.to_json
43
+ )
44
+
45
+ response = @account.find_by_id(test_account_id)
46
+ assert_equal response[:display_name], 'account2'
47
+ end
48
+ end
@@ -0,0 +1,47 @@
1
+ require "test_helper"
2
+ # rake test TEST=test/business_central/object/company_test.rb
3
+
4
+ class BusinessCentral::Object::CompanyTest < Minitest::Test
5
+ def setup
6
+ @client = BusinessCentral::Client.new
7
+ @client.authorize_from_token(
8
+ token: '123',
9
+ refresh_token: '456',
10
+ expires_at: Time.now + 3600,
11
+ expires_in: 3600
12
+ )
13
+ @company = @client.company
14
+ end
15
+
16
+ def test_find_all
17
+ stub_request(:get, /companies/)
18
+ .to_return(
19
+ status: 200,
20
+ body: {
21
+ 'value': [
22
+ {
23
+ displayName: 'business1'
24
+ }
25
+ ]
26
+ }.to_json
27
+ )
28
+
29
+
30
+ response = @company.find_all
31
+ assert_equal response.first[:display_name], 'business1'
32
+ end
33
+
34
+ def test_find_by_id
35
+ test_company_id = '123'
36
+ stub_request(:get, /companies\(#{test_company_id}\)/)
37
+ .to_return(
38
+ status: 200,
39
+ body: {
40
+ displayName: 'business2'
41
+ }.to_json
42
+ )
43
+
44
+ response = @company.find_by_id(test_company_id)
45
+ assert_equal response[:display_name], 'business2'
46
+ end
47
+ end
@@ -0,0 +1,128 @@
1
+ require "test_helper"
2
+ # rake test TEST=test/business_central/object/item_test.rb
3
+
4
+ class BusinessCentral::Object::ItemTest < Minitest::Test
5
+ def setup
6
+ @company_id = '123456'
7
+ @client = BusinessCentral::Client.new
8
+ @client.authorize_from_token(
9
+ token: '123',
10
+ refresh_token: '456',
11
+ expires_at: Time.now + 3600,
12
+ expires_in: 3600
13
+ )
14
+ @item = @client.item(company_id: @company_id)
15
+ end
16
+
17
+ def test_find_all
18
+ stub_request(:get, /items/)
19
+ .to_return(
20
+ status: 200,
21
+ body: {
22
+ 'value': [
23
+ {
24
+ displayName: 'item1'
25
+ }
26
+ ]
27
+ }.to_json,
28
+ )
29
+
30
+ response = @item.find_all
31
+ assert_equal response.first[:display_name], 'item1'
32
+ end
33
+
34
+ def test_find_by_id
35
+ test_item_id = '09876'
36
+ stub_request(:get, /items\(#{test_item_id}\)/)
37
+ .to_return(
38
+ status: 200,
39
+ body: {
40
+ displayName: 'item2'
41
+ }.to_json
42
+ )
43
+
44
+ response = @item.find_by_id(test_item_id)
45
+ assert_equal response[:display_name], 'item2'
46
+ end
47
+
48
+ def test_where
49
+ test_filter = "displayName eq 'item3'"
50
+ stub_request(:get, /items\?\$filter=#{test_filter}/)
51
+ .to_return(
52
+ status: 200,
53
+ body: {
54
+ 'value': [
55
+ {
56
+ displayName: 'item3'
57
+ }
58
+ ]
59
+ }.to_json
60
+ )
61
+
62
+ response = @item.where(test_filter)
63
+ assert_equal response.first[:display_name], 'item3'
64
+ end
65
+
66
+ def test_create
67
+ stub_request(:post, /items/)
68
+ .to_return(
69
+ status: 200,
70
+ body: {
71
+ displayName: 'item4'
72
+ }.to_json
73
+ )
74
+
75
+ response = @item.create({
76
+ display_name: 'item4',
77
+ type: 'Inventory'
78
+ })
79
+ assert_equal response[:display_name], 'item4'
80
+ end
81
+
82
+
83
+ def test_update
84
+ test_item_id = '011123'
85
+ stub_request(:get, /items\(#{test_item_id}\)/)
86
+ .to_return(
87
+ status: 200,
88
+ body: {
89
+ etag: '112',
90
+ displayName: 'item5'
91
+ }.to_json
92
+ )
93
+
94
+ stub_request(:patch, /items\(#{test_item_id}\)/)
95
+ .to_return(
96
+ status: 200,
97
+ body: {
98
+ displayName: 'item6'
99
+ }.to_json
100
+ )
101
+
102
+ response = @item.update(
103
+ test_item_id,
104
+ {
105
+ display_name: 'item6',
106
+ type: 'Inventory'
107
+ }
108
+ )
109
+ assert_equal response[:display_name], 'item6'
110
+ end
111
+
112
+ def test_delete
113
+ test_item_id = '0111245'
114
+ stub_request(:get, /items\(#{test_item_id}\)/)
115
+ .to_return(
116
+ status: 200,
117
+ body: {
118
+ etag: '113',
119
+ displayName: 'item7'
120
+ }.to_json
121
+ )
122
+
123
+ stub_request(:delete, /items\(#{test_item_id}\)/)
124
+ .to_return(status: 204)
125
+
126
+ assert @item.destroy(test_item_id)
127
+ end
128
+ end
@@ -0,0 +1,129 @@
1
+ require "test_helper"
2
+ # rake test TEST=test/business_central/object/purchase_invoice_line_test.rb
3
+
4
+ class BusinessCentral::Object::PurchaseInvoiceLineTest < Minitest::Test
5
+ def setup
6
+ @company_id = '123456'
7
+ @purchase_invoice_id = '789456'
8
+ @client = BusinessCentral::Client.new
9
+ @client.authorize_from_token(
10
+ token: '123',
11
+ refresh_token: '456',
12
+ expires_at: Time.now + 3600,
13
+ expires_in: 3600
14
+ )
15
+ @purchase_invoice_line = @client.purchase_invoice_line(
16
+ company_id: @company_id,
17
+ purchase_invoice_id: @purchase_invoice_id
18
+ )
19
+ end
20
+
21
+ def test_find_all
22
+ stub_request(:get, /purchaseInvoiceLines/)
23
+ .to_return(
24
+ status: 200,
25
+ body: {
26
+ 'value': [
27
+ {
28
+ description: 'po line 1'
29
+ }
30
+ ]
31
+ }.to_json,
32
+ )
33
+
34
+ response = @purchase_invoice_line.find_all
35
+ assert_equal response.first[:description], 'po line 1'
36
+ end
37
+
38
+ def test_find_by_id
39
+ test_purchase_invoice_line_id = '09876'
40
+ stub_request(:get, /purchaseInvoiceLines\(#{test_purchase_invoice_line_id}\)/)
41
+ .to_return(
42
+ status: 200,
43
+ body: {
44
+ description: 'po line 2'
45
+ }.to_json
46
+ )
47
+
48
+ response = @purchase_invoice_line.find_by_id(test_purchase_invoice_line_id)
49
+ assert_equal response[:description], 'po line 2'
50
+ end
51
+
52
+ def test_where
53
+ test_filter = "description eq 'po line 3'"
54
+ stub_request(:get, /purchaseInvoiceLines\?\$filter=#{test_filter}/)
55
+ .to_return(
56
+ status: 200,
57
+ body: {
58
+ 'value': [
59
+ {
60
+ description: 'po line 3'
61
+ }
62
+ ]
63
+ }.to_json
64
+ )
65
+
66
+ response = @purchase_invoice_line.where(test_filter)
67
+ assert_equal response.first[:description], 'po line 3'
68
+ end
69
+
70
+ def test_create
71
+ stub_request(:post, /purchaseInvoiceLines/)
72
+ .to_return(
73
+ status: 200,
74
+ body: {
75
+ description: 'po line 4'
76
+ }.to_json
77
+ )
78
+
79
+ response = @purchase_invoice_line.create({
80
+ description: 'po line 4'
81
+ })
82
+ assert_equal response[:description], 'po line 4'
83
+ end
84
+
85
+ def test_update
86
+ test_purchase_invoice_line_id = '011123'
87
+ stub_request(:get, /purchaseInvoiceLines\(#{test_purchase_invoice_line_id}\)/)
88
+ .to_return(
89
+ status: 200,
90
+ body: {
91
+ etag: '112',
92
+ description: 'po line 5'
93
+ }.to_json
94
+ )
95
+
96
+ stub_request(:patch, /purchaseInvoiceLines\(#{test_purchase_invoice_line_id}\)/)
97
+ .to_return(
98
+ status: 200,
99
+ body: {
100
+ description: 'po line 6'
101
+ }.to_json
102
+ )
103
+
104
+ response = @purchase_invoice_line.update(
105
+ test_purchase_invoice_line_id,
106
+ {
107
+ description: 'po line 6'
108
+ }
109
+ )
110
+ assert_equal response[:description], 'po line 6'
111
+ end
112
+
113
+ def test_delete
114
+ test_purchase_invoice_line_id = '0111245'
115
+ stub_request(:get, /purchaseInvoiceLines\(#{test_purchase_invoice_line_id}\)/)
116
+ .to_return(
117
+ status: 200,
118
+ body: {
119
+ etag: '113',
120
+ description: 'po line 6'
121
+ }.to_json
122
+ )
123
+
124
+ stub_request(:delete, /purchaseInvoiceLines\(#{test_purchase_invoice_line_id}\)/)
125
+ .to_return(status: 204)
126
+
127
+ assert @purchase_invoice_line.destroy(test_purchase_invoice_line_id)
128
+ end
129
+ end
@@ -0,0 +1,125 @@
1
+ require "test_helper"
2
+ # rake test TEST=test/business_central/object/purchase_invoice_test.rb
3
+
4
+ class BusinessCentral::Object::PurchaseInvoiceTest < Minitest::Test
5
+ def setup
6
+ @company_id = '123456'
7
+ @client = BusinessCentral::Client.new
8
+ @client.authorize_from_token(
9
+ token: '123',
10
+ refresh_token: '456',
11
+ expires_at: Time.now + 3600,
12
+ expires_in: 3600
13
+ )
14
+ @purchase_invoice = @client.purchase_invoice(company_id: @company_id)
15
+ end
16
+
17
+ def test_find_all
18
+ stub_request(:get, /purchaseInvoices/)
19
+ .to_return(
20
+ status: 200,
21
+ body: {
22
+ 'value': [
23
+ {
24
+ displayName: 'po1'
25
+ }
26
+ ]
27
+ }.to_json,
28
+ )
29
+
30
+ response = @purchase_invoice.find_all
31
+ assert_equal response.first[:display_name], 'po1'
32
+ end
33
+
34
+ def test_find_by_id
35
+ test_purchase_invoice_id = '09876'
36
+ stub_request(:get, /purchaseInvoices\(#{test_purchase_invoice_id}\)/)
37
+ .to_return(
38
+ status: 200,
39
+ body: {
40
+ displayName: 'po2'
41
+ }.to_json
42
+ )
43
+
44
+ response = @purchase_invoice.find_by_id(test_purchase_invoice_id)
45
+ assert_equal response[:display_name], 'po2'
46
+ end
47
+
48
+ def test_where
49
+ test_filter = "displayName eq 'po3'"
50
+ stub_request(:get, /purchaseInvoices\?\$filter=#{test_filter}/)
51
+ .to_return(
52
+ status: 200,
53
+ body: {
54
+ 'value': [
55
+ {
56
+ displayName: 'po3'
57
+ }
58
+ ]
59
+ }.to_json
60
+ )
61
+
62
+ response = @purchase_invoice.where(test_filter)
63
+ assert_equal response.first[:display_name], 'po3'
64
+ end
65
+
66
+ def test_create
67
+ stub_request(:post, /purchaseInvoices/)
68
+ .to_return(
69
+ status: 200,
70
+ body: {
71
+ displayName: 'po4'
72
+ }.to_json
73
+ )
74
+
75
+ response = @purchase_invoice.create({
76
+ display_name: 'po4'
77
+ })
78
+ assert_equal response[:display_name], 'po4'
79
+ end
80
+
81
+ def test_update
82
+ test_purchase_invoice_id = '011123'
83
+ stub_request(:get, /purchaseInvoices\(#{test_purchase_invoice_id}\)/)
84
+ .to_return(
85
+ status: 200,
86
+ body: {
87
+ etag: '112',
88
+ displayName: 'po5'
89
+ }.to_json
90
+ )
91
+
92
+ stub_request(:patch, /purchaseInvoices\(#{test_purchase_invoice_id}\)/)
93
+ .to_return(
94
+ status: 200,
95
+ body: {
96
+ displayName: 'po6'
97
+ }.to_json
98
+ )
99
+
100
+ response = @purchase_invoice.update(
101
+ test_purchase_invoice_id,
102
+ {
103
+ display_name: 'po6'
104
+ }
105
+ )
106
+ assert_equal response[:display_name], 'po6'
107
+ end
108
+
109
+ def test_delete
110
+ test_purchase_invoice_id = '0111245'
111
+ stub_request(:get, /purchaseInvoices\(#{test_purchase_invoice_id}\)/)
112
+ .to_return(
113
+ status: 200,
114
+ body: {
115
+ etag: '113',
116
+ displayName: 'po7'
117
+ }.to_json
118
+ )
119
+
120
+ stub_request(:delete, /purchaseInvoices\(#{test_purchase_invoice_id}\)/)
121
+ .to_return(status: 204)
122
+
123
+ assert @purchase_invoice.destroy(test_purchase_invoice_id)
124
+ end
125
+ end