Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds basic functionality for managing attributes with several options #121

Merged
merged 8 commits into from
Feb 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
env:
global:
- CC_TEST_REPORTER_ID=397c19af63ca3467cff6fd640b0460b691f687118332b5989f530d28f82adcb8
- CC_TEST_REPORTER_ID=787a2f89b15c637323c7340d65ec17e898ac44480706b4b4122ea040c2a88f1d
language: ruby
rvm:
- "2.3.0"
- "2.3.1"
Expand Down
2 changes: 1 addition & 1 deletion fortnox-api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
spec.add_dependency 'httparty', '~> 0.14.0'

spec.add_development_dependency 'bundler', '~> 1.9'
spec.add_development_dependency 'codeclimate-test-reporter', '~> 0'
spec.add_development_dependency 'guard', '~> 2.12'
spec.add_development_dependency 'guard-rspec', '~> 4.5'
spec.add_development_dependency 'pry', '~> 0'
Expand All @@ -42,6 +41,7 @@ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
spec.add_development_dependency 'rspec-collection_matchers', '~> 0'
spec.add_development_dependency 'rubocop', '~> 0.52.0'
spec.add_development_dependency 'rubocop-rspec', '~> 1.22.2'
spec.add_development_dependency 'simplecov', '~> 0.15'
spec.add_development_dependency 'vcr', '~> 3.0'
spec.add_development_dependency 'webmock', '~> 1.21'
end
14 changes: 7 additions & 7 deletions lib/fortnox/api/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Article < Fortnox::API::Model::Base
STUB = { description: '' }.freeze

# Url Direct URL to the record
attribute :url, Types::Nullable::String.with(read_only: true)
attribute :url, Types::Nullable::String.is(:read_only)

# Active If the article is active
attribute :active, Types::Nullable::Boolean
Expand All @@ -30,10 +30,10 @@ class Article < Fortnox::API::Model::Base
attribute :depth, Types::Sized::Integer[0, 99_999_999]

# Description The description of the article
attribute :description, Types::Sized::String[200].with(required: true)
attribute :description, Types::Sized::String[200].is(:required)

# DisposableQuantity Disposable quantity of the article.
attribute :disposable_quantity, Types::Nullable::Float.with(read_only: true)
attribute :disposable_quantity, Types::Nullable::Float.is(:read_only)

# EAN EAN bar code
attribute :ean, Types::Sized::String[30]
Expand Down Expand Up @@ -79,14 +79,14 @@ class Article < Fortnox::API::Model::Base
attribute :quantity_in_stock, Types::Sized::Float[0.0, 99_999_999_999_999.9]

# ReservedQuantity Reserved quantity of the article
attribute :reserved_quantity, Types::Nullable::Float.with(read_only: true)
attribute :reserved_quantity, Types::Nullable::Float.is(:read_only)

# SalesAccount Account number for the sales account in Sweden.
# The number must be of an existing account.
attribute :sales_account, Types::Sized::Integer[0, 9_999]

# SalesPrice Price of article for its default price list
attribute :sales_price, Types::Nullable::Float.with(read_only: true)
attribute :sales_price, Types::Nullable::Float.is(:read_only)

# StockGoods If the article is stock goods
attribute :stock_goods, Types::Nullable::Boolean
Expand All @@ -95,13 +95,13 @@ class Article < Fortnox::API::Model::Base
attribute :stock_place, Types::Sized::String[100]

# StockValue Value in stock of the article
attribute :stock_value, Types::Nullable::Float.with(read_only: true)
attribute :stock_value, Types::Nullable::Float.is(:read_only)

# StockWarning When to start warning for low quantity in stock
attribute :stock_warning, Types::Sized::Float[0.0, 99_999_999_999_999.9]

# SupplierName Name of the supplier
attribute :supplier_name, Types::Nullable::String.with(read_only: true)
attribute :supplier_name, Types::Nullable::String.is(:read_only)

# SupplierNumber Supplier number for the article.
# The number must be of an existing supplier.
Expand Down
11 changes: 11 additions & 0 deletions lib/fortnox/api/models/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ def self.stub
new(self::STUB.dup)
end

# This filtering logic could be improved since it is currently O(N*M).
def attributes(*options)
return self.class.schema if options.nil?

options = Array(options)

self.class.schema.find_all do |_name, attribute|
options.all? { |option| attribute.is?(option) }
end
end

def unique_id
send(self.class::UNIQUE_ID)
end
Expand Down
12 changes: 6 additions & 6 deletions lib/fortnox/api/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class Customer < Fortnox::API::Model::Base
# :hire
# )

# Url Direct URL to the record
attribute :url, Types::Nullable::String.with(read_only: true)
# Url Direct URL to the record
attribute :url, Types::Nullable::String.is(:read_only)

# Address1 First address of the customer. 1024 characters
attribute :address1, Types::Sized::String[1024]
Expand All @@ -40,7 +40,7 @@ class Customer < Fortnox::API::Model::Base
attribute :city, Types::Sized::String[1024]

# Country Country of the customer. Read-only.
attribute :country, Types::Nullable::String.with(read_only: true)
attribute :country, Types::Nullable::String.is(:read_only)

# Comments Comments. 1024 characters.
attribute :comments, Types::Sized::String[1024]
Expand Down Expand Up @@ -73,7 +73,7 @@ class Customer < Fortnox::API::Model::Base
attribute :delivery_city, Types::Sized::String[1024]

# DeliveryCountry Delivery country of the customer. Read-only.
attribute :delivery_country, Types::Nullable::String.with(read_only: true)
attribute :delivery_country, Types::Nullable::String.is(:read_only)

# DeliveryCountryCode Delivery country code of the customer, 2 letters
attribute :delivery_country_code, Types::CountryCode
Expand Down Expand Up @@ -140,7 +140,7 @@ class Customer < Fortnox::API::Model::Base
attribute :invoice_remark, Types::Sized::String[1024]

# Name Name of the customer, 1024 characters
attribute :name, Types::Sized::String[1024].with(required: true)
attribute :name, Types::Sized::String[1024].is(:required)

# OrganisationNumber Organisation number of the customer. 30 characters
attribute :organisation_number, Types::Sized::String[30]
Expand Down Expand Up @@ -188,7 +188,7 @@ class Customer < Fortnox::API::Model::Base
attribute :visiting_city, Types::Sized::String[128]

# VisitCountry Visit country of the customer, read-only
attribute :visiting_country, Types::Nullable::String.with(read_only: true)
attribute :visiting_country, Types::Nullable::String.is(:read_only)

# VisitingCountryCode Code of the visiting country for the customer, 2 letters
attribute :visiting_country_code, Types::CountryCode
Expand Down
34 changes: 17 additions & 17 deletions lib/fortnox/api/models/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ module Model
# This is never used standalone, only included in the respective models.
class Document < Fortnox::API::Model::Base
# Url Direct url to the record.
attribute :url, Types::Nullable::String.with(read_only: true)
attribute :url, Types::Nullable::String.is(:read_only)

# AdministrationFee The document administration fee.
attribute :administration_fee, Types::Nullable::Float

# AdministrationFeeVAT VAT of the document administration fee.
attribute :administration_fee_vat, Types::Nullable::Float.with(read_only: true)
attribute :administration_fee_vat, Types::Nullable::Float.is(:read_only)

# Address1 Document address 1. 1024 characters
attribute :address1, Types::Sized::String[1024]
Expand All @@ -26,10 +26,10 @@ class Document < Fortnox::API::Model::Base
attribute :address2, Types::Sized::String[1024]

# BasisTaxReduction Basis of tax reduction.
attribute :basis_tax_reduction, Types::Nullable::Float.with(read_only: true)
attribute :basis_tax_reduction, Types::Nullable::Float.is(:read_only)

# Cancelled If the document is cancelled.
attribute :cancelled, Types::Nullable::Boolean.with(read_only: true)
attribute :cancelled, Types::Nullable::Boolean.is(:read_only)

# City City for the document address.
attribute :city, Types::Sized::String[1024]
Expand All @@ -38,10 +38,10 @@ class Document < Fortnox::API::Model::Base
attribute :comments, Types::Sized::String[1024]

# ContributionPercent Document contribution in percent.
attribute :contribution_percent, Types::Nullable::Float.with(read_only: true)
attribute :contribution_percent, Types::Nullable::Float.is(:read_only)

# ContributionValue Document contribution in amount.
attribute :contribution_value, Types::Nullable::Float.with(read_only: true)
attribute :contribution_value, Types::Nullable::Float.is(:read_only)

# Country Country for the document address.
attribute :country, Types::CountryCode
Expand Down Expand Up @@ -101,28 +101,28 @@ class Document < Fortnox::API::Model::Base
attribute :freight, Types::Sized::Float[0.0, 99_999_999_999.9]

# FreightVAT VAT of the freight cost.
attribute :freight_vat, Types::Nullable::Float.with(read_only: true)
attribute :freight_vat, Types::Nullable::Float.is(:read_only)

# Gross Gross value of the document
attribute :gross, Types::Nullable::Float.with(read_only: true)
attribute :gross, Types::Nullable::Float.is(:read_only)

# HouseWork If there is any row of the document marked "house work".
attribute :house_work, Types::Nullable::Boolean.with(read_only: true)
attribute :house_work, Types::Nullable::Boolean.is(:read_only)

attribute :labels, Types::Strict::Array.member(Label)

# Net Net amount
attribute :net, Types::Nullable::Float.with(read_only: true)
attribute :net, Types::Nullable::Float.is(:read_only)

# NotCompleted If the document is set as not completed.
attribute :not_completed, Types::Nullable::Boolean

# OfferReference Reference to the offer, if one exists.
attribute :offer_reference, Types::Nullable::Integer.with(read_only: true)
attribute :offer_reference, Types::Nullable::Integer.is(:read_only)

# OrganisationNumber Organisation number of the customer for the
# document.
attribute :organisation_number, Types::Nullable::String.with(read_only: true)
attribute :organisation_number, Types::Nullable::String.is(:read_only)

# OurReference Our reference. 50 characters
attribute :our_reference, Types::Sized::String[50]
Expand All @@ -146,13 +146,13 @@ class Document < Fortnox::API::Model::Base
attribute :remarks, Types::Sized::String[1024]

# RoundOff Round off amount for the document.
attribute :round_off, Types::Nullable::Float.with(read_only: true)
attribute :round_off, Types::Nullable::Float.is(:read_only)

# Sent If the document is printed or sent in any way.
attribute :sent, Types::Nullable::Boolean.with(read_only: true)
attribute :sent, Types::Nullable::Boolean.is(:read_only)

# TaxReduction The amount of tax reduction.
attribute :tax_reduction, Types::Nullable::Integer.with(read_only: true)
attribute :tax_reduction, Types::Nullable::Integer.is(:read_only)

# TermsOfDelivery Code of the terms of delivery.
attribute :terms_of_delivery, Types::Nullable::String
Expand All @@ -161,10 +161,10 @@ class Document < Fortnox::API::Model::Base
attribute :terms_of_payment, Types::Nullable::String

# Total The total amount of the document.
attribute :total, Types::Nullable::Float.with(read_only: true)
attribute :total, Types::Nullable::Float.is(:read_only)

# TotalVAT The total VAT amount of the document.
attribute :total_vat, Types::Nullable::Float.with(read_only: true)
attribute :total_vat, Types::Nullable::Float.is(:read_only)

# VATIncluded If the price of the document is including VAT.
attribute :vat_included, Types::Nullable::Boolean
Expand Down
28 changes: 14 additions & 14 deletions lib/fortnox/api/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ class Invoice < Document
STUB = { customer_number: '' }.freeze

# UrlTaxReductionList Direct url to the tax reduction for the invoice.
attribute :url_tax_reduction_list, Types::Nullable::String.with(read_only: true)
attribute :url_tax_reduction_list, Types::Nullable::String.is(:read_only)

# AccountingMethod Accounting Method.
attribute :accounting_method, Types::Nullable::String

# Balance Balance of the invoice.
attribute :balance, Types::Nullable::Float.with(read_only: true)
attribute :balance, Types::Nullable::Float.is(:read_only)

# Booked If the invoice is bookkept.
attribute :booked, Types::Nullable::Boolean.with(read_only: true)
attribute :booked, Types::Nullable::Boolean.is(:read_only)

# Credit If the invoice is a credit invoice.
attribute :credit, Types::Nullable::Boolean.with(read_only: true)
attribute :credit, Types::Nullable::Boolean.is(:read_only)

# CreditInvoiceReference Reference to the credit invoice, if one exits.
attribute :credit_invoice_reference, Types::Nullable::Integer

# ContractReference Reference to the contract, if one exists.
attribute :contract_reference, Types::Nullable::Integer.with(read_only: true)
attribute :contract_reference, Types::Nullable::Integer.is(:read_only)

# DueDate Due date of the invoice.
attribute :due_date, Types::Nullable::Date
Expand All @@ -44,10 +44,10 @@ class Invoice < Document
attribute :invoice_date, Types::Nullable::Date

# InvoicePeriodStart Start date of the invoice period.
attribute :invoice_period_start, Types::Nullable::Date.with(read_only: true)
attribute :invoice_period_start, Types::Nullable::Date.is(:read_only)

# InvoicePeriodEnd End date of the invoice period.
attribute :invoice_period_end, Types::Nullable::Date.with(read_only: true)
attribute :invoice_period_end, Types::Nullable::Date.is(:read_only)

# InvoiceRows Separate object
attribute :invoice_rows, Types::Strict::Array.member(Types::InvoiceRow)
Expand All @@ -59,28 +59,28 @@ class Invoice < Document
attribute :language, Types::Nullable::String

# LastRemindDate Date of last reminder.
attribute :last_remind_date, Types::Nullable::Date.with(read_only: true)
attribute :last_remind_date, Types::Nullable::Date.is(:read_only)

# NoxFinans If the invoice is managed by NoxFinans
attribute :nox_finans, Types::Nullable::Boolean.with(read_only: true)
attribute :nox_finans, Types::Nullable::Boolean.is(:read_only)

# OCR OCR number of the invoice.
attribute :ocr, Types::Nullable::String

# OrderReference Reference to the order, if one exists.
attribute :order_reference, Types::Nullable::Integer.with(read_only: true)
attribute :order_reference, Types::Nullable::Integer.is(:read_only)

# Reminders Number of reminders sent to the customer.
attribute :reminders, Types::Nullable::Integer.with(read_only: true)
attribute :reminders, Types::Nullable::Integer.is(:read_only)

# VoucherNumber Voucher number for the invoice.
attribute :voucher_number, Types::Nullable::Integer.with(read_only: true)
attribute :voucher_number, Types::Nullable::Integer.is(:read_only)

# VoucherSeries Voucher series for the invoice.
attribute :voucher_series, Types::Nullable::String.with(read_only: true)
attribute :voucher_series, Types::Nullable::String.is(:read_only)

# VoucherYear Voucher year for the invoice.
attribute :voucher_year, Types::Nullable::Integer.with(read_only: true)
attribute :voucher_year, Types::Nullable::Integer.is(:read_only)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/fortnox/api/models/label.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class Label < Model::Base
STUB = {}.freeze

# Id integer, read-only. The ID of the label.
attribute :id, Types::Required::Integer.with(read_only: true)
attribute :id, Types::Required::Integer.is(:read_only)

# Description string, 25 characters, required. Description of the label
attribute :description, Types::Sized::String[25].with(read_only: true)
attribute :description, Types::Sized::String[25].is(:read_only)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/fortnox/api/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Order < Document
attribute :copy_remarks, Types::Nullable::Boolean

# InvoiceReference Reference if an invoice is created from order
attribute :invoice_reference, Types::Nullable::Integer.with(read_only: true)
attribute :invoice_reference, Types::Nullable::Integer.is(:read_only)

# OrderDate Date of order
attribute :order_date, Types::Nullable::Date
Expand Down
2 changes: 1 addition & 1 deletion lib/fortnox/api/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Project < Fortnox::API::Model::Base
STUB = { description: '' }.freeze

# Url Direct URL to the record
attribute :url, Types::Nullable::String.with(read_only: true)
attribute :url, Types::Nullable::String.is(:read_only)

# Comments Comments on project. 512 characters
attribute :comments, Types::Sized::String[512]
Expand Down
4 changes: 2 additions & 2 deletions lib/fortnox/api/models/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class Unit < Fortnox::API::Model::Base
STUB = { code: '' }.freeze

# Url Direct URL to the record
attribute :url, Types::Nullable::String.with(read_only: true)
attribute :url, Types::Nullable::String.is(:read_only)

# Comments Comments on project. 512 characters
attribute :code, Types::Required::String.with(read_only: true)
attribute :code, Types::Required::String.is(:read_only)

# ContactPerson ContactPerson for project. 50 characters
attribute :description, Types::Nullable::String
Expand Down
17 changes: 17 additions & 0 deletions lib/fortnox/api/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
require 'dry-struct'
require 'dry-types'

module Dry
module Types
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should definitely add test this override. We want to verify it's working as intended and we also want to see if it breaks when we update Dry::Types in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could make this a refinement instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a great suggestion! Then we only need to test that our Types is behaving as intended and it will prevent there modifications from leaking too :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out you can't refine modules, only classes. I tried a few workarounds but didn't get it to work at all with refinements :/
I'll probably open a PR against dry types and try to get this into the real repo. If not I'll ask @solnic to advice on how to do this refinement in a safe way ...

module Options
def is(*option_names)
new_options = option_names.each_with_object({}) do |name, hash|
hash[name] = true
end
with(new_options)
end

def is?(option_name)
@options[option_name]
end
end
end
end

module Fortnox
module API
module Types
Expand Down
Loading