moloni_api 0.1.4 → 0.2.1

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: d4ce20574d9044023df19c350c588d21ff192530da34e92b20185c70cf9fffe3
4
- data.tar.gz: e40027e3826ea130df3e9f8ab435522958eb549232dc85c87e883c004c7daa93
3
+ metadata.gz: 673d8af117bb955354df4f2e33b97db71748da48432fa4f3e5cb92d348316e91
4
+ data.tar.gz: d610ee570e45511f03ad0daf45e6ad07b908c74e49c4bcbbe45ef1069a09bee9
5
5
  SHA512:
6
- metadata.gz: 0c94ad5acf50457127342b81d97f463a4874ea417232ff25dc954b3b69da1fe8338f4a533af16baf422577f7b3949ccf7b84f79fc3da050b2a12084949a79e37
7
- data.tar.gz: 0f84659d5f1183ec4b4d75c0c6bf5eaa7ec1ffa2e86dfcaf2ef13c1bd4fc488646b2852df2b80c2764d5a5d4f65a35c2ac358a2222b591723ced38cc204241c3
6
+ metadata.gz: 519b7cbbdde21394bb9bad14e89a8689352baa80715abce5a6f12af4d3c19c97fb893fdc598796518218dc3420cc083be25f2be2947bdc5002adbbe4ee5c4203
7
+ data.tar.gz: 25d2849b0bff4f596ff2663044b71e06c08cbee836ed898448f49abdcb307ddfc577e71e1cca338bb3ac682bdd68de289e056823c7f42af015abb76b1bff9342
data/CHANGELOG.md CHANGED
@@ -0,0 +1,3 @@
1
+ * 0.2.1 - Compatibility restored by configuring inside the initialization method
2
+ * 0.2.0 - Breaking change: initialization using a configure block now - dry-configurable
3
+ * 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'
@@ -1,7 +1,7 @@
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'
@@ -12,20 +12,7 @@ module MoloniApi
12
12
  include ApiExceptions
13
13
  include Constants
14
14
  include HttpStatusCodes
15
-
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
15
+ include Dry::Configurable
29
16
 
30
17
  HTTP_STATUS_MAPPING = {
31
18
  HTTP_BAD_REQUEST_CODE => BadRequestError,
@@ -36,15 +23,37 @@ module MoloniApi
36
23
  'default' => ApiError
37
24
  }.freeze
38
25
 
26
+ setting :follow_redirects, default: true
27
+
28
+ # The api endpoint used to connect to MoloniApi if none is set
29
+ # prd: https://api.moloni.pt/v1/
30
+ # sandbox: https://api.moloni.pt/sandbox/
31
+ setting :endpoint, default: ENV['MOLONI_API_ENDPOINT'] || API_ENDPOINT, reader: true
32
+
33
+ # The value sent in the http header for 'User-Agent' if none is set
34
+ setting :user_agent, default: "MoloniApi API Ruby Gem #{MoloniApi::VERSION}"
35
+
36
+ # By default uses the Faraday connection options if none is set
37
+ setting :connection_options, default: {}
38
+
39
+ # By default display 30 resources
40
+ setting :per_page, default: 20
41
+
42
+ # Add Faraday::RackBuilder to overwrite middleware
43
+ setting :stack
44
+
45
+ setting :api_access_token, reader: true
46
+ setting :api_client_id, default: ENV['MOLONI_API_CLIENT_ID'] || API_CLIENT_ID, reader: true
47
+ setting :api_client_secret, default: ENV['MOLONI_API_CLIENT_SECRET'] || API_CLIENT_SECRET, reader: true
48
+
39
49
  # Create new API
40
50
  #
41
51
  # @api public
42
52
  def initialize(options = {}, &block)
43
- opts = MoloniApi.configuration.fetch.merge(options)
44
- @current_options = opts
45
-
46
- MoloniApi.configuration.property_names.each do |key|
47
- send("#{key}=", opts[key])
53
+ configure do |c|
54
+ options.each_key do |key|
55
+ c.send("#{key}=", options[key])
56
+ end
48
57
  end
49
58
 
50
59
  yield_or_eval(&block) if block_given?
@@ -65,19 +74,19 @@ module MoloniApi
65
74
  # provide your own logger
66
75
  logger = Logger.new $stderr
67
76
  logger.level = Logger::DEBUG
68
- @client ||= Faraday.new(@endpoint) do |client|
77
+ @client ||= Faraday.new(config.endpoint) do |client|
69
78
  client.request :url_encoded
70
79
  # client.request :json
71
80
  # client.response :json
72
81
  client.adapter Faraday.default_adapter
73
- client.headers['User-Agent'] = @user_agent
82
+ client.headers['User-Agent'] = config.user_agent
74
83
  client.response :logger, logger
75
84
  end
76
85
  end
77
86
 
78
87
  def request(http_method:, endpoint:, params: {}, query_params: {}, add_access_token: true)
79
88
  if add_access_token
80
- query_params[:access_token] = @api_access_token
89
+ query_params[:access_token] = self.api_access_token
81
90
  query_params[:json] = true
82
91
  end
83
92
  response = client.public_send(http_method, endpoint) do |req|
@@ -107,25 +116,6 @@ module MoloniApi
107
116
  def response_successful?(response)
108
117
  response.status == HTTP_OK_CODE
109
118
  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).nil?
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
119
  end
130
120
  end
131
121
  # mapi = MoloniApi.new(token: )
@@ -24,8 +24,8 @@ module MoloniApi
24
24
  endpoint: 'grant/',
25
25
  query_params: {
26
26
  grant_type: 'password',
27
- client_id: @api_client_id,
28
- client_secret: @api_client_secret,
27
+ client_id: self.api_client_id,
28
+ client_secret: self.api_client_secret,
29
29
  username: user_username,
30
30
  password: user_password
31
31
  },
@@ -43,14 +43,14 @@ module MoloniApi
43
43
  endpoint: 'grant/',
44
44
  query_params: {
45
45
  grant_type: 'refresh_token',
46
- client_id: @api_client_id,
47
- client_secret: @api_client_secret,
48
- 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
49
49
  },
50
50
  add_access_token: false
51
51
  )
52
52
  res = process_response(response)
53
- @api_access_token = res[:access_token]
53
+ self.api_access_token = res[:access_token]
54
54
  res
55
55
  end
56
56
 
@@ -178,7 +178,16 @@ module MoloniApi
178
178
  endpoint: 'customers/insert/',
179
179
  params: { company_id: company_id }.merge(customer_data)
180
180
  )
181
- process_response(response)
181
+ p_res = process_response(response)
182
+ case p_res
183
+ when Hash
184
+ p_res
185
+ else
186
+ {
187
+ valid: 0,
188
+ errors: p_res
189
+ }
190
+ end
182
191
  end
183
192
 
184
193
  def invoices_insert(company_id, invoice_params: {}, products: [])
@@ -3,6 +3,11 @@
3
3
  module MoloniApi
4
4
  # Constants
5
5
  module Constants
6
+ # API Defaults
7
+ API_ENDPOINT = 'https://api.moloni.pt/sandbox/'
8
+ API_CLIENT_ID = 'apigem'
9
+ API_CLIENT_SECRET = 'c9c9f4274658da2ad78b55a452894942898b5614'
10
+
6
11
  # Response headers
7
12
  RATELIMIT_REMAINING = 'X-RateLimit-Remaining'
8
13
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MoloniApi
4
- VERSION = '0.1.4'
4
+ VERSION = '0.2.1'
5
5
  end
data/lib/moloni_api.rb CHANGED
@@ -13,35 +13,6 @@ 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
18
  # @param [Hash] options
@@ -70,20 +41,15 @@ module MoloniApi
70
41
  def method_missing(method_name, *args, &block)
71
42
  if new.respond_to?(method_name)
72
43
  new.send(method_name, *args, &block)
73
- elsif configuration.respond_to?(method_name)
74
- MoloniApi.configuration.send(method_name, *args, &block)
75
44
  else
76
45
  super.respond_to_missing?
77
46
  end
78
47
  end
79
48
 
80
49
  def respond_to_missing?(method_name, include_private = false)
81
- new.respond_to?(method_name, include_private) ||
82
- configuration.respond_to?(method_name) ||
83
- super(method_name, include_private)
50
+ new.respond_to?(method_name, include_private) || super(method_name, include_private)
84
51
  end
85
52
  end
86
53
  end
87
54
 
88
55
  require_relative 'moloni_api/client'
89
- require_relative 'moloni_api/configuration'
data/moloni_api.gemspec CHANGED
@@ -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.4
4
+ version: 0.2.1
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-27 00:00:00.000000000 Z
11
+ date: 2022-11-07 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,36 +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
- API_CLIENT_ID = 'apigem'
11
- API_CLIENT_SECRET = 'c9c9f4274658da2ad78b55a452894942898b5614'
12
-
13
- property :follow_redirects, default: true
14
-
15
- # The api endpoint used to connect to MoloniApi if none is set
16
- # prd: https://api.moloni.pt/v1/
17
- # sandbox: https://api.moloni.pt/sandbox/
18
- property :endpoint, default: ENV['MOLONI_API_ENDPOINT'] || API_ENDPOINT
19
-
20
- # The value sent in the http header for 'User-Agent' if none is set
21
- property :user_agent, default: "MoloniApi API Ruby Gem #{MoloniApi::VERSION}"
22
-
23
- # By default uses the Faraday connection options if none is set
24
- property :connection_options, default: {}
25
-
26
- # By default display 30 resources
27
- property :per_page, default: 20
28
-
29
- # Add Faraday::RackBuilder to overwrite middleware
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
35
- end
36
- end