tripletexer 0.1.2 → 0.2.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 (38) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -1
  3. data/lib/tripletexer.rb +23 -28
  4. data/lib/tripletexer/api_client.rb +95 -0
  5. data/lib/tripletexer/endpoints/abstract_endpoint.rb +12 -62
  6. data/lib/tripletexer/endpoints/customer.rb +1 -1
  7. data/lib/tripletexer/endpoints/employee.rb +1 -1
  8. data/lib/tripletexer/endpoints/invoice.rb +4 -4
  9. data/lib/tripletexer/endpoints/ledger.rb +10 -10
  10. data/lib/tripletexer/endpoints/ledger/close_group.rb +2 -2
  11. data/lib/tripletexer/endpoints/ledger/posting.rb +2 -2
  12. data/lib/tripletexer/endpoints/ledger/voucher.rb +2 -2
  13. data/lib/tripletexer/endpoints/order.rb +5 -5
  14. data/lib/tripletexer/endpoints/project.rb +1 -1
  15. data/lib/tripletexer/endpoints/timesheet.rb +2 -2
  16. data/lib/tripletexer/endpoints/timesheet/entry.rb +2 -2
  17. data/lib/tripletexer/endpoints/timesheet/time_clock.rb +2 -2
  18. data/lib/tripletexer/endpoints/token.rb +2 -2
  19. data/lib/tripletexer/endpoints/token/session.rb +7 -7
  20. data/lib/tripletexer/format_helpers.rb +8 -5
  21. data/lib/tripletexer/version.rb +1 -1
  22. data/spec/client_spec.rb +56 -0
  23. data/spec/endpoints/order_spec.rb +50 -0
  24. data/spec/endpoints/timesheet_spec.rb +19 -0
  25. data/spec/endpoints/token/session_spec.rb +33 -0
  26. data/spec/endpoints/token_spec.rb +19 -0
  27. data/spec/format_helpers_spec.rb +55 -0
  28. data/spec/spec_helper.rb +111 -0
  29. data/spec/tripletexer_spec.rb +114 -0
  30. data/spec/vcr_cassettes/endpoints/order/find.yml +116 -0
  31. data/spec/vcr_cassettes/endpoints/order/find__unauthorized.yml +71 -0
  32. data/spec/vcr_cassettes/endpoints/order/search.yml +164 -0
  33. data/spec/vcr_cassettes/endpoints/token/session/create.yml +77 -0
  34. data/spec/vcr_cassettes/endpoints/token/session/destroy.yml +295 -0
  35. data/spec/vcr_cassettes/endpoints/token/session/whoami.yml +68 -0
  36. data/spec/web_helper.rb +20 -0
  37. metadata +162 -16
  38. data/lib/tripletexer/connection.rb +0 -49
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 221764e80b338688dfca6518a8730a6452af289a
4
- data.tar.gz: e0040ae9b99e950f8e73d3fca62bf881555eea8c
3
+ metadata.gz: bcf1f36657638f5e3584d071122ae69310909da6
4
+ data.tar.gz: 66a31e734019fd8137144c90c80808c1f8182a1c
5
5
  SHA512:
6
- metadata.gz: 23894199fafbbec759335b9359dd97adf0ad853624e4e22bef685e08671c2c92e08d8ed20e6ba1943e52a63741ea0f795dab915e31aa13e913279bfed63fdc94
7
- data.tar.gz: d00989ab6d5b2d42d68169538bb021d046eb8b2b79277b7e3f3f5c82e31f536aa8a1ebb0399c9978a78dc4950792fcfef8fdad92db8f2213bd9ec8117aa11899
6
+ metadata.gz: a2b71951766e6c75960aedee1f37ae3eb048defd4845feb03935d81da3649cab926613dca0598fa83e8f75ef856bc0bfedd34851043d93f883a89cd49efb47cb
7
+ data.tar.gz: c1fc07477f60e2e9186ff7c480f6a47130e362fad4c4cec344d27c629db61e43484ccb0239091ab783ebd72321e00d531d6aeec2c34ec0684ddce139467d3ec1
data/README.md CHANGED
@@ -1,9 +1,14 @@
1
1
  # Tripletexer - ruby client for tripletex.no API v2
2
2
 
3
+ [![Build Status](https://travis-ci.org/graudeejs/tripletexer.svg?branch=master)](https://travis-ci.org/graudeejs/tripletexer)
4
+ [![CircleCI](https://circleci.com/gh/graudeejs/tripletexer.svg?style=svg)](https://circleci.com/gh/graudeejs/tripletexer)
5
+ [![Code Climate](https://codeclimate.com/github/graudeejs/tripletexer.svg)](https://codeclimate.com/github/graudeejs/tripletexer)
6
+ [![Coverage Status](https://coveralls.io/repos/github/graudeejs/tripletexer/badge.svg?branch=master)](https://coveralls.io/github/graudeejs/tripletexer?branch=master)
7
+
3
8
  ## Getting started
4
9
  Add tripletexer to your Gemfile
5
10
  ```ruby
6
- gem 'tripletexer', '~> 0.1.0'
11
+ gem 'tripletexer', '~> 0.2.0'
7
12
  ```
8
13
  run bundle install
9
14
 
data/lib/tripletexer.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  %w[
4
4
  tripletexer/version.rb
5
5
  tripletexer/errors.rb
6
- tripletexer/connection.rb
6
+ tripletexer/api_client.rb
7
7
  tripletexer/format_helpers.rb
8
8
  tripletexer/endpoints.rb
9
9
  tripletexer/endpoints/abstract_endpoint.rb
@@ -47,90 +47,85 @@
47
47
  end
48
48
 
49
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
50
+ def initialize(api_client: Tripletexer::APIClient.new, session_token: nil)
51
+ @api_client = api_client
52
+ @api_client.session_token = session_token if session_token
54
53
  end
55
54
 
56
55
  def activity
57
- Tripletexer::Endpoints::Activity.new(connection)
56
+ Tripletexer::Endpoints::Activity.new(api_client)
58
57
  end
59
58
 
60
59
  def address
61
- Tripletexer::Endpoints::Address.new(connection)
60
+ Tripletexer::Endpoints::Address.new(api_client)
62
61
  end
63
62
 
64
63
  def company
65
- Tripletexer::Endpoints::Company.new(connection)
64
+ Tripletexer::Endpoints::Company.new(api_client)
66
65
  end
67
66
 
68
67
  def contact
69
- Tripletexer::Endpoints::Contact.new(connection)
68
+ Tripletexer::Endpoints::Contact.new(api_client)
70
69
  end
71
70
 
72
71
  def country
73
- Tripletexer::Endpoints::Country.new(connection)
72
+ Tripletexer::Endpoints::Country.new(api_client)
74
73
  end
75
74
 
76
75
  def currency
77
- Tripletexer::Endpoints::Currency.new(connection)
76
+ Tripletexer::Endpoints::Currency.new(api_client)
78
77
  end
79
78
 
80
79
  def customer
81
- Tripletexer::Endpoints::Customer.new(connection)
80
+ Tripletexer::Endpoints::Customer.new(api_client)
82
81
  end
83
82
 
84
83
  def department
85
- Tripletexer::Endpoints::Department.new(connection)
84
+ Tripletexer::Endpoints::Department.new(api_client)
86
85
  end
87
86
 
88
87
  def employee
89
- Tripletexer::Endpoints::Employee.new(connection)
88
+ Tripletexer::Endpoints::Employee.new(api_client)
90
89
  end
91
90
 
92
91
  def inventory
93
- Tripletexer::Endpoints::Inventory.new(connection)
92
+ Tripletexer::Endpoints::Inventory.new(api_client)
94
93
  end
95
94
 
96
95
  def invoice
97
- Tripletexer::Endpoints::Invoice.new(connection)
96
+ Tripletexer::Endpoints::Invoice.new(api_client)
98
97
  end
99
98
 
100
99
  def ledger
101
- Tripletexer::Endpoints::Ledger.new(connection)
100
+ Tripletexer::Endpoints::Ledger.new(api_client)
102
101
  end
103
102
 
104
103
  def order
105
- Tripletexer::Endpoints::Order.new(connection)
104
+ Tripletexer::Endpoints::Order.new(api_client)
106
105
  end
107
106
 
108
107
  def product
109
- Tripletexer::Endpoints::Product.new(connection)
108
+ Tripletexer::Endpoints::Product.new(api_client)
110
109
  end
111
110
 
112
111
  def project
113
- Tripletexer::Endpoints::Project.new(connection)
112
+ Tripletexer::Endpoints::Project.new(api_client)
114
113
  end
115
114
 
116
115
  def supplier
117
- Tripletexer::Endpoints::Supplier.new(connection)
116
+ Tripletexer::Endpoints::Supplier.new(api_client)
118
117
  end
119
118
 
120
119
  def timesheet
121
- Tripletexer::Endpoints::Timesheet.new(connection)
120
+ Tripletexer::Endpoints::Timesheet.new(api_client)
122
121
  end
123
122
 
124
123
  def token
125
- Tripletexer::Endpoints::Token.new(connection)
124
+ Tripletexer::Endpoints::Token.new(api_client)
126
125
  end
127
126
 
128
127
  private
129
128
 
130
- attr_accessor :object_class, :proxy
131
-
132
- def connection
133
- @connection ||= Tripletexer::Connection.new(object_class: object_class, proxy: proxy)
134
- end
129
+ attr_accessor :api_client
135
130
 
136
131
  end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri'
4
+ require 'json'
5
+ require 'faraday'
6
+ require 'faraday_middleware'
7
+
8
+
9
+ class Tripletexer::APIClient
10
+ ENDPOINT = 'https://tripletex.no/'
11
+
12
+ attr_reader :session_token, :debug
13
+
14
+ def initialize(object_class: Hash, proxy: nil, debug: false)
15
+ @object_class = object_class
16
+ @proxy = proxy
17
+ @debug = debug
18
+ end
19
+
20
+ def connection
21
+ return @connection if session_token && @connection
22
+ @connection = init_connection
23
+ end
24
+
25
+ def reset_connection
26
+ @session_token = @connection = nil
27
+ end
28
+
29
+ def session_token=(new_session_token)
30
+ reset_connection
31
+ @session_token = new_session_token
32
+ end
33
+
34
+ def get(path, *args, &block)
35
+ call(:get, path, *args, &block)
36
+ end
37
+
38
+ def post(path, *args, &block)
39
+ call(:post, path, *args, &block)
40
+ end
41
+
42
+ def put(path, *args, &block)
43
+ call(:put, path, *args, &block)
44
+ end
45
+
46
+ def delete(path, *args, &block)
47
+ call(:delete, path, *args, &block)
48
+ end
49
+
50
+ private
51
+
52
+ attr_reader :object_class, :proxy
53
+
54
+ def init_connection
55
+ Faraday.new(url: ENDPOINT) do |faraday|
56
+ faraday.response :logger if debug
57
+ faraday.response :json, parser_options: { object_class: object_class }, content_type: %r[/json$]
58
+ faraday.headers = {
59
+ 'Content-Type' => 'application/json'
60
+ }
61
+ faraday.adapter :net_http
62
+ faraday.basic_auth(0, session_token) if session_token
63
+ faraday.proxy = proxy if proxy # https://github.com/lostisland/faraday/issues/733
64
+ end
65
+ end
66
+
67
+ def call(method, path, *args, &block)
68
+ normalized_path = URI.escape(path)
69
+ response = connection.public_send(method, normalized_path, *args, &block)
70
+ handle_response(response)
71
+ end
72
+
73
+ def handle_response(response)
74
+ body = response.body
75
+ case response.status
76
+ when 200, 201, 204
77
+ body
78
+ when 400, 422
79
+ raise ::Tripletexer::Errors::BadRequest, body
80
+ when 401
81
+ raise ::Tripletexer::Errors::Unauthorized, body
82
+ when 403
83
+ raise ::Tripletexer::Errors::Forbidden, body
84
+ when 404
85
+ raise ::Tripletexer::Errors::NotFound, body
86
+ when 409
87
+ raise ::Tripletexer::Errors::Conflict, body
88
+ when 500
89
+ raise ::Tripletexer::Errors::InternalError, body
90
+ else
91
+ raise NotImplementedError, "don't know how to handle #{response.status} http status code"
92
+ end
93
+ end
94
+
95
+ end
@@ -1,25 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
4
- require 'uri'
5
-
6
3
  module Tripletexer::Endpoints
7
4
  class AbstractEndpoint
8
- include ::Tripletexer::FormatHelpers
9
5
 
10
- def initialize(connection)
11
- @connection = connection
6
+ def initialize(api_client)
7
+ @api_client = api_client
12
8
  end
13
9
 
14
10
  private
15
11
 
16
- attr_reader :connection
12
+ attr_reader :api_client
17
13
 
18
14
  def find_entities(path, params, &block)
19
15
  Enumerator.new do |enum_yielder|
20
16
  request_params = params.dup
21
17
  begin
22
- result = get(path, request_params, &block)
18
+ result = api_client.get(path, request_params, &block)
23
19
 
24
20
  result['values'].each do |value|
25
21
  enum_yielder.yield(value)
@@ -31,74 +27,28 @@ module Tripletexer::Endpoints
31
27
  end
32
28
 
33
29
  def find_entity(path, params = {})
34
- get(path, params)['value']
30
+ api_client.get(path, params)['value']
35
31
  end
36
32
 
37
33
  def create_entity(path, body, params = {})
38
- post(path, params) do |req|
39
- req.body = normalize_body(body)
40
- end['value']
34
+ send_data(:post, path, body, params)['value']
41
35
  end
42
36
 
43
37
  def update_entity(path, body, params = {})
44
- put(path, params) do |req|
45
- req.body = normalize_body(body)
46
- end['value']
38
+ send_data(:put, path, body, params)['value']
47
39
  end
48
40
 
49
41
  def create_entities(path, body, params = {})
50
- post(path, params) do |req|
51
- req.body = normalize_body(body)
52
- end['values']
42
+ send_data(:post, path, body, params)['values']
53
43
  end
54
44
 
55
45
  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)
46
+ send_data(:put, path, body, params)['values']
81
47
  end
82
48
 
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"
49
+ def send_data(method, path, body, params)
50
+ api_client.public_send(method, path, params) do |req|
51
+ req.body = ::Tripletexer::FormatHelpers.normalize_body(body)
102
52
  end
103
53
  end
104
54
 
@@ -24,7 +24,7 @@ module Tripletexer::Endpoints
24
24
  end
25
25
 
26
26
  def category
27
- Tripletexer::Endpoints::Customer::Category.new(connection)
27
+ Tripletexer::Endpoints::Customer::Category.new(api_client)
28
28
  end
29
29
 
30
30
  end
@@ -13,7 +13,7 @@ module Tripletexer::Endpoints
13
13
  end
14
14
 
15
15
  def entitlement
16
- Tripletexer::Endpoints::Employee::Entitlement.new(connection)
16
+ Tripletexer::Endpoints::Employee::Entitlement.new(api_client)
17
17
  end
18
18
 
19
19
  end
@@ -6,8 +6,8 @@ module Tripletexer::Endpoints
6
6
  # https://tripletex.no/v2-docs/#!/invoice/search
7
7
  def search(date_from, date_to, params = {})
8
8
  final_params = params.merge(
9
- 'invoiceDateFrom' => format_date(date_from),
10
- 'invoiceDateTo' => format_date(date_to)
9
+ 'invoiceDateFrom' => ::Tripletexer::FormatHelpers.format_date(date_from),
10
+ 'invoiceDateTo' => ::Tripletexer::FormatHelpers.format_date(date_to)
11
11
  )
12
12
  find_entities('/v2/invoice', final_params)
13
13
  end
@@ -34,11 +34,11 @@ module Tripletexer::Endpoints
34
34
  'paymentTypeId' => payment_type_id,
35
35
  'paidAmount' => paid_amount
36
36
  }
37
- put("/v2/invoice/#{id}/:payment", final_params)
37
+ api_client.put("/v2/invoice/#{id}/:payment", final_params)
38
38
  end
39
39
 
40
40
  def payment_type
41
- Tripletexer::Endpoints::Invoice::PaymentType.new(connection)
41
+ Tripletexer::Endpoints::Invoice::PaymentType.new(api_client)
42
42
  end
43
43
  end
44
44
  end
@@ -7,43 +7,43 @@ module Tripletexer::Endpoints
7
7
  # https://tripletex.no/v2-docs/#!/ledger/search
8
8
  def search(date_from, date_to, params = {})
9
9
  final_params = params.merge(
10
- 'dateFrom' => format_date(date_from),
11
- 'dateTo' => format_date(date_to)
10
+ 'dateFrom' => ::Tripletexer::FormatHelpers.format_date(date_from),
11
+ 'dateTo' => ::Tripletexer::FormatHelpers.format_date(date_to)
12
12
  )
13
13
  final_params['fields'] = DEFAULT_FIELDS unless final_params.key?('fields')
14
14
  find_entities('/v2/ledger', final_params)
15
15
  end
16
16
 
17
17
  def account
18
- Tripletexer::Endpoints::Ledger::Account.new(connection)
18
+ Tripletexer::Endpoints::Ledger::Account.new(api_client)
19
19
  end
20
20
 
21
21
  def accounting_period
22
- Tripletexer::Endpoints::Ledger::AccountingPeriod.new(connection)
22
+ Tripletexer::Endpoints::Ledger::AccountingPeriod.new(api_client)
23
23
  end
24
24
 
25
25
  def annual_account
26
- Tripletexer::Endpoints::Ledger::AnnualAccount.new(connection)
26
+ Tripletexer::Endpoints::Ledger::AnnualAccount.new(api_client)
27
27
  end
28
28
 
29
29
  def close_group
30
- Tripletexer::Endpoints::Ledger::CloseGroup.new(connection)
30
+ Tripletexer::Endpoints::Ledger::CloseGroup.new(api_client)
31
31
  end
32
32
 
33
33
  def posting
34
- Tripletexer::Endpoints::Ledger::Posting.new(connection)
34
+ Tripletexer::Endpoints::Ledger::Posting.new(api_client)
35
35
  end
36
36
 
37
37
  def vat_type
38
- Tripletexer::Endpoints::Ledger::VatType.new(connection)
38
+ Tripletexer::Endpoints::Ledger::VatType.new(api_client)
39
39
  end
40
40
 
41
41
  def voucher
42
- Tripletexer::Endpoints::Ledger::Voucher.new(connection)
42
+ Tripletexer::Endpoints::Ledger::Voucher.new(api_client)
43
43
  end
44
44
 
45
45
  def type
46
- Tripletexer::Endpoints::Ledger::Type.new(connection)
46
+ Tripletexer::Endpoints::Ledger::Type.new(api_client)
47
47
  end
48
48
  end
49
49
  end