moloni_api 0.1.1 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2cc0888f69d92fa88c0a05fa447f841f29289b2e8ab44ed9a22e446d2a1155b2
4
- data.tar.gz: 0f40cc723684575b0659735e85a4793d4abe218dd5d246006fc7f09f9d3c0acb
3
+ metadata.gz: d4ce20574d9044023df19c350c588d21ff192530da34e92b20185c70cf9fffe3
4
+ data.tar.gz: e40027e3826ea130df3e9f8ab435522958eb549232dc85c87e883c004c7daa93
5
5
  SHA512:
6
- metadata.gz: ad88efc407714ef37b5a1e7659d0b868565df5d83f58aea2c456c5cbbb1b85c231fba5962acfa77d13915c14195ddad054a4a92a1483f90a32c11943ba0ac25b
7
- data.tar.gz: '0739379b6fe2beb1c62c30316d7827f3bc6b6091da77919223f2717fcc6decd64ab83477cfa1c082b6fc438a279996742992a762a0bc6b059f98c71612096d63'
6
+ metadata.gz: 0c94ad5acf50457127342b81d97f463a4874ea417232ff25dc954b3b69da1fe8338f4a533af16baf422577f7b3949ccf7b84f79fc3da050b2a12084949a79e37
7
+ data.tar.gz: 0f84659d5f1183ec4b4d75c0c6bf5eaa7ec1ffa2e86dfcaf2ef13c1bd4fc488646b2852df2b80c2764d5a5d4f65a35c2ac358a2222b591723ced38cc204241c3
data/.gitignore CHANGED
@@ -16,3 +16,5 @@ moloni_api.iml
16
16
 
17
17
  # custom ignores
18
18
  .env
19
+ *.gem
20
+ /.irb_history
data/.irbrc ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'irb'
4
+ require 'irb/completion'
5
+ require 'rubygems'
6
+ require 'pp'
7
+ IRB.conf[:SAVE_HISTORY] = 300
data/Dockerfile CHANGED
@@ -3,6 +3,7 @@ FROM ruby:3.0
3
3
  WORKDIR /usr/src/app
4
4
 
5
5
  COPY Gemfile .
6
+ COPY Gemfile.lock .
6
7
  COPY moloni_api.gemspec .
7
8
  COPY lib/moloni_api/version.rb lib/moloni_api/version.rb
8
9
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- moloni_api (0.1.0)
4
+ moloni_api (0.1.3)
5
5
  faraday (~> 1.3.0)
6
6
  oj (~> 3.11)
7
7
 
@@ -15,14 +15,14 @@ GEM
15
15
  rexml
16
16
  diff-lcs (1.4.4)
17
17
  dotenv (2.7.6)
18
- faraday (1.3.0)
18
+ faraday (1.3.1)
19
19
  faraday-net_http (~> 1.0)
20
20
  multipart-post (>= 1.2, < 3)
21
- ruby2_keywords
21
+ ruby2_keywords (>= 0.0.4)
22
22
  faraday-net_http (1.0.1)
23
23
  hashdiff (1.0.1)
24
- multipart-post (2.1.1)
25
- oj (3.11.3)
24
+ multipart-post (2.2.3)
25
+ oj (3.13.21)
26
26
  parallel (1.20.1)
27
27
  parser (3.0.0.0)
28
28
  ast (~> 2.4.1)
@@ -56,7 +56,7 @@ GEM
56
56
  rubocop-ast (1.4.1)
57
57
  parser (>= 2.7.1.5)
58
58
  ruby-progressbar (1.11.0)
59
- ruby2_keywords (0.0.4)
59
+ ruby2_keywords (0.0.5)
60
60
  unicode-display_width (1.7.0)
61
61
  vcr (6.0.0)
62
62
  webmock (3.12.2)
data/README.md CHANGED
@@ -1,8 +1,16 @@
1
1
  # MoloniApi
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/moloni_api`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ ![main workflow](https://github.com/dlage/moloni_api_gem/actions/workflows/main.yml/badge.svg)
4
+
5
+ Implementation of the [Moloni API](https://www.moloni.pt/dev/endpoints/).
6
+ It's still a rough draft but already usable.
7
+ I will be adding more method implementation but it should be easy for anyone to add new methods (Pull Requests are welcome!).
8
+ There's already a mix of methods implemented to:
9
+ - Get and insert Customers
10
+ - Get Products
11
+ - Get and create some Documents
12
+ - Get Payment Methods
13
+ Moloni's API is quite extensive, reason why I didn't implement methods for all endpoints, yet.
6
14
 
7
15
  ## Installation
8
16
 
@@ -32,7 +40,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
40
 
33
41
  ## Contributing
34
42
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/moloni_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/moloni_api/blob/master/CODE_OF_CONDUCT.md).
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dlage/moloni_api_gem. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/dlage/moloni_api_gem/blob/master/CODE_OF_CONDUCT.md).
36
44
 
37
45
  ## License
38
46
 
@@ -40,4 +48,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
40
48
 
41
49
  ## Code of Conduct
42
50
 
43
- Everyone interacting in the MoloniApi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/moloni_api/blob/master/CODE_OF_CONDUCT.md).
51
+ Everyone interacting in the MoloniApi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/dlage/moloni_api/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile CHANGED
@@ -3,10 +3,13 @@
3
3
  require 'bundler/gem_tasks'
4
4
  require 'rspec/core/rake_task'
5
5
 
6
- RSpec::Core::RakeTask.new(:spec)
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.pattern = FileList['spec/**/*_spec.rb']
8
+ end
7
9
 
8
10
  require 'rubocop/rake_task'
9
11
 
10
12
  RuboCop::RakeTask.new
11
13
 
12
- task default: %i[spec rubocop]
14
+ # task default: %i[spec rubocop]
15
+ task default: %i[spec]
@@ -6,14 +6,14 @@ require_relative 'constants'
6
6
  require_relative 'http_status_codes'
7
7
  require 'json'
8
8
 
9
+ # Core class responsible for api interface operations
9
10
  module MoloniApi
10
- # Core class responsible for api interface operations
11
11
  class API
12
12
  include ApiExceptions
13
13
  include Constants
14
14
  include HttpStatusCodes
15
15
 
16
- attr_reader(*MoloniApi.configuration.property_names, :token, :endpoint)
16
+ attr_reader(*MoloniApi.configuration.property_names)
17
17
 
18
18
  attr_accessor :current_options
19
19
 
@@ -27,7 +27,6 @@ module MoloniApi
27
27
  end
28
28
  end
29
29
 
30
- API_ENDPOINT = 'https://api.moloni.pt/sandbox/'
31
30
  HTTP_STATUS_MAPPING = {
32
31
  HTTP_BAD_REQUEST_CODE => BadRequestError,
33
32
  HTTP_UNAUTHORIZED_CODE => UnauthorizedError,
@@ -47,10 +46,6 @@ module MoloniApi
47
46
  MoloniApi.configuration.property_names.each do |key|
48
47
  send("#{key}=", opts[key])
49
48
  end
50
- @api_access_token = opts[:access_token] # If not provided will need to ask for credentials
51
- @api_client_id = opts[:client_id] || ENV['MOLONI_API_CLIENT_ID']
52
- @api_client_secret = opts[:client_secret] || ENV['MOLONI_API_CLIENT_SECRET']
53
- @api_endpoint = opts[:endpoint] || ENV['MOLONI_API_ENDPOINT'] || API_ENDPOINT
54
49
 
55
50
  yield_or_eval(&block) if block_given?
56
51
  end
@@ -70,11 +65,12 @@ module MoloniApi
70
65
  # provide your own logger
71
66
  logger = Logger.new $stderr
72
67
  logger.level = Logger::DEBUG
73
- @client ||= Faraday.new(@api_endpoint) do |client|
68
+ @client ||= Faraday.new(@endpoint) do |client|
74
69
  client.request :url_encoded
75
- #client.request :json
76
- #client.response :json
70
+ # client.request :json
71
+ # client.response :json
77
72
  client.adapter Faraday.default_adapter
73
+ client.headers['User-Agent'] = @user_agent
78
74
  client.response :logger, logger
79
75
  end
80
76
  end
@@ -90,7 +86,7 @@ module MoloniApi
90
86
  req.body = params.to_json if params.size
91
87
  end
92
88
  logger = Logger.new $stderr
93
- logger.debug('Response: ' + response.body)
89
+ logger.debug("Response: #{response.body}")
94
90
 
95
91
  if response_successful?(response)
96
92
  parsed_response = Oj.load(response.body)
@@ -119,7 +115,7 @@ module MoloniApi
119
115
  # :nodoc:
120
116
  case method_name.to_s
121
117
  when /^(.*)\?$/
122
- !!send(Regexp.last_match(1).to_s)
118
+ !send(Regexp.last_match(1).to_s).nil?
123
119
  when /^clear_(.*)$/
124
120
  send("#{Regexp.last_match(1)}=", nil)
125
121
  else
@@ -3,10 +3,10 @@
3
3
  require 'json'
4
4
  require 'logger'
5
5
  require_relative 'api'
6
- require_relative 'models/product'
6
+ # require_relative 'models/product'
7
7
 
8
+ # Main client class that implements communication with the API
8
9
  module MoloniApi
9
- # Main client class that implements communication with the API
10
10
  # other_params - view documentation for additional options:
11
11
  # https://www.moloni.pt/dev/endpoints/
12
12
  # - qty
@@ -16,12 +16,11 @@ module MoloniApi
16
16
  # - json
17
17
  # - human_errors
18
18
  class Client < API
19
-
20
19
  attr_accessor :api_access_token, :api_refresh_token
21
20
 
22
21
  def authenticate(user_username, user_password)
23
22
  response = request(
24
- http_method: :get,
23
+ http_method: :post,
25
24
  endpoint: 'grant/',
26
25
  query_params: {
27
26
  grant_type: 'password',
@@ -35,12 +34,12 @@ module MoloniApi
35
34
  res = process_response(response)
36
35
  self.api_access_token = res[:access_token]
37
36
  self.api_refresh_token = res[:refresh_token]
38
- return res
37
+ res
39
38
  end
40
39
 
41
40
  def renew_token(refresh_token = nil)
42
41
  response = request(
43
- http_method: :get,
42
+ http_method: :post,
44
43
  endpoint: 'grant/',
45
44
  query_params: {
46
45
  grant_type: 'refresh_token',
@@ -52,12 +51,12 @@ module MoloniApi
52
51
  )
53
52
  res = process_response(response)
54
53
  @api_access_token = res[:access_token]
55
- return res
54
+ res
56
55
  end
57
56
 
58
57
  def companies(other_params: {})
59
58
  response = request(
60
- http_method: :get,
59
+ http_method: :post,
61
60
  endpoint: 'companies/getAll/',
62
61
  query_params: other_params
63
62
  )
@@ -73,7 +72,7 @@ module MoloniApi
73
72
  company_id: company_id,
74
73
  customer_id: customer_id,
75
74
  your_reference: your_reference,
76
- our_reference: our_reference,
75
+ our_reference: our_reference
77
76
  }.merge(other_params)
78
77
  )
79
78
  process_response(response)
@@ -87,7 +86,7 @@ module MoloniApi
87
86
  company_id: company_id,
88
87
  customer_id: customer_id,
89
88
  your_reference: your_reference,
90
- our_reference: our_reference,
89
+ our_reference: our_reference
91
90
  }.merge(other_params)
92
91
  )
93
92
  process_response(response)
@@ -101,7 +100,7 @@ module MoloniApi
101
100
  company_id: company_id,
102
101
  customer_id: customer_id,
103
102
  your_reference: your_reference,
104
- our_reference: our_reference,
103
+ our_reference: our_reference
105
104
  }.merge(other_params)
106
105
  )
107
106
  process_response(response)
@@ -131,6 +130,18 @@ module MoloniApi
131
130
  process_response(response)
132
131
  end
133
132
 
133
+ # economicActivityClassificationCodes/getAll/
134
+ def economic_activity_classification_codes(company_id, other_params: {})
135
+ response = request(
136
+ http_method: :post,
137
+ endpoint: 'economicActivityClassificationCodes/getAll/',
138
+ params: {
139
+ company_id: company_id
140
+ }
141
+ )
142
+ process_response(response)
143
+ end
144
+
134
145
  # customers/getByNumber/
135
146
  def customers_getByNumber(company_id, customer_number)
136
147
  response = request(
@@ -138,7 +149,7 @@ module MoloniApi
138
149
  endpoint: 'customers/getByNumber/',
139
150
  params: {
140
151
  company_id: company_id,
141
- number: customer_number,
152
+ number: customer_number
142
153
  }
143
154
  )
144
155
  process_response(response)
@@ -224,7 +235,7 @@ module MoloniApi
224
235
  endpoint: 'products/getBySearch/',
225
236
  params: {
226
237
  company_id: company_id,
227
- search: search_str,
238
+ search: search_str
228
239
  }.merge(other_params)
229
240
  )
230
241
  process_response(response)
@@ -236,12 +247,61 @@ module MoloniApi
236
247
  endpoint: 'products/getByReference/',
237
248
  params: {
238
249
  company_id: company_id,
239
- reference: reference,
250
+ reference: reference
240
251
  }.merge(other_params)
241
252
  )
242
253
  process_response(response)
243
254
  end
244
255
 
256
+ #############################################
257
+ ### Global Data Methods
258
+ # Note: Calls to these methods should be cached somewhere
259
+ #############################################
260
+ def countries(other_params: {})
261
+ response = request(
262
+ http_method: :post,
263
+ endpoint: 'countries/getAll/',
264
+ query_params: other_params
265
+ )
266
+ process_response(response)
267
+ end
268
+
269
+ def languages(other_params: {})
270
+ response = request(
271
+ http_method: :post,
272
+ endpoint: 'languages/getAll/',
273
+ query_params: other_params
274
+ )
275
+ process_response(response)
276
+ end
277
+
278
+ def fiscal_zones(other_params: {})
279
+ response = request(
280
+ http_method: :post,
281
+ endpoint: 'fiscalZones/getAll/',
282
+ query_params: other_params
283
+ )
284
+ process_response(response)
285
+ end
286
+
287
+ def currencies(other_params: {})
288
+ response = request(
289
+ http_method: :post,
290
+ endpoint: 'currencies/getAll/',
291
+ query_params: other_params
292
+ )
293
+ process_response(response)
294
+ end
295
+
296
+ def document_models(other_params: {})
297
+ response = request(
298
+ http_method: :post,
299
+ endpoint: 'documentModels/getAll/',
300
+ query_params: other_params
301
+ )
302
+ process_response(response)
303
+ end
304
+
245
305
  protected
246
306
 
247
307
  def process_response(response)
@@ -249,7 +309,7 @@ module MoloniApi
249
309
  case result
250
310
  when Hash
251
311
  result.transform_keys!(&:to_sym)
252
- result.values.each do |r|
312
+ result.each_value do |r|
253
313
  process_response(r)
254
314
  end
255
315
  when Array
@@ -6,23 +6,31 @@ require_relative 'version'
6
6
  module MoloniApi
7
7
  # Stores the configuration
8
8
  class Configuration < API::Config
9
+ API_ENDPOINT = 'https://api.moloni.pt/sandbox/'
10
+ API_CLIENT_ID = 'apigem'
11
+ API_CLIENT_SECRET = 'c9c9f4274658da2ad78b55a452894942898b5614'
12
+
9
13
  property :follow_redirects, default: true
10
14
 
11
15
  # The api endpoint used to connect to MoloniApi if none is set
12
16
  # prd: https://api.moloni.pt/v1/
13
17
  # sandbox: https://api.moloni.pt/sandbox/
14
- property :endpoint, default: 'https://api.moloni.pt/sandbox/'
18
+ property :endpoint, default: ENV['MOLONI_API_ENDPOINT'] || API_ENDPOINT
15
19
 
16
20
  # The value sent in the http header for 'User-Agent' if none is set
17
- property :user_agent, default: "MoloniApi API Ruby Gem #{MoloniApi::VERSION}"
21
+ property :user_agent, default: "MoloniApi API Ruby Gem #{MoloniApi::VERSION}"
18
22
 
19
23
  # By default uses the Faraday connection options if none is set
20
- property :connection_options, default: {}
24
+ property :connection_options, default: {}
21
25
 
22
26
  # By default display 30 resources
23
27
  property :per_page, default: 20
24
28
 
25
29
  # Add Faraday::RackBuilder to overwrite middleware
26
30
  property :stack
31
+
32
+ property :api_access_token
33
+ property :api_client_id, default: ENV['MOLONI_API_CLIENT_ID'] || API_CLIENT_ID
34
+ property :api_client_secret, default: ENV['MOLONI_API_CLIENT_SECRET'] || API_CLIENT_SECRET
27
35
  end
28
36
  end
@@ -1,39 +1,42 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module MoloniApi
2
4
  module Models
5
+ # Model to map product
3
6
  class Product
4
7
  ##
5
8
  # company_id int
6
- # Obrigatório
9
+ # Mandatory
7
10
  #
8
11
  # category_id int
9
- # Obrigatório
12
+ # Mandatory
10
13
  #
11
14
  # type int
12
- # Obrigatório
15
+ # Mandatory
13
16
  #
14
17
  # name string
15
- # Obrigatório
18
+ # Mandatory
16
19
  #
17
20
  # summary string
18
21
  # Facultativo
19
22
  #
20
23
  # reference string
21
- # Obrigatório
24
+ # Mandatory
22
25
  #
23
26
  # ean string
24
27
  # Facultativo
25
28
  #
26
29
  # price float
27
- # Obrigatório
30
+ # Mandatory
28
31
  #
29
32
  # unit_id int
30
- # Obrigatório
33
+ # Mandatory
31
34
  #
32
35
  # has_stock int
33
- # Obrigatório
36
+ # Mandatory
34
37
  #
35
38
  # stock float
36
- # Obrigatório
39
+ # Mandatory
37
40
  #
38
41
  # minimum_stock float
39
42
  # Facultativo
@@ -51,25 +54,25 @@ module MoloniApi
51
54
  # Facultativo
52
55
  #
53
56
  # tax_id int
54
- # Obrigatório
57
+ # Mandatory
55
58
  #
56
59
  # value float
57
- # Obrigatório
60
+ # Mandatory
58
61
  #
59
62
  # order int
60
- # Obrigatório
63
+ # Mandatory
61
64
  #
62
65
  # cumulative int
63
- # Obrigatório
66
+ # Mandatory
64
67
  #
65
68
  # suppliers array
66
69
  # Facultativo
67
70
  #
68
71
  # supplier_id int
69
- # Obrigatório
72
+ # Mandatory
70
73
  #
71
74
  # cost_price float
72
- # Obrigatório
75
+ # Mandatory
73
76
  #
74
77
  # referenceint
75
78
  # Facultativo
@@ -78,19 +81,19 @@ module MoloniApi
78
81
  # Facultativo
79
82
  #
80
83
  # property_id int
81
- # Obrigatório
84
+ # Mandatory
82
85
  #
83
86
  # value string
84
- # Obrigatório
87
+ # Mandatory
85
88
  #
86
89
  # warehouses array
87
90
  # Facultativo
88
91
  #
89
92
  # warehouse_id int
90
- # Obrigatório
93
+ # Mandatory
91
94
  #
92
95
  # stock float
93
- # Obrigatório
96
+ # Mandatory
94
97
  def initialize(raw_result)
95
98
  @raw_result = raw_result
96
99
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MoloniApi
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.4'
5
5
  end
data/moloni_api.gemspec CHANGED
@@ -8,13 +8,13 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ['Dinis']
9
9
  spec.email = ['dinis@lage.pw']
10
10
 
11
- spec.summary = 'FantasticStay API Wrapper.'
12
- spec.description = 'A gem that implements functions from the FS API available for its users.'
11
+ spec.summary = 'Moloni API Wrapper.'
12
+ spec.description = 'A gem that implements functions from the Moloni API available for its users.'
13
13
  spec.homepage = 'https://github.com/dlage/moloni_api_gem'
14
14
  spec.license = 'MIT'
15
15
  spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
16
16
 
17
- spec.metadata['allowed_push_host'] = "https://rubygems.org"
17
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18
18
 
19
19
  spec.metadata['homepage_uri'] = spec.homepage
20
20
  spec.metadata['source_code_uri'] = 'https://github.com/dlage/moloni_api_gem'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moloni_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dinis
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-11 00:00:00.000000000 Z
11
+ date: 2022-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -38,7 +38,8 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.11'
41
- description: A gem that implements functions from the FS API available for its users.
41
+ description: A gem that implements functions from the Moloni API available for its
42
+ users.
42
43
  email:
43
44
  - dinis@lage.pw
44
45
  executables: []
@@ -47,6 +48,7 @@ extra_rdoc_files: []
47
48
  files:
48
49
  - ".github/workflows/main.yml"
49
50
  - ".gitignore"
51
+ - ".irbrc"
50
52
  - ".rspec"
51
53
  - ".rubocop.yml"
52
54
  - CHANGELOG.md
@@ -81,7 +83,7 @@ metadata:
81
83
  homepage_uri: https://github.com/dlage/moloni_api_gem
82
84
  source_code_uri: https://github.com/dlage/moloni_api_gem
83
85
  changelog_uri: https://github.com/dlage/moloni_api_gem/blob/master/CHANGELOG.md
84
- post_install_message:
86
+ post_install_message:
85
87
  rdoc_options: []
86
88
  require_paths:
87
89
  - lib
@@ -96,8 +98,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
98
  - !ruby/object:Gem::Version
97
99
  version: '0'
98
100
  requirements: []
99
- rubygems_version: 3.3.19
100
- signing_key:
101
+ rubygems_version: 3.2.33
102
+ signing_key:
101
103
  specification_version: 4
102
- summary: FantasticStay API Wrapper.
104
+ summary: Moloni API Wrapper.
103
105
  test_files: []