Class: Moloni::BaseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/moloni/base_model.rb

Constant Summary collapse

ERROR_MESSAGES =
{
  1 => 'is required',
  2 => 'must be numeric',
  3 => 'must be a valid email',
  4 => 'must be unique',
  5 => 'must be one of accepted values',
  6 => 'must be a valid URL',
  7 => 'must be a valid postal code',
  8 => 'must be a valid Portuguese VAT number',
  9 => 'must be a date in YYYY-mm-dd format',
  10 => 'has an invalid document association',
  11 => 'cannot be sent to Tax Authority',
  12 => 'has an invalid date comparison',
  13 => 'must be a valid phone contact',
  14 => 'has invalid tax configuration',
  15 => 'has more than one VAT',
  16 => 'customer identification does not meet legal requirements',
  17 => 'field limit reached'
}.freeze
COMMON_ACRONYMS =
%w[ean sku nif iban].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.intermediate_pathObject

Returns the value of attribute intermediate_path.



13
14
15
# File 'lib/moloni/base_model.rb', line 13

def intermediate_path
  @intermediate_path
end

Class Method Details

.all(args = {}) ⇒ Object

Backward-compatible aliases for generic CRUD operations. These map common Ruby method names to the actual Moloni API endpoints.



63
64
65
# File 'lib/moloni/base_model.rb', line 63

def self.all(args = {})
  post('getAll/', args)
end

.composed_params(opts) ⇒ Object



39
40
41
42
43
44
# File 'lib/moloni/base_model.rb', line 39

def self.composed_params(opts)
  trimmed_opts = opts.compact
  {
    company_id: Moloni.config.company_id
  }.merge(trimmed_opts)
end

.count(args = {}) ⇒ Object



71
72
73
# File 'lib/moloni/base_model.rb', line 71

def self.count(args = {})
  post('count/', args)
end

.create(args = {}) ⇒ Object



75
76
77
# File 'lib/moloni/base_model.rb', line 75

def self.create(args = {})
  post('insert/', args)
end

.delete(args = {}) ⇒ Object



79
80
81
# File 'lib/moloni/base_model.rb', line 79

def self.delete(args = {})
  post('delete/', args)
end

.find(args = {}) ⇒ Object



67
68
69
# File 'lib/moloni/base_model.rb', line 67

def self.find(args = {})
  post('getOne/', args)
end

.format_url(url, params) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/moloni/base_model.rb', line 16

def self.format_url(url, params)
  formatted = url.dup.strip

  params.each { |key, value| formatted.sub!(":#{key}", value.to_s) }

  formatted
end

.method_missing(method_name, *args, &_block) ⇒ Object

Dynamic dispatch for any Moloni API method. Examples:

Product.getByEAN(ean: '123')
Product.get_by_ean(ean: '123')  # snake_case alias
Invoice.insert(args)


51
52
53
54
55
# File 'lib/moloni/base_model.rb', line 51

def self.method_missing(method_name, *args, &_block)
  args = [{}] if args.empty?
  camelized_method = camelize_method_name(method_name.to_s)
  post("#{camelized_method}/", args.first || {})
end

.post(method_path, opts = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/moloni/base_model.rb', line 28

def self.post(method_path, opts = {})
  ensure_token_valid!

  full_uri = build_full_uri(method_path)
  full_params = composed_params(opts)

  result = api_post(full_uri.to_s, full_params)

  parse_response(result)
end

.respond_to_missing?(_method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/moloni/base_model.rb', line 57

def self.respond_to_missing?(_method_name, _include_private = false)
  true
end

Instance Method Details

#format_url(url, params) ⇒ Object



24
25
26
# File 'lib/moloni/base_model.rb', line 24

def format_url(url, params)
  self.class.format_url(url, params)
end