tripletexer 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.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +26 -0
  3. data/README.md +35 -0
  4. data/lib/tripletexer.rb +136 -0
  5. data/lib/tripletexer/connection.rb +49 -0
  6. data/lib/tripletexer/endpoints.rb +3 -0
  7. data/lib/tripletexer/endpoints/abstract_endpoint.rb +106 -0
  8. data/lib/tripletexer/endpoints/activity.rb +22 -0
  9. data/lib/tripletexer/endpoints/address.rb +22 -0
  10. data/lib/tripletexer/endpoints/company.rb +17 -0
  11. data/lib/tripletexer/endpoints/contact.rb +22 -0
  12. data/lib/tripletexer/endpoints/country.rb +17 -0
  13. data/lib/tripletexer/endpoints/currency.rb +17 -0
  14. data/lib/tripletexer/endpoints/customer.rb +31 -0
  15. data/lib/tripletexer/endpoints/customer/category.rb +27 -0
  16. data/lib/tripletexer/endpoints/department.rb +17 -0
  17. data/lib/tripletexer/endpoints/employee.rb +20 -0
  18. data/lib/tripletexer/endpoints/employee/entitlement.rb +15 -0
  19. data/lib/tripletexer/endpoints/inventory.rb +15 -0
  20. data/lib/tripletexer/endpoints/invoice.rb +44 -0
  21. data/lib/tripletexer/endpoints/invoice/payment_type.rb +16 -0
  22. data/lib/tripletexer/endpoints/ledger.rb +49 -0
  23. data/lib/tripletexer/endpoints/ledger/account.rb +17 -0
  24. data/lib/tripletexer/endpoints/ledger/accounting_period.rb +17 -0
  25. data/lib/tripletexer/endpoints/ledger/annual_account.rb +17 -0
  26. data/lib/tripletexer/endpoints/ledger/close_group.rb +19 -0
  27. data/lib/tripletexer/endpoints/ledger/posting.rb +21 -0
  28. data/lib/tripletexer/endpoints/ledger/vat_type.rb +17 -0
  29. data/lib/tripletexer/endpoints/ledger/voucher.rb +40 -0
  30. data/lib/tripletexer/endpoints/ledger/voucher_type.rb +17 -0
  31. data/lib/tripletexer/endpoints/order.rb +43 -0
  32. data/lib/tripletexer/endpoints/order/orderline.rb +21 -0
  33. data/lib/tripletexer/endpoints/product.rb +17 -0
  34. data/lib/tripletexer/endpoints/project.rb +26 -0
  35. data/lib/tripletexer/endpoints/project/category.rb +27 -0
  36. data/lib/tripletexer/endpoints/supplier.rb +27 -0
  37. data/lib/tripletexer/endpoints/timesheet.rb +13 -0
  38. data/lib/tripletexer/endpoints/timesheet/entry.rb +64 -0
  39. data/lib/tripletexer/endpoints/timesheet/time_clock.rb +39 -0
  40. data/lib/tripletexer/endpoints/token.rb +13 -0
  41. data/lib/tripletexer/endpoints/token/consumer.rb +13 -0
  42. data/lib/tripletexer/endpoints/token/session.rb +32 -0
  43. data/lib/tripletexer/errors.rb +11 -0
  44. data/lib/tripletexer/format_helpers.rb +12 -0
  45. data/lib/tripletexer/version.rb +6 -0
  46. metadata +228 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 66ce0e7589da08bb03238b69a831ac8d35692865
4
+ data.tar.gz: 52d3c8c42e5695d1775efadf6eae9daf64463fda
5
+ SHA512:
6
+ metadata.gz: 3b39f1a23c84628acff62e3ca07cf26a7dad630437577241a0e28f6843d2b5e0285c51458b7b6a961c5cf38b6e3195b5c8c5566a661a5a3d851586c946e0d1de
7
+ data.tar.gz: 1910c4776bb6a92fd01f54c79ad0bc1f99dd17a9650ea930f545b8e4faac17d547ba16106f6a140523ded934f4133ce09c325302b89232a5712b92da40c562d4
data/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ BSD 2-Clause License
2
+
3
+ Copyright (c) 2017, Aldis Berjoza <aldis@berjoza.lv>
4
+
5
+ All rights reserved.
6
+
7
+ Redistribution and use in source and binary forms, with or without
8
+ modification, are permitted provided that the following conditions are met:
9
+
10
+ * Redistributions of source code must retain the above copyright notice, this
11
+ list of conditions and the following disclaimer.
12
+
13
+ * Redistributions in binary form must reproduce the above copyright notice,
14
+ this list of conditions and the following disclaimer in the documentation
15
+ and/or other materials provided with the distribution.
16
+
17
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Tripletexer - ruby client for tripletex.no API v2
2
+
3
+ ## Getting started
4
+ Add tripletexer to your Gemfile
5
+ ```ruby
6
+ gem 'tripletexer', '~> 0.1.0'
7
+ ```
8
+ run bundle install
9
+
10
+ ## Using tripletexer
11
+
12
+ Tripletex.no API documentation can be found here: https://tripletex.no/v2-docs/
13
+
14
+ This gem tries to fallow patterns of tripletex.no documentation, however not completely.
15
+
16
+ ```ruby
17
+ client = Tripletexer.new
18
+
19
+ # start new session
20
+ client.token.session.create('your_consumer_token', 'your_employee_token')
21
+
22
+ client.invoice.search(Date.new(2017), Time.now.to_date).each do |invoice|
23
+ # do something with invoice
24
+ end
25
+
26
+ # revoke token
27
+ client.token.session.destroy
28
+ ```
29
+
30
+ When tipletex.no API returns a list tripletexer gem returns an instance of
31
+ enumerator, this way you never have to deal with pagination.
32
+
33
+ ## TODO
34
+
35
+ Write some tests
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ %w[
4
+ tripletexer/version.rb
5
+ tripletexer/errors.rb
6
+ tripletexer/connection.rb
7
+ tripletexer/format_helpers.rb
8
+ tripletexer/endpoints.rb
9
+ tripletexer/endpoints/abstract_endpoint.rb
10
+ tripletexer/endpoints/activity.rb
11
+ tripletexer/endpoints/address.rb
12
+ tripletexer/endpoints/company.rb
13
+ tripletexer/endpoints/contact.rb
14
+ tripletexer/endpoints/country.rb
15
+ tripletexer/endpoints/currency.rb
16
+ tripletexer/endpoints/customer.rb
17
+ tripletexer/endpoints/customer/category.rb
18
+ tripletexer/endpoints/department.rb
19
+ tripletexer/endpoints/employee.rb
20
+ tripletexer/endpoints/employee/entitlement.rb
21
+ tripletexer/endpoints/inventory.rb
22
+ tripletexer/endpoints/invoice.rb
23
+ tripletexer/endpoints/invoice/payment_type.rb
24
+ tripletexer/endpoints/ledger.rb
25
+ tripletexer/endpoints/ledger/account.rb
26
+ tripletexer/endpoints/ledger/accounting_period.rb
27
+ tripletexer/endpoints/ledger/annual_account.rb
28
+ tripletexer/endpoints/ledger/close_group.rb
29
+ tripletexer/endpoints/ledger/posting.rb
30
+ tripletexer/endpoints/ledger/vat_type.rb
31
+ tripletexer/endpoints/ledger/voucher.rb
32
+ tripletexer/endpoints/ledger/voucher_type.rb
33
+ tripletexer/endpoints/order.rb
34
+ tripletexer/endpoints/order/orderline.rb
35
+ tripletexer/endpoints/product.rb
36
+ tripletexer/endpoints/project.rb
37
+ tripletexer/endpoints/project/category.rb
38
+ tripletexer/endpoints/supplier.rb
39
+ tripletexer/endpoints/timesheet.rb
40
+ tripletexer/endpoints/timesheet/entry.rb
41
+ tripletexer/endpoints/timesheet/time_clock.rb
42
+ tripletexer/endpoints/token.rb
43
+ tripletexer/endpoints/token/consumer.rb
44
+ tripletexer/endpoints/token/session.rb
45
+ ].each do |file|
46
+ require File.expand_path(file, __dir__)
47
+ end
48
+
49
+ class Tripletexer
50
+ def initialize(object_class: Hash, proxy: nil, token: nil)
51
+ @object_class = object_class
52
+ @proxy = proxy
53
+ connection.token = token if token
54
+ end
55
+
56
+ def activity
57
+ Tripletexer::Endpoints::Activity.new(connection)
58
+ end
59
+
60
+ def address
61
+ Tripletexer::Endpoints::Address.new(connection)
62
+ end
63
+
64
+ def company
65
+ Tripletexer::Endpoints::Company.new(connection)
66
+ end
67
+
68
+ def contact
69
+ Tripletexer::Endpoints::Contact.new(connection)
70
+ end
71
+
72
+ def country
73
+ Tripletexer::Endpoints::Country.new(connection)
74
+ end
75
+
76
+ def currency
77
+ Tripletexer::Endpoints::Currency.new(connection)
78
+ end
79
+
80
+ def customer
81
+ Tripletexer::Endpoints::Customer.new(connection)
82
+ end
83
+
84
+ def department
85
+ Tripletexer::Endpoints::Department.new(connection)
86
+ end
87
+
88
+ def employee
89
+ Tripletexer::Endpoints::Employee.new(connection)
90
+ end
91
+
92
+ def inventory
93
+ Tripletexer::Endpoints::Inventory.new(connection)
94
+ end
95
+
96
+ def invoice
97
+ Tripletexer::Endpoints::Invoice.new(connection)
98
+ end
99
+
100
+ def ledger
101
+ Tripletexer::Endpoints::Ledger.new(connection)
102
+ end
103
+
104
+ def order
105
+ Tripletexer::Endpoints::Order.new(connection)
106
+ end
107
+
108
+ def product
109
+ Tripletexer::Endpoints::Product.new(connection)
110
+ end
111
+
112
+ def project
113
+ Tripletexer::Endpoints::Project.new(connection)
114
+ end
115
+
116
+ def supplier
117
+ Tripletexer::Endpoints::Supplier.new(connection)
118
+ end
119
+
120
+ def timesheet
121
+ Tripletexer::Endpoints::Timesheet.new(connection)
122
+ end
123
+
124
+ def token
125
+ Tripletexer::Endpoints::Token.new(connection)
126
+ end
127
+
128
+ private
129
+
130
+ attr_accessor :object_class, :proxy
131
+
132
+ def connection
133
+ @connection ||= Tripletexer::Connection.new(object_class: object_class, proxy: proxy)
134
+ end
135
+
136
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'faraday'
5
+ require 'faraday_middleware'
6
+
7
+ class Tripletexer::Connection
8
+ ENDPOINT = 'https://tripletex.no/'
9
+
10
+ attr_reader :token
11
+
12
+ def initialize(object_class: Hash, proxy: nil)
13
+ @object_class = object_class
14
+ @proxy = proxy
15
+ end
16
+
17
+ def connection
18
+ return @connection if token && @connection
19
+ @connection = init_connection
20
+ end
21
+
22
+ def reset
23
+ @token = nil
24
+ @connection = nil
25
+ nil
26
+ end
27
+
28
+ def token=(new_token)
29
+ reset
30
+ @token = new_token
31
+ end
32
+
33
+ private
34
+
35
+ attr_reader :object_class, :proxy
36
+
37
+ def init_connection
38
+ Faraday.new(url: ENDPOINT) do |faraday|
39
+ faraday.response :logger
40
+ faraday.response :json, parser_options: { object_class: object_class }, content_type: %r[/json$]
41
+ faraday.headers = {
42
+ 'Content-Type': 'application/json'
43
+ }
44
+ faraday.adapter :patron
45
+ faraday.basic_auth(0, token) if token
46
+ faraday.proxy = proxy if proxy # https://github.com/lostisland/faraday/issues/733
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tripletexer::Endpoints; end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'uri'
5
+
6
+ module Tripletexer::Endpoints
7
+ class AbstractEndpoint
8
+ include ::Tripletexer::FormatHelpers
9
+
10
+ def initialize(connection)
11
+ @connection = connection
12
+ end
13
+
14
+ private
15
+
16
+ attr_reader :connection
17
+
18
+ def find_entities(path, params, &block)
19
+ Enumerator.new do |enum_yielder|
20
+ request_params = params.dup
21
+ begin
22
+ result = get(path, request_params, &block)
23
+
24
+ result['values'].each do |value|
25
+ enum_yielder.yield(value)
26
+ end
27
+
28
+ request_params['from'] = result['from'] + result['count']
29
+ end until result['fullResultSize'] <= request_params['from']
30
+ end
31
+ end
32
+
33
+ def find_entity(path, params = {})
34
+ get(path, params)['value']
35
+ end
36
+
37
+ def create_entity(path, body, params = {})
38
+ post(path, params) do |req|
39
+ req.body = normalize_body(body)
40
+ end['value']
41
+ end
42
+
43
+ def update_entity(path, body, params = {})
44
+ put(path, params) do |req|
45
+ req.body = normalize_body(body)
46
+ end['value']
47
+ end
48
+
49
+ def create_entities(path, body, params = {})
50
+ post(path, params) do |req|
51
+ req.body = normalize_body(body)
52
+ end['values']
53
+ end
54
+
55
+ def update_entities(path, body, params = {})
56
+ put(path, params) do |req|
57
+ req.body = normalize_body(body)
58
+ end['values']
59
+ end
60
+
61
+ def get(path, *args, &block)
62
+ call(:get, path, *args, &block)
63
+ end
64
+
65
+ def post(path, *args, &block)
66
+ call(:post, path, *args, &block)
67
+ end
68
+
69
+ def put(path, *args, &block)
70
+ call(:put, path, *args, &block)
71
+ end
72
+
73
+ def delete(path, *args, &block)
74
+ call(:delete, path, *args, &block)
75
+ end
76
+
77
+ def call(method, path, *args, &block)
78
+ normalized_path = URI.escape(path)
79
+ response = connection.connection.public_send(method, normalized_path, *args, &block)
80
+ handle_response(response)
81
+ end
82
+
83
+ def handle_response(response)
84
+ body = response.body
85
+ case response.status
86
+ when 200, 201, 204
87
+ body
88
+ when 400, 422
89
+ raise ::Tripletexer::Errors::BadRequest, body
90
+ when 401
91
+ raise ::Tripletexer::Errors::Unauthorized, body
92
+ when 403
93
+ raise ::Tripletexer::Errors::Forbidden, body
94
+ when 404
95
+ raise ::Tripletexer::Errors::NotFound, body
96
+ when 409
97
+ raise ::Tripletexer::Errors::Conflict, body
98
+ when 500
99
+ raise ::Tripletexer::Errors::InternalError, body
100
+ else
101
+ raise NotImplementedError, "don't know how to handle #{response.status} http status code"
102
+ end
103
+ end
104
+
105
+ end
106
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tripletexer::Endpoints
4
+ class Activity < AbstractEndpoint
5
+
6
+ # https://tripletex.no/v2-docs/#!/activity/search
7
+ def search(params = {})
8
+ find_entities('/v2/activity', params)
9
+ end
10
+
11
+ # https://tripletex.no/v2-docs/#!/activity/getForTimeSheet
12
+ def get_for_time_sheet(project_id, params = {})
13
+ final_params = params.merge('projectId' => project_id)
14
+ find_entities('/v2/activity/>forTimeSheet', final_params)
15
+ end
16
+
17
+ # https://tripletex.no/v2-docs/#!/activity/get
18
+ def find(id, params = {})
19
+ find_entity("/v2/activity/#{id}", params)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tripletexer::Endpoints
4
+ class Address < AbstractEndpoint
5
+
6
+ # https://tripletex.no/v2-docs/#!/address/search
7
+ def search(params = {})
8
+ find_entities('/v2/address', params)
9
+ end
10
+
11
+ # https://tripletex.no/v2-docs/#!/address/get
12
+ def find(id, params = {})
13
+ find_entity("/v2/address/#{id}", params)
14
+ end
15
+
16
+ # https://tripletex.no/v2-docs/#!/address/put
17
+ def update(id, body)
18
+ update_entity("/v2/address/#{id}", body)
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tripletexer::Endpoints
4
+ class Company < AbstractEndpoint
5
+
6
+ # https://tripletex.no/v2-docs/#!/company/getWithLoginAccess
7
+ def with_login_access(params = {})
8
+ find_entities('/v2/company/>withLoginAccess', params)
9
+ end
10
+
11
+ # https://tripletex.no/v2-docs/#!/company/get
12
+ def find(id, params = {})
13
+ find_entity("/v2/company/#{id}", params)
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tripletexer::Endpoints
4
+ class Contact < AbstractEndpoint
5
+
6
+ # https://tripletex.no/v2-docs/#!/contact/search
7
+ def search(params = {})
8
+ find_entities('/v2/contact', params)
9
+ end
10
+
11
+ # https://tripletex.no/v2-docs/#!/contact/post
12
+ def create(body)
13
+ cerate_entity('/v2/contact', body)
14
+ end
15
+
16
+ # https://tripletex.no/v2-docs/#!/contact/get
17
+ def find(id, params = {})
18
+ find_entity("/v2/contact/#{id}", params)
19
+ end
20
+
21
+ end
22
+ end