Module: Fortnox

Extended by:
RestEasy
Defined in:
lib/fortnox/rails.rb,
lib/fortnox.rb,
lib/fortnox/types.rb,
lib/fortnox/struct.rb,
lib/fortnox/version.rb,
lib/fortnox/resource.rb,
lib/fortnox/collection.rb,
lib/fortnox/mappers/date.rb,
lib/fortnox/serialisation.rb,
lib/fortnox/attribute_keys.rb,
lib/fortnox/mappers/struct.rb,
lib/fortnox/resources/unit.rb,
lib/fortnox/resources/label.rb,
lib/fortnox/resources/order.rb,
lib/fortnox/auth/thread_local.rb,
lib/fortnox/error_translation.rb,
lib/fortnox/mappers/order_row.rb,
lib/fortnox/resources/article.rb,
lib/fortnox/resources/invoice.rb,
lib/fortnox/resources/project.rb,
lib/fortnox/structs/order_row.rb,
lib/fortnox/resources/customer.rb,
lib/fortnox/resources/document.rb,
lib/fortnox/mappers/invoice_row.rb,
lib/fortnox/structs/invoice_row.rb,
lib/fortnox/mappers/country_code.rb,
lib/fortnox/mappers/document_row.rb,
lib/fortnox/mappers/struct_array.rb,
lib/fortnox/structs/document_row.rb,
lib/fortnox/mappers/edi_information.rb,
lib/fortnox/structs/edi_information.rb,
lib/fortnox/mappers/label_references.rb,
lib/fortnox/mappers/email_information.rb,
lib/fortnox/structs/default_templates.rb,
lib/fortnox/structs/email_information.rb,
lib/fortnox/resources/terms_of_payment.rb,
lib/fortnox/structs/default_delivery_types.rb

Overview

Optional Rails integration. Not loaded with the rest of the gem — require it explicitly, e.g. from config/initializers/fortnox.rb:

require 'fortnox/rails'

Rails serialises nested objects by calling as_json on them. Anything that doesn't define it falls through to Object#as_json, which dumps instance variables — so without this, render json: { invoices: [invoice] } puts the gem's internals (api_data, model_attributes, changes, meta) in the response body. to_json needs no help here; it is correct without Rails.

Defined Under Namespace

Modules: AttributeKeys, Auth, ErrorTranslation, Mappers, RailsSerialisation, Serialisation, Structs, Types Classes: Article, AttributeError, Collection, ConstraintError, Customer, Document, Error, Invoice, Label, MissingAccessToken, MissingAttributeError, Order, Project, RequestError, Resource, Struct, TermsOfPayment, Unit, UnknownAttributeError

Constant Summary collapse

OAUTH_TOKEN_URL =
'https://apps.fortnox.se/oauth-v1/token'
ALLOWED_CHARACTERS_REGEXP =

Character set allowed in Fortnox text fields, per the official Fortnox docs. Useful for pre-validating strings before sending them to the API. See: https://www.fortnox.se/developer/guides-and-good-to-know/formats-and-encoding (The docs spell Unicode codepoints as \xNNNN; Ruby's regex parser uses \uNNNN for the same thing, so the three codepoints below are translated.)

%r{\A[\p{L}’\\\u{0308}\u{030a}a-zåäöéáœæøüA-ZÅÄÖÉÁÜŒÆØ0-9 –:.`´,;\^¤#%§£$€¢¥©™°&/()=+\-*_!?²³®½@\u{00a0}\n\r]*\z}
VERSION =
'1.0.0.rc14'

Class Method Summary collapse

Class Method Details

.access_tokenObject



127
128
129
# File 'lib/fortnox.rb', line 127

def access_token
  Thread.current[Auth::ThreadLocal::THREAD_LOCAL_KEY]
end

.access_token=(token) ⇒ Object



123
124
125
# File 'lib/fortnox.rb', line 123

def access_token=(token)
  Thread.current[Auth::ThreadLocal::THREAD_LOCAL_KEY] = token
end

.request_access_token(client_id:, client_secret:, tenant_id:, scopes: nil) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/fortnox.rb', line 131

def request_access_token(client_id:, client_secret:, tenant_id:, scopes: nil)
  response = token_request(client_id, client_secret, tenant_id, scopes)
  parsed = JSON.parse(response.body)

  unless response.success?
    error = parsed['error_description'] || parsed['error'] || response.body
    raise RequestError, "Token request failed (#{response.status}): #{error}"
  end

  parsed['access_token']
end

.scopesObject



143
144
145
146
147
148
149
150
# File 'lib/fortnox.rb', line 143

def scopes
  @loader.eager_load
  Resource.registered_resources
          .group_by(&:scope)
          .reject { |scope, _| scope.nil? }
          .sort
          .to_h
end