Class: Jortt::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/jortt/client.rb,
lib/jortt/client/error.rb,
lib/jortt/client/version.rb,
lib/jortt/client/invoices.rb,
lib/jortt/client/customers.rb,
lib/jortt/client/ledger_accounts.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Customers, Error, Invoices, LedgerAccounts

Constant Summary collapse

SITE =
'https://api.jortt.nl'
OAUTH_PROVIDER_URL =
'https://app.jortt.nl/oauth-provider/oauth'
VERSION =
'6.1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, secret, opts = {}) ⇒ Jortt::Client

Initialize a Jortt client.

Examples:

Jortt::Client.new(
  "my-client-id",
  "my-client-secret"
)

Since:

  • 1.0.0



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jortt/client.rb', line 32

def initialize(id, secret, opts = {})
  oauth_provider_url = opts[:oauth_provider_url] || OAUTH_PROVIDER_URL

  client = OAuth2::Client.new(id, secret,
    site: opts[:site] || SITE,
    token_url: "#{oauth_provider_url}/token",
    authorize_url: "#{oauth_provider_url}/authorize",
    auth_scheme: :basic_auth
  )

  @token = client.client_credentials.get_token(scope: "invoices:read invoices:write customers:read customers:write")
end

Instance Attribute Details

#tokenObject

Returns the value of attribute token.



16
17
18
# File 'lib/jortt/client.rb', line 16

def token
  @token
end

Instance Method Details

#customersJortt::Client::Customers

Access the customer resource to perform operations.

Examples:

client.customers

Returns:

Since:

  • 1.0.0



53
54
55
# File 'lib/jortt/client.rb', line 53

def customers
  @customers ||= Jortt::Client::Customers.new(self)
end

#delete(path) ⇒ Object



93
94
95
# File 'lib/jortt/client.rb', line 93

def delete(path)
  handle_response { token.delete(path) }
end

#get(path, params = {}) ⇒ Object



81
82
83
# File 'lib/jortt/client.rb', line 81

def get(path, params = {})
  handle_response { token.get(path, params: params) }
end

#handle_response(&block) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/jortt/client.rb', line 97

def handle_response(&block)
  response = yield
  return true if response.status == 204
  response.parsed.fetch('data')
rescue OAuth2::Error => e
  raise Error.from_response(e.response)
end

#invoicesJortt::Client::Invoices

Access the invoices resource to perform operations.

Examples:

client.invoices

Returns:

Since:

  • 1.0.0



65
66
67
# File 'lib/jortt/client.rb', line 65

def invoices
  @invoices ||= Jortt::Client::Invoices.new(self)
end

#ledger_accountsJortt::Client::LedgerAccounts

Access the ledger_accounts resource.

Examples:

client.ledger_accounts

Returns:

Since:

  • 5.0.0



77
78
79
# File 'lib/jortt/client.rb', line 77

def ledger_accounts
  Jortt::Client::LedgerAccounts.new(self)
end

#paginated(path, params = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/jortt/client.rb', line 105

def paginated(path, params = {})
  page = 1

  Enumerator.new do |yielder|
    loop do
      response = token.get(path, params: params.merge(page: page)).parsed
      response['data'].each { |item| yielder << item }
      break if response['_links']['next'].nil?
      page += 1
    end
  end
rescue OAuth2::Error => e
  raise Error.from_response(e.response)
end

#post(path, params = {}) ⇒ Object



85
86
87
# File 'lib/jortt/client.rb', line 85

def post(path, params = {})
  handle_response { token.post(path, params: params) }
end

#put(path, params = {}) ⇒ Object



89
90
91
# File 'lib/jortt/client.rb', line 89

def put(path, params = {})
  handle_response { token.put(path, params: params) }
end