fakturoid 0.1.1 → 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.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/CHANGELOG.md +9 -1
- data/README.md +6 -6
- data/Rakefile +1 -1
- data/fakturoid.gemspec +2 -2
- data/lib/fakturoid.rb +4 -4
- data/lib/fakturoid/api.rb +2 -2
- data/lib/fakturoid/api/arguments.rb +2 -2
- data/lib/fakturoid/api/http_methods.rb +3 -3
- data/lib/fakturoid/client/event.rb +3 -3
- data/lib/fakturoid/client/expense.rb +7 -7
- data/lib/fakturoid/client/generator.rb +9 -9
- data/lib/fakturoid/client/invoice.rb +18 -15
- data/lib/fakturoid/client/subject.rb +6 -6
- data/lib/fakturoid/client/todo.rb +2 -2
- data/lib/fakturoid/client/user.rb +2 -2
- data/lib/fakturoid/config.rb +5 -5
- data/lib/fakturoid/connection.rb +3 -3
- data/lib/fakturoid/request.rb +5 -5
- data/lib/fakturoid/response.rb +14 -14
- data/lib/fakturoid/version.rb +1 -1
- data/test/api_test.rb +2 -2
- data/test/config_test.rb +4 -4
- data/test/fixtures/blocked_account.json +1 -1
- data/test/fixtures/invoice.json +1 -1
- data/test/fixtures/invoice_error.json +1 -1
- data/test/fixtures/subjects.json +2 -2
- data/test/request_test.rb +1 -1
- data/test/response_test.rb +28 -28
- data/test/test_helper.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6b2689a302bc8c51aad83f0adace9767fb2b3fed
|
|
4
|
+
data.tar.gz: 401da40fc65ad68fd261f6cc4c1be79b1e8b357f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 72dc160cbff635c30ba4037ec5152006ab1f8263a1fcbe9d9a86444995fdbf7c6e65ddb6c7e56c15cb7c11fb2d7789a199088622d4ab8c0dba4d0b8b0deabd42
|
|
7
|
+
data.tar.gz: 5ce5f5b56d266c1be628bc24162eb8dce0f5e2e65ed03538c090b6a38e94e2cd19fc0c90b341c25ea9ef792f0b3a61d089a82478b07a5462d1cdbdf6718926f4
|
data/.ruby-version
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.
|
|
1
|
+
2.3.4
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
## 0.2.0
|
|
2
|
+
|
|
3
|
+
- Add support for `paid_at` param in invoice `fire` method.
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
Fakturoid::Client::Invoice.fire(1234, 'pay', paid_at: '2017-07-03')
|
|
7
|
+
```
|
|
8
|
+
|
|
1
9
|
## 0.1.1
|
|
2
10
|
|
|
3
11
|
- Added support for `updated_since` param.
|
|
@@ -8,7 +16,7 @@
|
|
|
8
16
|
invoice = Fakturoid::Client::Invoice.find(1234)
|
|
9
17
|
invoice.respond_to?(:number)
|
|
10
18
|
# => false
|
|
11
|
-
|
|
19
|
+
|
|
12
20
|
### After
|
|
13
21
|
invoice = Fakturoid::Client::Invoice.find(1234)
|
|
14
22
|
invoice.respond_to?(:number)
|
data/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Fakturoid
|
|
2
2
|
|
|
3
3
|
The Fakturoid gem is ruby library for API communication with web based invoicing service [www.fakturoid.cz](https://fakturoid.cz).
|
|
4
|
-
Fakturoid [API documentation](http://docs.fakturoid.apiary.io).
|
|
4
|
+
Fakturoid [API documentation](http://docs.fakturoid.apiary.io).
|
|
5
5
|
|
|
6
6
|
[](http://badge.fury.io/rb/fakturoid)
|
|
7
7
|
[](https://circleci.com/gh/fakturoid/fakturoid-ruby)
|
|
@@ -148,11 +148,11 @@ You can create new invoice with:
|
|
|
148
148
|
|
|
149
149
|
```ruby
|
|
150
150
|
invoice = {
|
|
151
|
-
subject_id: 123,
|
|
151
|
+
subject_id: 123,
|
|
152
152
|
lines: [
|
|
153
|
-
{
|
|
154
|
-
quantity: 5,
|
|
155
|
-
unit_name: 'kg',
|
|
153
|
+
{
|
|
154
|
+
quantity: 5,
|
|
155
|
+
unit_name: 'kg',
|
|
156
156
|
name: 'Sand',
|
|
157
157
|
unit_price: '100',
|
|
158
158
|
vat_rate: 21
|
|
@@ -260,4 +260,4 @@ The Fakturoid gem raises exceptions if error response is returned from the serve
|
|
|
260
260
|
</table>
|
|
261
261
|
|
|
262
262
|
## Thanks
|
|
263
|
-
Development was supported by [eBallance Creative s.r.o.](http://www.eballance.cz)
|
|
263
|
+
Development was supported by [eBallance Creative s.r.o.](http://www.eballance.cz)
|
data/Rakefile
CHANGED
data/fakturoid.gemspec
CHANGED
|
@@ -17,10 +17,10 @@ Gem::Specification.new do |s|
|
|
|
17
17
|
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
18
18
|
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
|
19
19
|
s.require_paths = ["lib"]
|
|
20
|
-
|
|
20
|
+
|
|
21
21
|
s.add_dependency 'multi_json'
|
|
22
22
|
s.add_dependency 'faraday'
|
|
23
|
-
|
|
23
|
+
|
|
24
24
|
s.add_development_dependency "bundler", "> 1"
|
|
25
25
|
s.add_development_dependency "rake"
|
|
26
26
|
s.add_development_dependency "minitest"
|
data/lib/fakturoid.rb
CHANGED
|
@@ -14,14 +14,14 @@ require 'fakturoid/railtie' if defined?(::Rails)
|
|
|
14
14
|
module Fakturoid
|
|
15
15
|
class ApiError < StandardError
|
|
16
16
|
attr_accessor :response_code, :response_body
|
|
17
|
-
|
|
17
|
+
|
|
18
18
|
def initialize(message = nil, response_code = nil, response_body = nil)
|
|
19
19
|
super(message)
|
|
20
20
|
self.response_code = response_code
|
|
21
21
|
self.response_body = response_body
|
|
22
22
|
end
|
|
23
23
|
end
|
|
24
|
-
|
|
24
|
+
|
|
25
25
|
class ContentTypeError < ApiError; end
|
|
26
26
|
class UserAgentError < ApiError; end
|
|
27
27
|
class AuthenticationError < ApiError; end
|
|
@@ -36,10 +36,10 @@ module Fakturoid
|
|
|
36
36
|
class SubjectLimitError < ApiError; end
|
|
37
37
|
class GeneratorLimitError < ApiError; end
|
|
38
38
|
class UnsupportedFeatureError < ApiError; end
|
|
39
|
-
|
|
39
|
+
|
|
40
40
|
class ClientError < ApiError; end
|
|
41
41
|
class ServerError < ApiError; end
|
|
42
|
-
|
|
42
|
+
|
|
43
43
|
def self.configure(&block)
|
|
44
44
|
Fakturoid::Api.configure(&block)
|
|
45
45
|
end
|
data/lib/fakturoid/api.rb
CHANGED
|
@@ -4,12 +4,12 @@ module Fakturoid
|
|
|
4
4
|
def permit_params(params_hash, *permitted_params)
|
|
5
5
|
params_hash.select { |param, _value| permitted_params.include?(param.to_sym) }
|
|
6
6
|
end
|
|
7
|
-
|
|
7
|
+
|
|
8
8
|
def validate_numerical_id(id)
|
|
9
9
|
raise ArgumentError, "Wrong ID given: #{id}" if !id.is_a?(Integer) && !(id.is_a?(String) && id =~ /\A\d+\z/)
|
|
10
10
|
true
|
|
11
11
|
end
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
def validate_search_query(query)
|
|
14
14
|
raise ArgumentError, 'Query parameter is required' if query.nil? || query.empty?
|
|
15
15
|
true
|
|
@@ -4,15 +4,15 @@ module Fakturoid
|
|
|
4
4
|
def get_request(path, params = {})
|
|
5
5
|
Request.new(:get, path, self).call(params)
|
|
6
6
|
end
|
|
7
|
-
|
|
7
|
+
|
|
8
8
|
def post_request(path, params = {})
|
|
9
9
|
Request.new(:post, path, self).call(params)
|
|
10
10
|
end
|
|
11
|
-
|
|
11
|
+
|
|
12
12
|
def patch_request(path, params = {})
|
|
13
13
|
Request.new(:patch, path, self).call(params)
|
|
14
14
|
end
|
|
15
|
-
|
|
15
|
+
|
|
16
16
|
def delete_request(path, params = {})
|
|
17
17
|
Request.new(:delete, path, self).call(params)
|
|
18
18
|
end
|
|
@@ -3,13 +3,13 @@ module Fakturoid
|
|
|
3
3
|
class Event < Fakturoid::Api
|
|
4
4
|
def self.all(params = {})
|
|
5
5
|
request_params = permit_params(params, :page, :since, :subject_id) || {}
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
get_request('events.json', request_params: request_params)
|
|
8
8
|
end
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
def self.paid(params = {})
|
|
11
11
|
request_params = permit_params(params, :page, :since, :subject_id) || {}
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
get_request('events/paid.json', request_params: request_params)
|
|
14
14
|
end
|
|
15
15
|
end
|
|
@@ -3,34 +3,34 @@ module Fakturoid
|
|
|
3
3
|
class Expense < Fakturoid::Api
|
|
4
4
|
def self.all(params = {})
|
|
5
5
|
request_params = permit_params(params, :page, :since, :updated_since, :number, :variable_symbol, :status, :subject_id) || {}
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
get_request('expenses.json', request_params: request_params)
|
|
8
8
|
end
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
def self.find(id)
|
|
11
11
|
validate_numerical_id(id)
|
|
12
12
|
get_request("expenses/#{id}.json")
|
|
13
13
|
end
|
|
14
|
-
|
|
14
|
+
|
|
15
15
|
def self.search(query)
|
|
16
16
|
validate_search_query(query)
|
|
17
17
|
get_request('expenses/search.json', request_params: { query: query })
|
|
18
18
|
end
|
|
19
|
-
|
|
19
|
+
|
|
20
20
|
def self.fire(id, event)
|
|
21
21
|
validate_numerical_id(id)
|
|
22
22
|
post_request("expenses/#{id}/fire.json", request_params: { event: event })
|
|
23
23
|
end
|
|
24
|
-
|
|
24
|
+
|
|
25
25
|
def self.create(payload = {})
|
|
26
26
|
post_request('expenses.json', payload: payload)
|
|
27
27
|
end
|
|
28
|
-
|
|
28
|
+
|
|
29
29
|
def self.update(id, payload = {})
|
|
30
30
|
validate_numerical_id(id)
|
|
31
31
|
patch_request("expenses/#{id}.json", payload: payload)
|
|
32
32
|
end
|
|
33
|
-
|
|
33
|
+
|
|
34
34
|
def self.delete(id)
|
|
35
35
|
validate_numerical_id(id)
|
|
36
36
|
delete_request("expenses/#{id}.json")
|
|
@@ -3,36 +3,36 @@ module Fakturoid
|
|
|
3
3
|
class Generator < Fakturoid::Api
|
|
4
4
|
def self.all(params = {})
|
|
5
5
|
request_params = permit_params(params, :page, :since, :updated_since, :subject_id) || {}
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
get_request('generators.json', request_params: request_params)
|
|
8
8
|
end
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
def self.recurring(params = {})
|
|
11
11
|
request_params = permit_params(params, :page, :since, :updated_since, :subject_id) || {}
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
get_request('generators/recurring.json', request_params: request_params)
|
|
14
14
|
end
|
|
15
|
-
|
|
15
|
+
|
|
16
16
|
def self.template(params = {})
|
|
17
17
|
request_params = permit_params(params, :page, :since, :updated_since, :subject_id) || {}
|
|
18
|
-
|
|
18
|
+
|
|
19
19
|
get_request('generators/template.json', request_params: request_params)
|
|
20
20
|
end
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
def self.find(id)
|
|
23
23
|
validate_numerical_id(id)
|
|
24
24
|
get_request("generators/#{id}.json")
|
|
25
25
|
end
|
|
26
|
-
|
|
26
|
+
|
|
27
27
|
def self.create(payload = {})
|
|
28
28
|
post_request('generators.json', payload: payload)
|
|
29
29
|
end
|
|
30
|
-
|
|
30
|
+
|
|
31
31
|
def self.update(id, payload = {})
|
|
32
32
|
validate_numerical_id(id)
|
|
33
33
|
patch_request("generators/#{id}.json", payload: payload)
|
|
34
34
|
end
|
|
35
|
-
|
|
35
|
+
|
|
36
36
|
def self.delete(id)
|
|
37
37
|
validate_numerical_id(id)
|
|
38
38
|
delete_request("generators/#{id}.json")
|
|
@@ -3,56 +3,59 @@ module Fakturoid
|
|
|
3
3
|
class Invoice < Fakturoid::Api
|
|
4
4
|
def self.all(params = {})
|
|
5
5
|
request_params = permit_params(params, :page, :since, :updated_since, :number, :status, :subject_id) || {}
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
get_request('invoices.json', request_params: request_params)
|
|
8
8
|
end
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
def self.regular(params = {})
|
|
11
11
|
request_params = permit_params(params, :page, :since, :updated_since, :number, :status, :subject_id) || {}
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
get_request('invoices/regular.json', request_params: request_params)
|
|
14
14
|
end
|
|
15
|
-
|
|
15
|
+
|
|
16
16
|
def self.proforma(params = {})
|
|
17
17
|
request_params = permit_params(params, :page, :since, :updated_since, :number, :status, :subject_id) || {}
|
|
18
|
-
|
|
18
|
+
|
|
19
19
|
get_request('invoices/proforma.json', request_params: request_params)
|
|
20
20
|
end
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
def self.find(id)
|
|
23
23
|
validate_numerical_id(id)
|
|
24
24
|
get_request("invoices/#{id}.json")
|
|
25
25
|
end
|
|
26
|
-
|
|
26
|
+
|
|
27
27
|
def self.search(query)
|
|
28
28
|
validate_search_query(query)
|
|
29
29
|
get_request('invoices/search.json', request_params: { query: query })
|
|
30
30
|
end
|
|
31
|
-
|
|
31
|
+
|
|
32
32
|
def self.download_pdf(id)
|
|
33
33
|
validate_numerical_id(id)
|
|
34
34
|
get_request("invoices/#{id}/download.pdf", headers: { content_type: 'application/pdf' })
|
|
35
35
|
end
|
|
36
|
-
|
|
37
|
-
def self.fire(id, event)
|
|
36
|
+
|
|
37
|
+
def self.fire(id, event, params = {})
|
|
38
|
+
request_params = permit_params(params, :paid_at) || {}
|
|
39
|
+
request_params[:event] = event
|
|
40
|
+
|
|
38
41
|
validate_numerical_id(id)
|
|
39
|
-
post_request("invoices/#{id}/fire.json", request_params:
|
|
42
|
+
post_request("invoices/#{id}/fire.json", request_params: request_params)
|
|
40
43
|
end
|
|
41
|
-
|
|
44
|
+
|
|
42
45
|
def self.deliver_message(invoice_id, payload = {})
|
|
43
46
|
validate_numerical_id(invoice_id)
|
|
44
47
|
post_request("invoices/#{invoice_id}/message.json", payload: payload)
|
|
45
48
|
end
|
|
46
|
-
|
|
49
|
+
|
|
47
50
|
def self.create(payload = {})
|
|
48
51
|
post_request('invoices.json', payload: payload)
|
|
49
52
|
end
|
|
50
|
-
|
|
53
|
+
|
|
51
54
|
def self.update(id, payload = {})
|
|
52
55
|
validate_numerical_id(id)
|
|
53
56
|
patch_request("invoices/#{id}.json", payload: payload)
|
|
54
57
|
end
|
|
55
|
-
|
|
58
|
+
|
|
56
59
|
def self.delete(id)
|
|
57
60
|
validate_numerical_id(id)
|
|
58
61
|
delete_request("invoices/#{id}.json")
|
|
@@ -3,29 +3,29 @@ module Fakturoid
|
|
|
3
3
|
class Subject < Fakturoid::Api
|
|
4
4
|
def self.all(params = {})
|
|
5
5
|
request_params = permit_params(params, :page, :since, :updated_since, :custom_id) || {}
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
get_request('subjects.json', request_params: request_params)
|
|
8
8
|
end
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
def self.find(id)
|
|
11
11
|
validate_numerical_id(id)
|
|
12
12
|
get_request("subjects/#{id}.json")
|
|
13
13
|
end
|
|
14
|
-
|
|
14
|
+
|
|
15
15
|
def self.search(query)
|
|
16
16
|
validate_search_query(query)
|
|
17
17
|
get_request('subjects/search.json', request_params: { query: query })
|
|
18
18
|
end
|
|
19
|
-
|
|
19
|
+
|
|
20
20
|
def self.create(payload = {})
|
|
21
21
|
post_request('subjects.json', payload: payload)
|
|
22
22
|
end
|
|
23
|
-
|
|
23
|
+
|
|
24
24
|
def self.update(id, payload = {})
|
|
25
25
|
validate_numerical_id(id)
|
|
26
26
|
patch_request("subjects/#{id}.json", payload: payload)
|
|
27
27
|
end
|
|
28
|
-
|
|
28
|
+
|
|
29
29
|
def self.delete(id)
|
|
30
30
|
validate_numerical_id(id)
|
|
31
31
|
delete_request("subjects/#{id}.json")
|
|
@@ -3,10 +3,10 @@ module Fakturoid
|
|
|
3
3
|
class Todo < Fakturoid::Api
|
|
4
4
|
def self.all(params = {})
|
|
5
5
|
request_params = permit_params(params, :page, :since) || {}
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
get_request('todos.json', request_params: request_params)
|
|
8
8
|
end
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
def self.toggle_completion(id)
|
|
11
11
|
validate_numerical_id(id)
|
|
12
12
|
post_request("todos/#{id}/toggle_completion.json")
|
|
@@ -4,12 +4,12 @@ module Fakturoid
|
|
|
4
4
|
def self.current
|
|
5
5
|
get_request('user.json', url: Fakturoid::Api.config.endpoint_without_account)
|
|
6
6
|
end
|
|
7
|
-
|
|
7
|
+
|
|
8
8
|
def self.find(id)
|
|
9
9
|
validate_numerical_id(id)
|
|
10
10
|
get_request("users/#{id}.json")
|
|
11
11
|
end
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
def self.all
|
|
14
14
|
get_request('users.json')
|
|
15
15
|
end
|
data/lib/fakturoid/config.rb
CHANGED
|
@@ -2,13 +2,13 @@ module Fakturoid
|
|
|
2
2
|
class Config
|
|
3
3
|
attr_accessor :email, :api_key, :account
|
|
4
4
|
attr_writer :user_agent
|
|
5
|
-
|
|
5
|
+
|
|
6
6
|
ENDPOINT = 'https://app.fakturoid.cz/api/v2'
|
|
7
|
-
|
|
7
|
+
|
|
8
8
|
def initialize(&_block)
|
|
9
9
|
yield self
|
|
10
10
|
end
|
|
11
|
-
|
|
11
|
+
|
|
12
12
|
def user_agent
|
|
13
13
|
if @user_agent.nil? || @user_agent.empty?
|
|
14
14
|
"Fakturoid ruby gem (#{email})"
|
|
@@ -16,11 +16,11 @@ module Fakturoid
|
|
|
16
16
|
@user_agent
|
|
17
17
|
end
|
|
18
18
|
end
|
|
19
|
-
|
|
19
|
+
|
|
20
20
|
def endpoint
|
|
21
21
|
"#{ENDPOINT}/accounts/#{account}"
|
|
22
22
|
end
|
|
23
|
-
|
|
23
|
+
|
|
24
24
|
def endpoint_without_account
|
|
25
25
|
ENDPOINT
|
|
26
26
|
end
|
data/lib/fakturoid/connection.rb
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
module Fakturoid
|
|
2
2
|
module Connection
|
|
3
|
-
|
|
3
|
+
|
|
4
4
|
def default_options(options = {})
|
|
5
5
|
content_type = options[:headers] && options[:headers][:content_type]
|
|
6
6
|
{
|
|
@@ -11,11 +11,11 @@ module Fakturoid
|
|
|
11
11
|
url: options[:url] || Fakturoid::Api.config.endpoint
|
|
12
12
|
}
|
|
13
13
|
end
|
|
14
|
-
|
|
14
|
+
|
|
15
15
|
def connection(options = {})
|
|
16
16
|
@connection = Faraday.new default_options(options)
|
|
17
17
|
@connection.basic_auth(Fakturoid::Api.config.email, Fakturoid::Api.config.api_key)
|
|
18
|
-
|
|
18
|
+
|
|
19
19
|
@connection
|
|
20
20
|
end
|
|
21
21
|
end
|
data/lib/fakturoid/request.rb
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
module Fakturoid
|
|
2
2
|
class Request
|
|
3
3
|
include Connection
|
|
4
|
-
|
|
4
|
+
|
|
5
5
|
attr_reader :method, :path, :caller
|
|
6
6
|
HTTP_METHODS = [:get, :post, :patch, :delete]
|
|
7
|
-
|
|
7
|
+
|
|
8
8
|
def initialize(method, path, caller)
|
|
9
9
|
@method = method
|
|
10
10
|
@path = path
|
|
11
11
|
@caller = caller
|
|
12
12
|
end
|
|
13
|
-
|
|
13
|
+
|
|
14
14
|
def call(params = {})
|
|
15
15
|
raise ArgumentError, "Unknown http method: #{method}" unless HTTP_METHODS.include?(method.to_sym)
|
|
16
|
-
|
|
16
|
+
|
|
17
17
|
request_params = params[:request_params] || {}
|
|
18
|
-
|
|
18
|
+
|
|
19
19
|
http_connection = connection(params)
|
|
20
20
|
response = http_connection.send(method) do |req|
|
|
21
21
|
req.url path, request_params
|
data/lib/fakturoid/response.rb
CHANGED
|
@@ -1,45 +1,45 @@
|
|
|
1
1
|
module Fakturoid
|
|
2
2
|
class Response
|
|
3
3
|
attr_reader :response, :caller, :env, :body, :request_method
|
|
4
|
-
|
|
4
|
+
|
|
5
5
|
def initialize(faraday_response, caller, request_method)
|
|
6
6
|
@response = faraday_response
|
|
7
7
|
@caller = caller
|
|
8
8
|
@env = faraday_response.env
|
|
9
9
|
@request_method = request_method.to_sym
|
|
10
|
-
|
|
10
|
+
|
|
11
11
|
if !(env.body.nil? || env.body.empty? || (json? && env.body =~ /\A\s+\z/))
|
|
12
12
|
@body = json? ? MultiJson.load(env.body) : env.body
|
|
13
13
|
end
|
|
14
14
|
handle_response
|
|
15
15
|
end
|
|
16
|
-
|
|
16
|
+
|
|
17
17
|
def status_code
|
|
18
18
|
env['status']
|
|
19
19
|
end
|
|
20
|
-
|
|
20
|
+
|
|
21
21
|
def json?
|
|
22
22
|
env.request_headers['Content-Type'] == 'application/json'
|
|
23
23
|
end
|
|
24
|
-
|
|
24
|
+
|
|
25
25
|
def headers
|
|
26
26
|
env.response_headers
|
|
27
27
|
end
|
|
28
|
-
|
|
28
|
+
|
|
29
29
|
def inspect
|
|
30
30
|
"#<#{self.class.name}:#{object_id} @body=\"#{body}\" @status_code=\"#{status_code}\">"
|
|
31
31
|
end
|
|
32
|
-
|
|
32
|
+
|
|
33
33
|
private
|
|
34
|
-
|
|
34
|
+
|
|
35
35
|
def handle_response
|
|
36
36
|
case status_code
|
|
37
|
-
when 400
|
|
37
|
+
when 400
|
|
38
38
|
raise error(UserAgentError, 'User-Agent header missing') if env.request_headers['User-Agent'].nil? || env.request_headers['User-Agent'].empty?
|
|
39
39
|
raise error(PaginationError, 'Page does not exist')
|
|
40
40
|
when 401 then raise error(AuthenticationError, 'Authentification failed')
|
|
41
41
|
when 402 then raise error(BlockedAccountError, 'Account is blocked')
|
|
42
|
-
when 403 then
|
|
42
|
+
when 403 then
|
|
43
43
|
raise error(DestroySubjectError, 'Cannot destroy subject with invoices') if caller == Client::Subject && request_method == :delete
|
|
44
44
|
raise error(SubjectLimitError, 'Subject limit for account reached') if caller == Client::Subject && request_method == :post
|
|
45
45
|
raise error(GeneratorLimitError, 'Recurring generator limit for account reached') if caller == Client::Generator
|
|
@@ -54,11 +54,11 @@ module Fakturoid
|
|
|
54
54
|
raise error(ClientError, 'Client error') if status_code >= 400
|
|
55
55
|
end
|
|
56
56
|
end
|
|
57
|
-
|
|
57
|
+
|
|
58
58
|
def error(klass, message = nil)
|
|
59
59
|
klass.new message, status_code, body
|
|
60
60
|
end
|
|
61
|
-
|
|
61
|
+
|
|
62
62
|
def method_missing(method, *args, &block)
|
|
63
63
|
if body_has_key?(method)
|
|
64
64
|
body[method.to_s]
|
|
@@ -66,11 +66,11 @@ module Fakturoid
|
|
|
66
66
|
super
|
|
67
67
|
end
|
|
68
68
|
end
|
|
69
|
-
|
|
69
|
+
|
|
70
70
|
def respond_to_missing?(method, _include_all)
|
|
71
71
|
body_has_key?(method) || super
|
|
72
72
|
end
|
|
73
|
-
|
|
73
|
+
|
|
74
74
|
def body_has_key?(key)
|
|
75
75
|
body && body.is_a?(Hash) && body.key?(key.to_s)
|
|
76
76
|
end
|
data/lib/fakturoid/version.rb
CHANGED
data/test/api_test.rb
CHANGED
|
@@ -6,14 +6,14 @@ class Fakturoid::ApiTest < Fakturoid::TestCase
|
|
|
6
6
|
permitted_params = Fakturoid::Api.permit_params(hash, :page, :number, :status)
|
|
7
7
|
assert_equal({ page: 4, number: '2015-0015' }, permitted_params)
|
|
8
8
|
end
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
should 'raise argument error if id is in wrong format' do
|
|
11
11
|
assert_raises(ArgumentError) { Fakturoid::Api.validate_numerical_id(nil) }
|
|
12
12
|
assert_raises(ArgumentError) { Fakturoid::Api.validate_numerical_id('nil') }
|
|
13
13
|
assert Fakturoid::Api.validate_numerical_id(15)
|
|
14
14
|
assert Fakturoid::Api.validate_numerical_id('15')
|
|
15
15
|
end
|
|
16
|
-
|
|
16
|
+
|
|
17
17
|
should 'raise argument error if search query is not given' do
|
|
18
18
|
assert_raises(ArgumentError) { Fakturoid::Api.validate_search_query(nil) }
|
|
19
19
|
assert_raises(ArgumentError) { Fakturoid::Api.validate_search_query('') }
|
data/test/config_test.rb
CHANGED
|
@@ -8,13 +8,13 @@ class Fakturoid::ConfigTest < Fakturoid::TestCase
|
|
|
8
8
|
c.account = 'testaccount'
|
|
9
9
|
c.user_agent = 'My test app (test@email.cz)'
|
|
10
10
|
end
|
|
11
|
-
|
|
11
|
+
|
|
12
12
|
assert_equal 'test@email.cz', config.email
|
|
13
13
|
assert_equal 'XXXXXXXXXXX', config.api_key
|
|
14
14
|
assert_equal 'testaccount', config.account
|
|
15
15
|
assert_equal 'My test app (test@email.cz)', config.user_agent
|
|
16
16
|
end
|
|
17
|
-
|
|
17
|
+
|
|
18
18
|
should 'use default user agent' do
|
|
19
19
|
config = Fakturoid::Config.new do |c|
|
|
20
20
|
c.email = 'test@email.cz'
|
|
@@ -24,14 +24,14 @@ class Fakturoid::ConfigTest < Fakturoid::TestCase
|
|
|
24
24
|
|
|
25
25
|
assert_equal 'Fakturoid ruby gem (test@email.cz)', config.user_agent
|
|
26
26
|
end
|
|
27
|
-
|
|
27
|
+
|
|
28
28
|
should 'return correct endpoints' do
|
|
29
29
|
config = Fakturoid::Config.new do |c|
|
|
30
30
|
c.email = 'test@email.cz'
|
|
31
31
|
c.api_key = 'XXXXXXXXXXX'
|
|
32
32
|
c.account = 'testaccount'
|
|
33
33
|
end
|
|
34
|
-
|
|
34
|
+
|
|
35
35
|
assert_equal 'https://app.fakturoid.cz/api/v2/accounts/testaccount', config.endpoint
|
|
36
36
|
assert_equal 'https://app.fakturoid.cz/api/v2', config.endpoint_without_account
|
|
37
37
|
end
|
data/test/fixtures/invoice.json
CHANGED
data/test/fixtures/subjects.json
CHANGED
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"vat_no": "CZ47123737",
|
|
39
39
|
"bank_account": "",
|
|
40
40
|
"iban": "",
|
|
41
|
-
"variable_symbol": "1234567890"
|
|
41
|
+
"variable_symbol": "1234567890",
|
|
42
42
|
"full_name": "",
|
|
43
43
|
"email": "",
|
|
44
44
|
"email_copy": "",
|
|
@@ -49,4 +49,4 @@
|
|
|
49
49
|
"url": "https://app.fakturoid.cz/api/v2/accounts/applecorp/subjects/28.json",
|
|
50
50
|
"updated_at": "2012-06-02T09:34:47+02:00"
|
|
51
51
|
}
|
|
52
|
-
]
|
|
52
|
+
]
|
data/test/request_test.rb
CHANGED
|
@@ -10,7 +10,7 @@ class Fakturoid::RequestTest < Fakturoid::TestCase
|
|
|
10
10
|
builder.headers = { content_type: 'application/pdf' }
|
|
11
11
|
end
|
|
12
12
|
Fakturoid::Request.any_instance.stubs(:connection).returns(test_connection)
|
|
13
|
-
|
|
13
|
+
|
|
14
14
|
response = Fakturoid::Request.new(:get, 'invoices/5/download.pdf', Fakturoid::Client::Invoice).call
|
|
15
15
|
assert !response.json?
|
|
16
16
|
assert_raises(NoMethodError) { response.name }
|
data/test/response_test.rb
CHANGED
|
@@ -9,17 +9,17 @@ class Fakturoid::ResponseTest < Fakturoid::TestCase
|
|
|
9
9
|
end
|
|
10
10
|
builder.headers = { content_type: 'application/json', user_agent: 'Fakturoid gem (email@testmail.cz)' }
|
|
11
11
|
end
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
response = Fakturoid::Response.new(test_connection.get('invoices/5.json'), Fakturoid::Client::Invoice, :get)
|
|
14
14
|
assert response.json?
|
|
15
|
-
assert_equal 200, response.status_code
|
|
15
|
+
assert_equal 200, response.status_code
|
|
16
16
|
assert_equal 5, response.id
|
|
17
17
|
assert_equal 5, response.body['id']
|
|
18
18
|
assert response.respond_to?(:body)
|
|
19
19
|
assert response.respond_to?(:id)
|
|
20
20
|
assert_raises(NoMethodError) { response.name }
|
|
21
21
|
end
|
|
22
|
-
|
|
22
|
+
|
|
23
23
|
context 'Exceptions' do
|
|
24
24
|
should 'raise user agent error' do
|
|
25
25
|
test_connection = Faraday.new do |builder|
|
|
@@ -28,10 +28,10 @@ class Fakturoid::ResponseTest < Fakturoid::TestCase
|
|
|
28
28
|
end
|
|
29
29
|
builder.headers = { content_type: 'application/json', user_agent: '' }
|
|
30
30
|
end
|
|
31
|
-
|
|
31
|
+
|
|
32
32
|
assert_raises(Fakturoid::UserAgentError) { Fakturoid::Response.new(test_connection.get('invoices/5.json'), Fakturoid::Client::Invoice, :get) }
|
|
33
33
|
end
|
|
34
|
-
|
|
34
|
+
|
|
35
35
|
should 'raise pagination error' do
|
|
36
36
|
test_connection = Faraday.new do |builder|
|
|
37
37
|
builder.adapter :test do |stub|
|
|
@@ -39,10 +39,10 @@ class Fakturoid::ResponseTest < Fakturoid::TestCase
|
|
|
39
39
|
end
|
|
40
40
|
builder.headers = { content_type: 'application/json', user_agent: 'Fakturoid gem (email@testmail.cz)' }
|
|
41
41
|
end
|
|
42
|
-
|
|
42
|
+
|
|
43
43
|
assert_raises(Fakturoid::PaginationError) { Fakturoid::Response.new(test_connection.get('invoices.json?page=4'), Fakturoid::Client::Invoice, :get) }
|
|
44
44
|
end
|
|
45
|
-
|
|
45
|
+
|
|
46
46
|
should 'raise authentication error' do
|
|
47
47
|
test_connection = Faraday.new do |builder|
|
|
48
48
|
builder.adapter :test do |stub|
|
|
@@ -50,10 +50,10 @@ class Fakturoid::ResponseTest < Fakturoid::TestCase
|
|
|
50
50
|
end
|
|
51
51
|
builder.headers = { content_type: 'application/json', user_agent: 'Fakturoid gem (email@testmail.cz)' }
|
|
52
52
|
end
|
|
53
|
-
|
|
53
|
+
|
|
54
54
|
assert_raises(Fakturoid::AuthenticationError) { Fakturoid::Response.new(test_connection.get('invoices.json?page=4'), Fakturoid::Client::Invoice, :get) }
|
|
55
55
|
end
|
|
56
|
-
|
|
56
|
+
|
|
57
57
|
should 'raise blocked account error' do
|
|
58
58
|
json = load_fixture('blocked_account.json')
|
|
59
59
|
test_connection = Faraday.new do |builder|
|
|
@@ -62,7 +62,7 @@ class Fakturoid::ResponseTest < Fakturoid::TestCase
|
|
|
62
62
|
end
|
|
63
63
|
builder.headers = { content_type: 'application/json', user_agent: 'Fakturoid gem (email@testmail.cz)' }
|
|
64
64
|
end
|
|
65
|
-
|
|
65
|
+
|
|
66
66
|
begin
|
|
67
67
|
Fakturoid::Response.new(test_connection.get('account.json'), Fakturoid::Client::Account, :get)
|
|
68
68
|
rescue Fakturoid::BlockedAccountError => e
|
|
@@ -74,7 +74,7 @@ class Fakturoid::ResponseTest < Fakturoid::TestCase
|
|
|
74
74
|
assert false, 'Exception was expected'
|
|
75
75
|
end
|
|
76
76
|
end
|
|
77
|
-
|
|
77
|
+
|
|
78
78
|
should 'raise destroy subject error' do
|
|
79
79
|
test_connection = Faraday.new do |builder|
|
|
80
80
|
builder.adapter :test do |stub|
|
|
@@ -82,10 +82,10 @@ class Fakturoid::ResponseTest < Fakturoid::TestCase
|
|
|
82
82
|
end
|
|
83
83
|
builder.headers = { content_type: 'application/json', user_agent: 'Fakturoid gem (email@testmail.cz)' }
|
|
84
84
|
end
|
|
85
|
-
|
|
85
|
+
|
|
86
86
|
assert_raises(Fakturoid::DestroySubjectError) { Fakturoid::Response.new(test_connection.delete('subjects/5.json'), Fakturoid::Client::Subject, :delete) }
|
|
87
87
|
end
|
|
88
|
-
|
|
88
|
+
|
|
89
89
|
should 'raise subject limit error' do
|
|
90
90
|
test_connection = Faraday.new do |builder|
|
|
91
91
|
builder.adapter :test do |stub|
|
|
@@ -93,10 +93,10 @@ class Fakturoid::ResponseTest < Fakturoid::TestCase
|
|
|
93
93
|
end
|
|
94
94
|
builder.headers = { content_type: 'application/json', user_agent: 'Fakturoid gem (email@testmail.cz)' }
|
|
95
95
|
end
|
|
96
|
-
|
|
96
|
+
|
|
97
97
|
assert_raises(Fakturoid::SubjectLimitError) { Fakturoid::Response.new(test_connection.post('subjects.json', name: 'Customer s.r.o.'), Fakturoid::Client::Subject, :post) }
|
|
98
98
|
end
|
|
99
|
-
|
|
99
|
+
|
|
100
100
|
should 'raise generator limit error' do
|
|
101
101
|
test_connection = Faraday.new do |builder|
|
|
102
102
|
builder.adapter :test do |stub|
|
|
@@ -104,10 +104,10 @@ class Fakturoid::ResponseTest < Fakturoid::TestCase
|
|
|
104
104
|
end
|
|
105
105
|
builder.headers = { content_type: 'application/json', user_agent: 'Fakturoid gem (email@testmail.cz)' }
|
|
106
106
|
end
|
|
107
|
-
|
|
107
|
+
|
|
108
108
|
assert_raises(Fakturoid::GeneratorLimitError) { Fakturoid::Response.new(test_connection.post('generators.json', name: 'Customer s.r.o.', recurring: true), Fakturoid::Client::Generator, :post) }
|
|
109
109
|
end
|
|
110
|
-
|
|
110
|
+
|
|
111
111
|
should 'raise unsupported feature error' do
|
|
112
112
|
test_connection = Faraday.new do |builder|
|
|
113
113
|
builder.adapter :test do |stub|
|
|
@@ -115,10 +115,10 @@ class Fakturoid::ResponseTest < Fakturoid::TestCase
|
|
|
115
115
|
end
|
|
116
116
|
builder.headers = { content_type: 'application/json', user_agent: 'Fakturoid gem (email@testmail.cz)' }
|
|
117
117
|
end
|
|
118
|
-
|
|
118
|
+
|
|
119
119
|
assert_raises(Fakturoid::UnsupportedFeatureError) { Fakturoid::Response.new(test_connection.post('invoices/5/message.json', email: 'customer@email.cz'), Fakturoid::Client::Invoice, :post) }
|
|
120
120
|
end
|
|
121
|
-
|
|
121
|
+
|
|
122
122
|
should 'raise record not found error' do
|
|
123
123
|
test_connection = Faraday.new do |builder|
|
|
124
124
|
builder.adapter :test do |stub|
|
|
@@ -126,10 +126,10 @@ class Fakturoid::ResponseTest < Fakturoid::TestCase
|
|
|
126
126
|
end
|
|
127
127
|
builder.headers = { content_type: 'application/json', user_agent: 'Fakturoid gem (email@testmail.cz)' }
|
|
128
128
|
end
|
|
129
|
-
|
|
129
|
+
|
|
130
130
|
assert_raises(Fakturoid::RecordNotFoundError) { Fakturoid::Response.new(test_connection.get('invoices/10.json'), Fakturoid::Client::Invoice, :get) }
|
|
131
131
|
end
|
|
132
|
-
|
|
132
|
+
|
|
133
133
|
should 'raise content type error' do
|
|
134
134
|
test_connection = Faraday.new do |builder|
|
|
135
135
|
builder.adapter :test do |stub|
|
|
@@ -137,10 +137,10 @@ class Fakturoid::ResponseTest < Fakturoid::TestCase
|
|
|
137
137
|
end
|
|
138
138
|
builder.headers = { content_type: 'application/xml', user_agent: 'Fakturoid gem (email@testmail.cz)' }
|
|
139
139
|
end
|
|
140
|
-
|
|
140
|
+
|
|
141
141
|
assert_raises(Fakturoid::ContentTypeError) { Fakturoid::Response.new(test_connection.get('invoices/5.xml'), Fakturoid::Client::Invoice, :get) }
|
|
142
142
|
end
|
|
143
|
-
|
|
143
|
+
|
|
144
144
|
should 'raise invalid record error' do
|
|
145
145
|
json = load_fixture('invoice_error.json')
|
|
146
146
|
test_connection = Faraday.new do |builder|
|
|
@@ -149,7 +149,7 @@ class Fakturoid::ResponseTest < Fakturoid::TestCase
|
|
|
149
149
|
end
|
|
150
150
|
builder.headers = { content_type: 'application/json', user_agent: 'Fakturoid gem (email@testmail.cz)' }
|
|
151
151
|
end
|
|
152
|
-
|
|
152
|
+
|
|
153
153
|
begin
|
|
154
154
|
Fakturoid::Response.new(test_connection.patch('invoice/5.json'), Fakturoid::Client::Invoice, :patch)
|
|
155
155
|
rescue Fakturoid::InvalidRecordError => e
|
|
@@ -161,7 +161,7 @@ class Fakturoid::ResponseTest < Fakturoid::TestCase
|
|
|
161
161
|
assert false, 'Exception was expected'
|
|
162
162
|
end
|
|
163
163
|
end
|
|
164
|
-
|
|
164
|
+
|
|
165
165
|
should 'raise rate limit error' do
|
|
166
166
|
test_connection = Faraday.new do |builder|
|
|
167
167
|
builder.adapter :test do |stub|
|
|
@@ -169,10 +169,10 @@ class Fakturoid::ResponseTest < Fakturoid::TestCase
|
|
|
169
169
|
end
|
|
170
170
|
builder.headers = { content_type: 'application/json', user_agent: 'Fakturoid gem (email@testmail.cz)' }
|
|
171
171
|
end
|
|
172
|
-
|
|
172
|
+
|
|
173
173
|
assert_raises(Fakturoid::RateLimitError) { Fakturoid::Response.new(test_connection.get('invoices/5.json'), Fakturoid::Client::Invoice, :get) }
|
|
174
174
|
end
|
|
175
|
-
|
|
175
|
+
|
|
176
176
|
should 'raise read only site error' do
|
|
177
177
|
test_connection = Faraday.new do |builder|
|
|
178
178
|
builder.adapter :test do |stub|
|
|
@@ -180,7 +180,7 @@ class Fakturoid::ResponseTest < Fakturoid::TestCase
|
|
|
180
180
|
end
|
|
181
181
|
builder.headers = { content_type: 'application/json', user_agent: 'Fakturoid gem (email@testmail.cz)' }
|
|
182
182
|
end
|
|
183
|
-
|
|
183
|
+
|
|
184
184
|
assert_raises(Fakturoid::ReadOnlySiteError) { Fakturoid::Response.new(test_connection.delete('invoices/5.json'), Fakturoid::Client::Invoice, :delete) }
|
|
185
185
|
end
|
|
186
186
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: fakturoid
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Eda Riedl
|
|
@@ -9,7 +9,7 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date:
|
|
12
|
+
date: 2017-07-03 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: multi_json
|
|
@@ -189,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
189
189
|
version: '0'
|
|
190
190
|
requirements: []
|
|
191
191
|
rubyforge_project:
|
|
192
|
-
rubygems_version: 2.
|
|
192
|
+
rubygems_version: 2.6.12
|
|
193
193
|
signing_key:
|
|
194
194
|
specification_version: 4
|
|
195
195
|
summary: Ruby client for web based invoicing service www.fakturoid.cz
|