moloni_api 0.1.3 → 0.2.0

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: 74d28c5f93cc5740ba6cf9f0af4937b931701c0b8914f795a5f888275b119bf3
4
- data.tar.gz: 87d99c68508f7cb982f97e8b561f53620ae993509fd95ab3ed7ade1c31b0e8f8
3
+ metadata.gz: 6ec107d21be2536a63ab97fe0280cf8f7ad4db82759272e6f93a649224e869e3
4
+ data.tar.gz: 342955e233c5fb4ea13b0073f856701ce096b559d26229018efa3f17cbafee08
5
5
  SHA512:
6
- metadata.gz: d59fa669a82f34f31b520a340aaa245d2236d1b253e73d70f7c00cf9470cbb02fc938e227e1df92115196136b762e4cd2bf7705460f49a1075a005baac073de1
7
- data.tar.gz: 28e247ea912108102b9725adba774564b5354c0840f5933573f903139a305d07b2e12ce010061465e39f94908ae36c5ac14dd301d26de679002d0938d07aebf4
6
+ metadata.gz: 52589d46b7dba75dbf5f15e938391c3fe65cf1571a35dd1029ff895e5ce2f61da690441d92829362e64840542f2d32c4aa2d108dac4d0136433ec0d8b5c1fb85
7
+ data.tar.gz: 998755fc207a05796be2664529ce81123fc141e4b26670e976600df7d36ab551ef9623bbde5a23be8c13438799c3c486de95d8d64b3d3599aade13ff4e613213
data/.irbrc CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'irb'
2
4
  require 'irb/completion'
3
5
  require 'rubygems'
data/CHANGELOG.md CHANGED
@@ -0,0 +1,2 @@
1
+ * 0.2.0 - Breaking change: initialization using a configure block now - dry-configurable
2
+ * 0.1.5 - Passing tests
data/Gemfile CHANGED
@@ -5,8 +5,6 @@ source 'https://rubygems.org'
5
5
  # Specify your gem's dependencies in moloni_api.gemspec
6
6
  gemspec
7
7
 
8
- gem 'dotenv', '~> 2.7'
9
-
10
8
  group :test do
11
9
  gem 'rake', '~> 13.0'
12
10
  gem 'rspec', '~> 3.0'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- moloni_api (0.1.2)
4
+ moloni_api (0.1.3)
5
5
  faraday (~> 1.3.0)
6
6
  oj (~> 3.11)
7
7
 
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # MoloniApi
2
2
 
3
+ ![main workflow](https://github.com/dlage/moloni_api_gem/actions/workflows/main.yml/badge.svg)
4
+
3
5
  Implementation of the [Moloni API](https://www.moloni.pt/dev/endpoints/).
4
6
  It's still a rough draft but already usable.
5
7
  I will be adding more method implementation but it should be easy for anyone to add new methods (Pull Requests are welcome!).
@@ -38,7 +40,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
38
40
 
39
41
  ## Contributing
40
42
 
41
- Bug reports and pull requests are welcome on GitHub at https://github.com/dlage/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/dlage/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).
42
44
 
43
45
  ## License
44
46
 
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]
@@ -1,32 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'dry-configurable'
3
4
  require_relative 'api_exceptions'
4
- require_relative 'configuration'
5
5
  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
+ include Dry::Configurable
15
16
 
16
- attr_reader(*MoloniApi.configuration.property_names)
17
-
18
- attr_accessor :current_options
19
-
20
- # Callback to update current configuration options
21
- class_eval do
22
- MoloniApi.configuration.property_names.each do |key|
23
- define_method "#{key}=" do |arg|
24
- instance_variable_set("@#{key}", arg)
25
- current_options.merge!({ "#{key}": arg })
26
- end
27
- end
28
- end
29
-
17
+ API_ENDPOINT = 'https://api.moloni.pt/sandbox/'
18
+ API_CLIENT_ID = 'apigem'
19
+ API_CLIENT_SECRET = 'c9c9f4274658da2ad78b55a452894942898b5614'
30
20
  HTTP_STATUS_MAPPING = {
31
21
  HTTP_BAD_REQUEST_CODE => BadRequestError,
32
22
  HTTP_UNAUTHORIZED_CODE => UnauthorizedError,
@@ -36,19 +26,28 @@ module MoloniApi
36
26
  'default' => ApiError
37
27
  }.freeze
38
28
 
39
- # Create new API
40
- #
41
- # @api public
42
- def initialize(options = {}, &block)
43
- opts = MoloniApi.configuration.fetch.merge(options)
44
- @current_options = opts
29
+ setting :follow_redirects, default: true
45
30
 
46
- MoloniApi.configuration.property_names.each do |key|
47
- send("#{key}=", opts[key])
48
- end
31
+ # The api endpoint used to connect to MoloniApi if none is set
32
+ # prd: https://api.moloni.pt/v1/
33
+ # sandbox: https://api.moloni.pt/sandbox/
34
+ setting :endpoint, default: ENV['MOLONI_API_ENDPOINT'] || API_ENDPOINT, reader: true
49
35
 
50
- yield_or_eval(&block) if block_given?
51
- end
36
+ # The value sent in the http header for 'User-Agent' if none is set
37
+ setting :user_agent, default: "MoloniApi API Ruby Gem #{MoloniApi::VERSION}"
38
+
39
+ # By default uses the Faraday connection options if none is set
40
+ setting :connection_options, default: {}
41
+
42
+ # By default display 30 resources
43
+ setting :per_page, default: 20
44
+
45
+ # Add Faraday::RackBuilder to overwrite middleware
46
+ setting :stack
47
+
48
+ setting :api_access_token, reader: true
49
+ setting :api_client_id, default: ENV['MOLONI_API_CLIENT_ID'] || API_CLIENT_ID, reader: true
50
+ setting :api_client_secret, default: ENV['MOLONI_API_CLIENT_SECRET'] || API_CLIENT_SECRET, reader: true
52
51
 
53
52
  # Call block with argument
54
53
  #
@@ -65,19 +64,19 @@ module MoloniApi
65
64
  # provide your own logger
66
65
  logger = Logger.new $stderr
67
66
  logger.level = Logger::DEBUG
68
- @client ||= Faraday.new(@endpoint) do |client|
67
+ @client ||= Faraday.new(config.endpoint) do |client|
69
68
  client.request :url_encoded
70
69
  # client.request :json
71
70
  # client.response :json
72
71
  client.adapter Faraday.default_adapter
73
- client.headers['User-Agent'] = @user_agent
72
+ client.headers['User-Agent'] = config.user_agent
74
73
  client.response :logger, logger
75
74
  end
76
75
  end
77
76
 
78
77
  def request(http_method:, endpoint:, params: {}, query_params: {}, add_access_token: true)
79
78
  if add_access_token
80
- query_params[:access_token] = @api_access_token
79
+ query_params[:access_token] = self.api_access_token
81
80
  query_params[:json] = true
82
81
  end
83
82
  response = client.public_send(http_method, endpoint) do |req|
@@ -86,7 +85,7 @@ module MoloniApi
86
85
  req.body = params.to_json if params.size
87
86
  end
88
87
  logger = Logger.new $stderr
89
- logger.debug('Response: ' + response.body)
88
+ logger.debug("Response: #{response.body}")
90
89
 
91
90
  if response_successful?(response)
92
91
  parsed_response = Oj.load(response.body)
@@ -107,25 +106,6 @@ module MoloniApi
107
106
  def response_successful?(response)
108
107
  response.status == HTTP_OK_CODE
109
108
  end
110
-
111
- # Responds to attribute query or attribute clear
112
- #
113
- # @api private
114
- def method_missing(method_name, *args, &block)
115
- # :nodoc:
116
- case method_name.to_s
117
- when /^(.*)\?$/
118
- !!send(Regexp.last_match(1).to_s)
119
- when /^clear_(.*)$/
120
- send("#{Regexp.last_match(1)}=", nil)
121
- else
122
- super
123
- end
124
- end
125
-
126
- def respond_to_missing?(method_name, include_private = false)
127
- method_name.to_s.start_with?('clear_') || super
128
- end
129
109
  end
130
110
  end
131
111
  # mapi = MoloniApi.new(token: )
@@ -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,7 +16,6 @@ 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)
@@ -25,8 +24,8 @@ module MoloniApi
25
24
  endpoint: 'grant/',
26
25
  query_params: {
27
26
  grant_type: 'password',
28
- client_id: @api_client_id,
29
- client_secret: @api_client_secret,
27
+ client_id: self.api_client_id,
28
+ client_secret: self.api_client_secret,
30
29
  username: user_username,
31
30
  password: user_password
32
31
  },
@@ -35,7 +34,7 @@ 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)
@@ -44,20 +43,20 @@ module MoloniApi
44
43
  endpoint: 'grant/',
45
44
  query_params: {
46
45
  grant_type: 'refresh_token',
47
- client_id: @api_client_id,
48
- client_secret: @api_client_secret,
49
- refresh_token: refresh_token || @api_refresh_token
46
+ client_id: self.api_client_id,
47
+ client_secret: self.api_client_secret,
48
+ refresh_token: refresh_token || self.api_refresh_token
50
49
  },
51
50
  add_access_token: false
52
51
  )
53
52
  res = process_response(response)
54
- @api_access_token = res[:access_token]
55
- return res
53
+ self.api_access_token = res[:access_token]
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,7 +247,7 @@ 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)
@@ -298,7 +309,7 @@ module MoloniApi
298
309
  case result
299
310
  when Hash
300
311
  result.transform_keys!(&:to_sym)
301
- result.values.each do |r|
312
+ result.each_value do |r|
302
313
  process_response(r)
303
314
  end
304
315
  when Array
@@ -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.3'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/moloni_api.rb CHANGED
@@ -13,45 +13,13 @@ module MoloniApi
13
13
  LIBDIR = File.expand_path(LIBNAME.to_s, __dir__)
14
14
 
15
15
  class << self
16
- # The client configuration
17
- #
18
- # @return [Configuration]
19
- #
20
- # @api public
21
- def configuration
22
- @configuration ||= Configuration.new
23
- end
24
-
25
- alias config configuration
26
-
27
- # Configure options
28
- #
29
- # @example
30
- # MoloniApi.configure do |c|
31
- # c.some_option = true
32
- # end
33
- #
34
- # @yield the configuration block
35
- # @yieldparam configuration [MoloniApi::Configuration]
36
- # the configuration instance
37
- #
38
- # @return [nil]
39
- #
40
- # @api public
41
- def configure
42
- yield configuration
43
- end
44
-
45
16
  # Alias for MoloniApi::Client.new
46
17
  #
47
- # @param [Hash] options
48
- # the configuration options
49
- #
50
18
  # @return [MoloniApi::Client]
51
19
  #
52
20
  # @api public
53
- def new(options = {}, &block)
54
- Client.new(options, &block)
21
+ def new
22
+ Client.new
55
23
  end
56
24
 
57
25
  # Default middleware stack that uses default adapter as specified
@@ -70,8 +38,8 @@ module MoloniApi
70
38
  def method_missing(method_name, *args, &block)
71
39
  if new.respond_to?(method_name)
72
40
  new.send(method_name, *args, &block)
73
- elsif configuration.respond_to?(method_name)
74
- MoloniApi.configuration.send(method_name, *args, &block)
41
+ elsif new.config.respond_to?(method_name)
42
+ MoloniApi::Client.config.send(method_name, *args, &block)
75
43
  else
76
44
  super.respond_to_missing?
77
45
  end
@@ -86,4 +54,3 @@ module MoloniApi
86
54
  end
87
55
 
88
56
  require_relative 'moloni_api/client'
89
- require_relative 'moloni_api/configuration'
data/moloni_api.gemspec CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
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'
@@ -31,9 +31,9 @@ Gem::Specification.new do |spec|
31
31
 
32
32
  # Uncomment to register a new dependency of your gem
33
33
  # spec.add_dependency "example-gem", "~> 1.0"
34
- spec.add_dependency 'faraday', '~> 1.3.0'
34
+ spec.add_dependency 'faraday', '~> 2.6.0'
35
35
  spec.add_dependency 'oj', '~> 3.11'
36
- # spec.add_dependency 'dry-configurable', '~> 0.12.1'
36
+ spec.add_dependency 'dry-configurable', '~> 0.16'
37
37
 
38
38
  # For more information and examples about making a new gem, checkout our
39
39
  # guide at: https://bundler.io/guides/creating_gem.html
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.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dinis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-21 00:00:00.000000000 Z
11
+ date: 2022-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 1.3.0
19
+ version: 2.6.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 1.3.0
26
+ version: 2.6.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: oj
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: dry-configurable
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.16'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.16'
41
55
  description: A gem that implements functions from the Moloni API available for its
42
56
  users.
43
57
  email:
@@ -64,12 +78,8 @@ files:
64
78
  - docker-compose.yml
65
79
  - lib/moloni_api.rb
66
80
  - lib/moloni_api/api.rb
67
- - lib/moloni_api/api/config.rb
68
- - lib/moloni_api/api/config/property.rb
69
- - lib/moloni_api/api/config/property_set.rb
70
81
  - lib/moloni_api/api_exceptions.rb
71
82
  - lib/moloni_api/client.rb
72
- - lib/moloni_api/configuration.rb
73
83
  - lib/moloni_api/constants.rb
74
84
  - lib/moloni_api/http_status_codes.rb
75
85
  - lib/moloni_api/models/product.rb
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MoloniApi
4
- class API
5
- class Config
6
- # Property objects provide an interface for configuration options
7
- class Property
8
- attr_reader :name, :default, :required
9
-
10
- def initialize(name, options)
11
- @name = name
12
- @default = options.fetch(:default, nil)
13
- @required = options.fetch(:required, nil)
14
- @options = options
15
- end
16
-
17
- # @api private
18
- def define_accessor_methods(properties)
19
- properties.define_reader_method(self, name, :public)
20
- properties.define_writer_method(self, "#{name}=", :public)
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,119 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'set'
4
-
5
- module MoloniApi
6
- class API
7
- class Config
8
- # Class responsible for storing configuration properties
9
- class PropertySet
10
- include Enumerable
11
-
12
- attr_reader :parent, :properties
13
-
14
- # Initialize an PropertySet
15
- #
16
- # @param [Object] parent
17
- # @param [Set] properties
18
- #
19
- # @return [undefined]
20
- #
21
- # @api private
22
- def initialize(parent = nil, properties = Set.new)
23
- @parent = parent
24
- @properties = properties
25
- @map = {}
26
- end
27
-
28
- # Iterate over properties
29
- #
30
- # @yield [property]
31
- #
32
- # @yieldparam [Property] property
33
- #
34
- # @return [self]
35
- #
36
- # @api public
37
- def each
38
- return to_enum unless block_given?
39
-
40
- @map.each { |name, property| yield property if name.is_a?(Symbol) }
41
- self
42
- end
43
-
44
- # Adds property to the set
45
- #
46
- # @example
47
- # properties_set << property
48
- #
49
- # @param [Property] property
50
- #
51
- # @return [self]
52
- #
53
- # @api public
54
- def <<(property)
55
- properties << property
56
- update_map(property.name, property.default)
57
- property.define_accessor_methods(self)
58
- self
59
- end
60
-
61
- # Access property by name
62
- #
63
- # @api public
64
- def [](name)
65
- @map[name]
66
- end
67
- alias fetch []
68
-
69
- # Set property value by name
70
- #
71
- # @api public
72
- def []=(name, property)
73
- update_map(name, property)
74
- end
75
-
76
- # Update map with index
77
- #
78
- # @api private
79
- def update_map(name, property)
80
- @map[name.to_sym] = @map[name.to_s.freeze] = property
81
- end
82
-
83
- # Convert properties to a hash of property names and
84
- # corresponding values
85
- #
86
- # @api public
87
- def to_hash
88
- properties.each_with_object({}) do |property, props|
89
- name = property.name
90
- props[name] = self[name]
91
- end
92
- end
93
-
94
- # Check if properties exist
95
- #
96
- # @api public
97
- def empty?
98
- @map.empty?
99
- end
100
-
101
- # @api private
102
- def define_reader_method(property, method_name, visibility)
103
- property_set = self
104
- parent.send(:define_method, method_name) { property_set[property.name] }
105
- parent.send(visibility, method_name)
106
- end
107
-
108
- # @api private
109
- def define_writer_method(property, method_name, visibility)
110
- property_set = self
111
- parent.send(:define_method, method_name) do |value|
112
- property_set[property.name] = value
113
- end
114
- parent.send(visibility, method_name)
115
- end
116
- end
117
- end
118
- end
119
- end
@@ -1,101 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'config/property'
4
- require_relative 'config/property_set'
5
-
6
- module MoloniApi
7
- class API
8
- # A base class for constructing api configuration
9
- class Config
10
- # Defines a property on an object's class or instance
11
- #
12
- # @example
13
- # class Configuration < Api::Config
14
- # property :adapter, default: :net_http
15
- # property :user, required: true
16
- # end
17
- #
18
- # @param [Symbol] name
19
- # the name of a property
20
- #
21
- # @param [#to_hash] options
22
- # the extra options
23
- #
24
- # @return [self]
25
- #
26
- # @api public
27
- def self.property(name, options = {})
28
- property_set << Property.new(name, options)
29
- update_subclasses(name, options)
30
- self
31
- end
32
-
33
- def self.update_subclasses(name, options)
34
- @subclasses.each { |klass| klass.property(name, options) } if defined?(@subclasses) && @subclasses
35
- end
36
-
37
- # Check if property is defined
38
- #
39
- # @param [Symbol] name
40
- # the name to check
41
- #
42
- # @return [Boolean]
43
- #
44
- # @api public
45
- def self.property?(name)
46
- property_set.include?(name)
47
- end
48
-
49
- class << self
50
- attr_reader :property_set
51
- end
52
-
53
- instance_variable_set('@property_set', PropertySet.new(self))
54
-
55
- def self.inherited(descendant)
56
- super
57
- (@subclasses ||= Set.new) << descendant
58
- descendant.instance_variable_set(
59
- '@property_set',
60
- PropertySet.new(descendant, property_set.properties.dup)
61
- )
62
- end
63
-
64
- def property_names
65
- self.class.property_set.properties.map(&:name)
66
- end
67
-
68
- def self.property_names
69
- property_set.properties.map(&:name)
70
- end
71
-
72
- # Fetch all the properties and their values
73
- #
74
- # @return [Hash[Symbol]]
75
- #
76
- # @api public
77
- def fetch(value = nil)
78
- if value
79
- self.class.property_set[value]
80
- else
81
- self.class.property_set.to_hash
82
- end
83
- end
84
-
85
- # Provide access to properties
86
- #
87
- # @example
88
- # config.call do |config|
89
- # config.adapter = :net_http
90
- # end
91
- #
92
- # @return [self]
93
- #
94
- # @api private
95
- def call(&block)
96
- block.call(self) if block_given?
97
- self
98
- end
99
- end
100
- end
101
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'api/config'
4
- require_relative 'version'
5
-
6
- module MoloniApi
7
- # Stores the configuration
8
- class Configuration < API::Config
9
- API_ENDPOINT = 'https://api.moloni.pt/sandbox/'
10
-
11
- property :follow_redirects, default: true
12
-
13
- # The api endpoint used to connect to MoloniApi if none is set
14
- # prd: https://api.moloni.pt/v1/
15
- # sandbox: https://api.moloni.pt/sandbox/
16
- property :endpoint, default: ENV['MOLONI_API_ENDPOINT'] || API_ENDPOINT
17
-
18
- # The value sent in the http header for 'User-Agent' if none is set
19
- property :user_agent, default: "MoloniApi API Ruby Gem #{MoloniApi::VERSION}"
20
-
21
- # By default uses the Faraday connection options if none is set
22
- property :connection_options, default: {}
23
-
24
- # By default display 30 resources
25
- property :per_page, default: 20
26
-
27
- # Add Faraday::RackBuilder to overwrite middleware
28
- property :stack
29
-
30
- property :api_access_token
31
- property :api_client_id, default: ENV['MOLONI_API_CLIENT_ID']
32
- property :api_client_secret, default: ENV['MOLONI_API_CLIENT_SECRET']
33
- end
34
- end