Class: MoloniApi::Client

Inherits:
API
  • Object
show all
Defined in:
lib/moloni_api/client.rb

Overview

other_params - view documentation for additional options: www.moloni.pt/dev/endpoints/

  • qty

  • offset

global:

  • access_token

  • json

  • human_errors

Constant Summary

Constants inherited from API

API::HTTP_STATUS_MAPPING

Constants included from HttpStatusCodes

HttpStatusCodes::HTTP_BAD_REQUEST_CODE, HttpStatusCodes::HTTP_FORBIDDEN_CODE, HttpStatusCodes::HTTP_NOT_FOUND_CODE, HttpStatusCodes::HTTP_OK_CODE, HttpStatusCodes::HTTP_UNAUTHORIZED_CODE, HttpStatusCodes::HTTP_UNPROCESSABLE_ENTITY_CODE

Constants included from Constants

MoloniApi::Constants::ACCEPT, MoloniApi::Constants::ACCEPTED_OAUTH_SCOPES, MoloniApi::Constants::ACCEPT_CHARSET, MoloniApi::Constants::API_CLIENT_ID, MoloniApi::Constants::API_CLIENT_SECRET, MoloniApi::Constants::API_ENDPOINT, MoloniApi::Constants::CACHE_CONTROL, MoloniApi::Constants::CONTENT_LENGTH, MoloniApi::Constants::CONTENT_TYPE, MoloniApi::Constants::DATE, MoloniApi::Constants::ETAG, MoloniApi::Constants::HEADER_LAST, MoloniApi::Constants::HEADER_LINK, MoloniApi::Constants::HEADER_NEXT, MoloniApi::Constants::LOCATION, MoloniApi::Constants::META_FIRST, MoloniApi::Constants::META_LAST, MoloniApi::Constants::META_NEXT, MoloniApi::Constants::META_PREV, MoloniApi::Constants::META_REL, MoloniApi::Constants::OAUTH_SCOPES, MoloniApi::Constants::PARAM_INCLUDE_RELATED, MoloniApi::Constants::PARAM_PAGE, MoloniApi::Constants::PARAM_PER_PAGE, MoloniApi::Constants::PARAM_START_PAGE, MoloniApi::Constants::RATELIMIT_LIMIT, MoloniApi::Constants::RATELIMIT_REMAINING, MoloniApi::Constants::RATELIMIT_RESET, MoloniApi::Constants::SERVER, MoloniApi::Constants::USER_AGENT

Constants included from ApiExceptions

ApiExceptions::APIExceptionError, ApiExceptions::ApiError, ApiExceptions::ApiRequestsQuotaReachedError, ApiExceptions::BadRequestError, ApiExceptions::ForbiddenError, ApiExceptions::NotFoundError, ApiExceptions::UnauthorizedError, ApiExceptions::UnprocessableEntityError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from API

#initialize, #yield_or_eval

Constructor Details

This class inherits a constructor from MoloniApi::API

Instance Attribute Details

#api_access_tokenObject

Returns the value of attribute api_access_token.



19
20
21
# File 'lib/moloni_api/client.rb', line 19

def api_access_token
  @api_access_token
end

#api_refresh_tokenObject

Returns the value of attribute api_refresh_token.



19
20
21
# File 'lib/moloni_api/client.rb', line 19

def api_refresh_token
  @api_refresh_token
end

Instance Method Details

#authenticate(user_username, user_password) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/moloni_api/client.rb', line 21

def authenticate(user_username, user_password)
  response = request(
    http_method: :post,
    endpoint: 'grant/',
    query_params: {
      grant_type: 'password',
      client_id: self.api_client_id,
      client_secret: self.api_client_secret,
      username: user_username,
      password: user_password
    },
    add_access_token: false
  )
  res = process_response(response)
  self.api_access_token = res[:access_token]
  self.api_refresh_token = res[:refresh_token]
  res
end

#companies(other_params: {}) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/moloni_api/client.rb', line 57

def companies(other_params: {})
  response = request(
    http_method: :post,
    endpoint: 'companies/getAll/',
    query_params: other_params
  )
  process_response(response)
end

#countries(other_params: {}) ⇒ Object

Global Data Methods Note: Calls to these methods should be cached somewhere



269
270
271
272
273
274
275
276
# File 'lib/moloni_api/client.rb', line 269

def countries(other_params: {})
  response = request(
    http_method: :post,
    endpoint: 'countries/getAll/',
    query_params: other_params
  )
  process_response(response)
end

#currencies(other_params: {}) ⇒ Object



296
297
298
299
300
301
302
303
# File 'lib/moloni_api/client.rb', line 296

def currencies(other_params: {})
  response = request(
    http_method: :post,
    endpoint: 'currencies/getAll/',
    query_params: other_params
  )
  process_response(response)
end

#customers_getAll(company_id) ⇒ Object

customers/getAll/



159
160
161
162
163
164
165
166
167
168
# File 'lib/moloni_api/client.rb', line 159

def customers_getAll(company_id)
  response = request(
    http_method: :post,
    endpoint: 'customers/getAll/',
    params: {
      company_id: company_id
    }
  )
  process_response(response)
end

#customers_getByNumber(company_id, customer_number) ⇒ Object

customers/getByNumber/



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/moloni_api/client.rb', line 146

def customers_getByNumber(company_id, customer_number)
  response = request(
    http_method: :post,
    endpoint: 'customers/getByNumber/',
    params: {
      company_id: company_id,
      number: customer_number
    }
  )
  process_response(response)
end

#customers_insert(company_id, customer_data: {}) ⇒ Object

customers/insert/



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/moloni_api/client.rb', line 171

def customers_insert(company_id, customer_data: {})
  # mandatory customer params:
  # vat, number, name, language_id, address, city, country_id, maturity_date_id, payment_method_id
  # optional params:
  # email, zip_code, phone
  response = request(
    http_method: :post,
    endpoint: 'customers/insert/',
    params: { company_id: company_id }.merge(customer_data)
  )
  p_res = process_response(response)
  case p_res
  when Hash
    p_res
  else
    {
      valid: 0,
      errors: p_res
    }
  end
end

#document_models(other_params: {}) ⇒ Object



305
306
307
308
309
310
311
312
# File 'lib/moloni_api/client.rb', line 305

def document_models(other_params: {})
  response = request(
    http_method: :post,
    endpoint: 'documentModels/getAll/',
    query_params: other_params
  )
  process_response(response)
end

#documents_getAll(company_id, customer_id: nil, your_reference: nil, our_reference: nil, other_params: {}) ⇒ Object

MoloniApi::Client.new.calendar(38859, ‘2022-01-01’, ‘2022-07-31’)



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/moloni_api/client.rb', line 67

def documents_getAll(company_id, customer_id: nil, your_reference: nil, our_reference: nil, other_params: {})
  response = request(
    http_method: :post,
    endpoint: 'documents/getAll/',
    params: {
      company_id: company_id,
      customer_id: customer_id,
      your_reference: your_reference,
      our_reference: our_reference
    }.merge(other_params)
  )
  process_response(response)
end

#documentSets_getAll(company_id) ⇒ Object

documentSets/getAll



110
111
112
113
114
115
116
117
118
119
# File 'lib/moloni_api/client.rb', line 110

def documentSets_getAll(company_id)
  response = request(
    http_method: :post,
    endpoint: 'documentSets/getAll/',
    params: {
      company_id: company_id
    }
  )
  process_response(response)
end

#economic_activity_classification_codes(company_id, other_params: {}) ⇒ Object

economicActivityClassificationCodes/getAll/



134
135
136
137
138
139
140
141
142
143
# File 'lib/moloni_api/client.rb', line 134

def economic_activity_classification_codes(company_id, other_params: {})
  response = request(
    http_method: :post,
    endpoint: 'economicActivityClassificationCodes/getAll/',
    params: {
      company_id: company_id
    }
  )
  process_response(response)
end

#fiscal_zones(other_params: {}) ⇒ Object



287
288
289
290
291
292
293
294
# File 'lib/moloni_api/client.rb', line 287

def fiscal_zones(other_params: {})
  response = request(
    http_method: :post,
    endpoint: 'fiscalZones/getAll/',
    query_params: other_params
  )
  process_response(response)
end

#invoiceReceipts_getAll(company_id, customer_id: nil, your_reference: nil, our_reference: nil, other_params: {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/moloni_api/client.rb', line 95

def invoiceReceipts_getAll(company_id, customer_id: nil, your_reference: nil, our_reference: nil, other_params: {})
  response = request(
    http_method: :post,
    endpoint: 'invoiceReceipts/getAll/',
    params: {
      company_id: company_id,
      customer_id: customer_id,
      your_reference: your_reference,
      our_reference: our_reference
    }.merge(other_params)
  )
  process_response(response)
end

#invoiceReceipts_insert(company_id, invoice_params: {}, products: [], payments: []) ⇒ Object

invoiceReceipts/insert/



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/moloni_api/client.rb', line 224

def invoiceReceipts_insert(company_id, invoice_params: {}, products: [], payments: [])
  # mandatory invoice params:
  # date, expiration_date, document_set_id, customer_id, products, payments
  # optional params:
  # our_reference, your_reference...
  response = request(
    http_method: :post,
    endpoint: 'invoiceReceipts/insert/',
    params: {
      company_id: company_id,
      products: products,
      payments: payments
    }.merge(invoice_params)
  )
  process_response(response)
end

#invoices_insert(company_id, invoice_params: {}, products: []) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/moloni_api/client.rb', line 193

def invoices_insert(company_id, invoice_params: {}, products: [])
  # mandatory invoice params:
  # date, expiration_date, document_set_id, customer_id, products
  # optional params:
  # our_reference, your_reference
  response = request(
    http_method: :post,
    endpoint: 'invoices/insert/',
    params: { company_id: company_id, products: products }.merge(invoice_params)
  )
  process_response(response)
end

#languages(other_params: {}) ⇒ Object



278
279
280
281
282
283
284
285
# File 'lib/moloni_api/client.rb', line 278

def languages(other_params: {})
  response = request(
    http_method: :post,
    endpoint: 'languages/getAll/',
    query_params: other_params
  )
  process_response(response)
end

#paymentMethods_getAll(company_id) ⇒ Object

paymentMethods/getAll/



122
123
124
125
126
127
128
129
130
131
# File 'lib/moloni_api/client.rb', line 122

def paymentMethods_getAll(company_id)
  response = request(
    http_method: :post,
    endpoint: 'paymentMethods/getAll/',
    params: {
      company_id: company_id
    }
  )
  process_response(response)
end

#products_getByReference(company_id, reference, other_params: {}) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/moloni_api/client.rb', line 253

def products_getByReference(company_id, reference, other_params: {})
  response = request(
    http_method: :post,
    endpoint: 'products/getByReference/',
    params: {
      company_id: company_id,
      reference: reference
    }.merge(other_params)
  )
  process_response(response)
end

#products_getBySearch(company_id, search_str, other_params: {}) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
# File 'lib/moloni_api/client.rb', line 241

def products_getBySearch(company_id, search_str, other_params: {})
  response = request(
    http_method: :post,
    endpoint: 'products/getBySearch/',
    params: {
      company_id: company_id,
      search: search_str
    }.merge(other_params)
  )
  process_response(response)
end

#renew_token(refresh_token = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/moloni_api/client.rb', line 40

def renew_token(refresh_token = nil)
  response = request(
    http_method: :post,
    endpoint: 'grant/',
    query_params: {
      grant_type: 'refresh_token',
      client_id: self.api_client_id,
      client_secret: self.api_client_secret,
      refresh_token: refresh_token || self.api_refresh_token
    },
    add_access_token: false
  )
  res = process_response(response)
  self.api_access_token = res[:access_token]
  res
end

#simplifiedInvoices_getAll(company_id, customer_id: nil, your_reference: nil, our_reference: nil, other_params: {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/moloni_api/client.rb', line 81

def simplifiedInvoices_getAll(company_id, customer_id: nil, your_reference: nil, our_reference: nil, other_params: {})
  response = request(
    http_method: :post,
    endpoint: 'simplifiedInvoices/getAll/',
    params: {
      company_id: company_id,
      customer_id: customer_id,
      your_reference: your_reference,
      our_reference: our_reference
    }.merge(other_params)
  )
  process_response(response)
end

#simplifiedInvoices_insert(company_id, invoice_params: {}, products: [], payments: []) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/moloni_api/client.rb', line 206

def simplifiedInvoices_insert(company_id, invoice_params: {}, products: [], payments: [])
  # mandatory invoice params:
  # date, expiration_date, document_set_id, customer_id, products, payments
  # optional params:
  # our_reference, your_reference...
  response = request(
    http_method: :post,
    endpoint: 'simplifiedInvoices/insert/',
    params: {
      company_id: company_id,
      products: products,
      payments: payments
    }.merge(invoice_params)
  )
  process_response(response)
end