fakturoid 0.3.0 → 0.4.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.
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Fakturoid
2
4
  module Client
3
5
  class Todo < Fakturoid::Api
4
6
  def self.all(params = {})
5
7
  request_params = permit_params(params, :page, :since) || {}
6
8
 
7
- get_request('todos.json', request_params: request_params)
9
+ get_request("todos.json", request_params: request_params)
8
10
  end
9
11
 
10
12
  def self.toggle_completion(id)
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Fakturoid
2
4
  module Client
3
5
  class User < Api
4
6
  def self.current
5
- get_request('user.json', url: Fakturoid::Api.config.endpoint_without_account)
7
+ get_request("user.json", url: Fakturoid::Api.config.endpoint_without_account)
6
8
  end
7
9
 
8
10
  def self.find(id)
@@ -11,7 +13,7 @@ module Fakturoid
11
13
  end
12
14
 
13
15
  def self.all
14
- get_request('users.json')
16
+ get_request("users.json")
15
17
  end
16
18
  end
17
19
  end
@@ -1,9 +1,12 @@
1
- require 'fakturoid/client/account'
2
- require 'fakturoid/client/bank_account'
3
- require 'fakturoid/client/user'
4
- require 'fakturoid/client/subject'
5
- require 'fakturoid/client/invoice'
6
- require 'fakturoid/client/expense'
7
- require 'fakturoid/client/generator'
8
- require 'fakturoid/client/event'
9
- require 'fakturoid/client/todo'
1
+ # frozen_string_literal: true
2
+
3
+ require "fakturoid/client/account"
4
+ require "fakturoid/client/bank_account"
5
+ require "fakturoid/client/number_format"
6
+ require "fakturoid/client/user"
7
+ require "fakturoid/client/subject"
8
+ require "fakturoid/client/invoice"
9
+ require "fakturoid/client/expense"
10
+ require "fakturoid/client/generator"
11
+ require "fakturoid/client/event"
12
+ require "fakturoid/client/todo"
@@ -1,16 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Fakturoid
2
4
  class Config
3
5
  attr_accessor :email, :api_key, :account
4
6
  attr_writer :user_agent
5
7
 
6
- ENDPOINT = 'https://app.fakturoid.cz/api/v2'
8
+ ENDPOINT = "https://app.fakturoid.cz/api/v2"
7
9
 
8
10
  def initialize(&_block)
9
11
  yield self
10
12
  end
11
13
 
12
14
  def user_agent
13
- if @user_agent.nil? || @user_agent.empty?
15
+ if !defined?(@user_agent) || @user_agent.nil? || @user_agent.empty?
14
16
  "Fakturoid ruby gem (#{email})"
15
17
  else
16
18
  @user_agent
@@ -24,5 +26,15 @@ module Fakturoid
24
26
  def endpoint_without_account
25
27
  ENDPOINT
26
28
  end
29
+
30
+ def faraday_v1?
31
+ major_faraday_version == "1"
32
+ end
33
+
34
+ private
35
+
36
+ def major_faraday_version
37
+ @major_faraday_version ||= Faraday::VERSION.split(".").first
38
+ end
27
39
  end
28
40
  end
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Fakturoid
2
4
  module Connection
5
+ DEFAULT_CONTENT_TYPE = "application/json"
3
6
 
4
7
  def default_options(options = {})
5
- content_type = options[:headers] && options[:headers][:content_type]
6
8
  {
7
9
  headers: {
8
- content_type: content_type || 'application/json',
10
+ content_type: options.dig(:headers, :content_type) || DEFAULT_CONTENT_TYPE,
9
11
  user_agent: Fakturoid::Api.config.user_agent
10
12
  },
11
13
  url: options[:url] || Fakturoid::Api.config.endpoint
@@ -14,7 +16,13 @@ module Fakturoid
14
16
 
15
17
  def connection(options = {})
16
18
  @connection = Faraday.new default_options(options)
17
- @connection.basic_auth(Fakturoid::Api.config.email, Fakturoid::Api.config.api_key)
19
+
20
+ # https://lostisland.github.io/faraday/middleware/authentication
21
+ if Fakturoid::Api.config.faraday_v1?
22
+ @connection.request :basic_auth, Fakturoid::Api.config.email, Fakturoid::Api.config.api_key
23
+ else
24
+ @connection.request :authorization, :basic, Fakturoid::Api.config.email, Fakturoid::Api.config.api_key
25
+ end
18
26
 
19
27
  @connection
20
28
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Fakturoid
2
4
  class Railtie < Rails::Railtie
3
5
  end
@@ -1,9 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Fakturoid
2
4
  class Request
3
5
  include Connection
4
6
 
5
7
  attr_reader :method, :path, :caller
6
- HTTP_METHODS = [:get, :post, :patch, :delete]
8
+
9
+ HTTP_METHODS = [:get, :post, :patch, :delete].freeze
7
10
 
8
11
  def initialize(method, path, caller)
9
12
  @method = method
@@ -19,7 +22,7 @@ module Fakturoid
19
22
  http_connection = connection(params)
20
23
  response = http_connection.send(method) do |req|
21
24
  req.url path, request_params
22
- req.headers['X-Client-Env'] = "Ruby #{RUBY_VERSION}"
25
+ req.headers["X-Client-Env"] = "Ruby #{RUBY_VERSION}"
23
26
  req.body = MultiJson.dump(params[:payload]) if params.key?(:payload)
24
27
  end
25
28
  Response.new(response, caller, method)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Fakturoid
2
4
  class Response
3
5
  attr_reader :response, :caller, :env, :body, :request_method
@@ -15,11 +17,11 @@ module Fakturoid
15
17
  end
16
18
 
17
19
  def status_code
18
- env['status']
20
+ env["status"]
19
21
  end
20
22
 
21
23
  def json?
22
- env.request_headers['Content-Type'] == 'application/json'
24
+ env.request_headers["Content-Type"] == "application/json"
23
25
  end
24
26
 
25
27
  def headers
@@ -35,23 +37,23 @@ module Fakturoid
35
37
  def handle_response
36
38
  case status_code
37
39
  when 400
38
- raise error(UserAgentError, 'User-Agent header missing') if env.request_headers['User-Agent'].nil? || env.request_headers['User-Agent'].empty?
39
- raise error(PaginationError, 'Page does not exist')
40
- when 401 then raise error(AuthenticationError, 'Authentification failed')
41
- when 402 then raise error(BlockedAccountError, 'Account is blocked')
42
- when 403 then
43
- raise error(DestroySubjectError, 'Cannot destroy subject with invoices') if caller == Client::Subject && request_method == :delete
44
- raise error(SubjectLimitError, 'Subject limit for account reached') if caller == Client::Subject && request_method == :post
45
- raise error(GeneratorLimitError, 'Recurring generator limit for account reached') if caller == Client::Generator
46
- raise error(UnsupportedFeatureError, 'Feature unavailable for account plan')
47
- when 404 then raise error(RecordNotFoundError, 'Record not found')
48
- when 415 then raise error(ContentTypeError, 'Unsupported Content-Type')
49
- when 422 then raise error(InvalidRecordError, 'Invalid record')
50
- when 429 then raise error(RateLimitError, 'Rate limit reached')
51
- when 503 then raise error(ReadOnlySiteError, 'Fakturoid is in read only state')
40
+ raise error(UserAgentError, "User-Agent header missing") if env.request_headers["User-Agent"].nil? || env.request_headers["User-Agent"].empty?
41
+ raise error(PaginationError, "Page does not exist")
42
+ when 401 then raise error(AuthenticationError, "Authentification failed")
43
+ when 402 then raise error(BlockedAccountError, "Account is blocked")
44
+ when 403
45
+ raise error(DestroySubjectError, "Cannot destroy subject with invoices") if caller == Client::Subject && request_method == :delete
46
+ raise error(SubjectLimitError, "Subject limit for account reached") if caller == Client::Subject && request_method == :post
47
+ raise error(GeneratorLimitError, "Recurring generator limit for account reached") if caller == Client::Generator
48
+ raise error(UnsupportedFeatureError, "Feature unavailable for account plan")
49
+ when 404 then raise error(RecordNotFoundError, "Record not found")
50
+ when 415 then raise error(ContentTypeError, "Unsupported Content-Type")
51
+ when 422 then raise error(InvalidRecordError, "Invalid record")
52
+ when 429 then raise error(RateLimitError, "Rate limit reached")
53
+ when 503 then raise error(ReadOnlySiteError, "Fakturoid is in read only state")
52
54
  else
53
- raise error(ServerError, 'Server error') if status_code >= 500
54
- raise error(ClientError, 'Client error') if status_code >= 400
55
+ raise error(ServerError, "Server error") if status_code >= 500
56
+ raise error(ClientError, "Client error") if status_code >= 400
55
57
  end
56
58
  end
57
59
 
@@ -72,7 +74,7 @@ module Fakturoid
72
74
  end
73
75
 
74
76
  def body_has_key?(key)
75
- body && body.is_a?(Hash) && body.key?(key.to_s)
77
+ body.is_a?(Hash) && body.key?(key.to_s)
76
78
  end
77
79
  end
78
80
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Fakturoid
2
- VERSION = '0.3.0'
4
+ VERSION = "0.4.0"
3
5
  end
data/lib/fakturoid.rb CHANGED
@@ -1,15 +1,17 @@
1
- require 'uri'
2
- require 'multi_json'
3
- require 'faraday'
4
-
5
- require 'fakturoid/config'
6
- require 'fakturoid/connection'
7
- require 'fakturoid/request'
8
- require 'fakturoid/response'
9
- require 'fakturoid/api'
10
- require 'fakturoid/client'
11
- require 'fakturoid/version'
12
- require 'fakturoid/railtie' if defined?(::Rails)
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+ require "multi_json"
5
+ require "faraday"
6
+
7
+ require "fakturoid/config"
8
+ require "fakturoid/connection"
9
+ require "fakturoid/request"
10
+ require "fakturoid/response"
11
+ require "fakturoid/api"
12
+ require "fakturoid/client"
13
+ require "fakturoid/version"
14
+ require "fakturoid/railtie" if defined?(::Rails)
13
15
 
14
16
  module Fakturoid
15
17
  class ApiError < StandardError
data/test/api_test.rb CHANGED
@@ -1,22 +1,24 @@
1
- require 'test_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
2
4
 
3
5
  class Fakturoid::ApiTest < Fakturoid::TestCase
4
- should 'permit only required arguments' do
5
- hash = { page: 4, number: '2015-0015', account: 15 }
6
+ should "permit only required arguments" do
7
+ hash = { page: 4, number: "2015-0015", account: 15 }
6
8
  permitted_params = Fakturoid::Api.permit_params(hash, :page, :number, :status)
7
- assert_equal({ page: 4, number: '2015-0015' }, permitted_params)
9
+ assert_equal({ page: 4, number: "2015-0015" }, permitted_params)
8
10
  end
9
11
 
10
- should 'raise argument error if id is in wrong format' do
12
+ should "raise argument error if id is in wrong format" do
11
13
  assert_raises(ArgumentError) { Fakturoid::Api.validate_numerical_id(nil) }
12
- assert_raises(ArgumentError) { Fakturoid::Api.validate_numerical_id('nil') }
14
+ assert_raises(ArgumentError) { Fakturoid::Api.validate_numerical_id("nil") }
13
15
  assert Fakturoid::Api.validate_numerical_id(15)
14
- assert Fakturoid::Api.validate_numerical_id('15')
16
+ assert Fakturoid::Api.validate_numerical_id("15")
15
17
  end
16
18
 
17
- should 'raise argument error if search query is not given' do
19
+ should "raise argument error if search query is not given" do
18
20
  assert_raises(ArgumentError) { Fakturoid::Api.validate_search_query(nil) }
19
- assert_raises(ArgumentError) { Fakturoid::Api.validate_search_query('') }
20
- assert Fakturoid::Api.validate_search_query('Company name')
21
+ assert_raises(ArgumentError) { Fakturoid::Api.validate_search_query("") }
22
+ assert Fakturoid::Api.validate_search_query("Company name")
21
23
  end
22
24
  end
data/test/config_test.rb CHANGED
@@ -1,38 +1,40 @@
1
- require 'test_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
2
4
 
3
5
  class Fakturoid::ConfigTest < Fakturoid::TestCase
4
- should 'configure with block param' do
6
+ should "configure with block param" do
5
7
  config = Fakturoid::Config.new do |c|
6
- c.email = 'test@email.cz'
7
- c.api_key = 'XXXXXXXXXXX'
8
- c.account = 'testaccount'
9
- c.user_agent = 'My test app (test@email.cz)'
8
+ c.email = "test@email.cz"
9
+ c.api_key = "XXXXXXXXXXX"
10
+ c.account = "testaccount"
11
+ c.user_agent = "My test app (test@email.cz)"
10
12
  end
11
13
 
12
- assert_equal 'test@email.cz', config.email
13
- assert_equal 'XXXXXXXXXXX', config.api_key
14
- assert_equal 'testaccount', config.account
15
- assert_equal 'My test app (test@email.cz)', config.user_agent
14
+ assert_equal "test@email.cz", config.email
15
+ assert_equal "XXXXXXXXXXX", config.api_key
16
+ assert_equal "testaccount", config.account
17
+ assert_equal "My test app (test@email.cz)", config.user_agent
16
18
  end
17
19
 
18
- should 'use default user agent' do
20
+ should "use default user agent" do
19
21
  config = Fakturoid::Config.new do |c|
20
- c.email = 'test@email.cz'
21
- c.api_key = 'XXXXXXXXXXX'
22
- c.account = 'testaccount'
22
+ c.email = "test@email.cz"
23
+ c.api_key = "XXXXXXXXXXX"
24
+ c.account = "testaccount"
23
25
  end
24
26
 
25
- assert_equal 'Fakturoid ruby gem (test@email.cz)', config.user_agent
27
+ assert_equal "Fakturoid ruby gem (test@email.cz)", config.user_agent
26
28
  end
27
29
 
28
- should 'return correct endpoints' do
30
+ should "return correct endpoints" do
29
31
  config = Fakturoid::Config.new do |c|
30
- c.email = 'test@email.cz'
31
- c.api_key = 'XXXXXXXXXXX'
32
- c.account = 'testaccount'
32
+ c.email = "test@email.cz"
33
+ c.api_key = "XXXXXXXXXXX"
34
+ c.account = "testaccount"
33
35
  end
34
36
 
35
- assert_equal 'https://app.fakturoid.cz/api/v2/accounts/testaccount', config.endpoint
36
- assert_equal 'https://app.fakturoid.cz/api/v2', config.endpoint_without_account
37
+ assert_equal "https://app.fakturoid.cz/api/v2/accounts/testaccount", config.endpoint
38
+ assert_equal "https://app.fakturoid.cz/api/v2", config.endpoint_without_account
37
39
  end
38
40
  end
data/test/request_test.rb CHANGED
@@ -1,17 +1,19 @@
1
- require 'test_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
2
4
 
3
5
  class Fakturoid::RequestTest < Fakturoid::TestCase
4
- should 'should return pdf' do
5
- pdf = load_fixture('invoice.pdf')
6
+ should "should return pdf" do
7
+ pdf = load_fixture("invoice.pdf")
6
8
  test_connection = Faraday.new do |builder|
7
9
  builder.adapter :test do |stub|
8
- stub.get('invoices/5/download.pdf') { |_env| [200, { content_type: 'application/pdf' }, pdf] }
10
+ stub.get("invoices/5/download.pdf") { |_env| [200, { content_type: "application/pdf" }, pdf] }
9
11
  end
10
- builder.headers = { content_type: 'application/pdf' }
12
+ builder.headers = { content_type: "application/pdf" }
11
13
  end
12
14
  Fakturoid::Request.any_instance.stubs(:connection).returns(test_connection)
13
15
 
14
- response = Fakturoid::Request.new(:get, 'invoices/5/download.pdf', Fakturoid::Client::Invoice).call
16
+ response = Fakturoid::Request.new(:get, "invoices/5/download.pdf", Fakturoid::Client::Invoice).call
15
17
  assert !response.json?
16
18
  assert_raises(NoMethodError) { response.name }
17
19
  end