moloni_api 0.1.4 → 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: d4ce20574d9044023df19c350c588d21ff192530da34e92b20185c70cf9fffe3
4
- data.tar.gz: e40027e3826ea130df3e9f8ab435522958eb549232dc85c87e883c004c7daa93
3
+ metadata.gz: 6ec107d21be2536a63ab97fe0280cf8f7ad4db82759272e6f93a649224e869e3
4
+ data.tar.gz: 342955e233c5fb4ea13b0073f856701ce096b559d26229018efa3f17cbafee08
5
5
  SHA512:
6
- metadata.gz: 0c94ad5acf50457127342b81d97f463a4874ea417232ff25dc954b3b69da1fe8338f4a533af16baf422577f7b3949ccf7b84f79fc3da050b2a12084949a79e37
7
- data.tar.gz: 0f84659d5f1183ec4b4d75c0c6bf5eaa7ec1ffa2e86dfcaf2ef13c1bd4fc488646b2852df2b80c2764d5a5d4f65a35c2ac358a2222b591723ced38cc204241c3
6
+ metadata.gz: 52589d46b7dba75dbf5f15e938391c3fe65cf1571a35dd1029ff895e5ce2f61da690441d92829362e64840542f2d32c4aa2d108dac4d0136433ec0d8b5c1fb85
7
+ data.tar.gz: 998755fc207a05796be2664529ce81123fc141e4b26670e976600df7d36ab551ef9623bbde5a23be8c13438799c3c486de95d8d64b3d3599aade13ff4e613213
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'
@@ -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,21 +12,11 @@ module MoloniApi
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|
@@ -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).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
109
  end
130
110
  end
131
111
  # 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
 
@@ -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.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
@@ -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.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-27 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,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