api_recipes 2.8.1 → 2.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c9d0a5a4530af3af22b9b22c091002d316e1bf7065c612d1d74f18f3aa62e37d
4
- data.tar.gz: 5d322a843fa2db9cceb97cd9733b482ce17626342ad519ea036c0c821efe0d8b
3
+ metadata.gz: 3c1d3c91651ea71c01c7ae5368076aa3b693422eedfd373b5206f6072b396df3
4
+ data.tar.gz: 19a3ab41ca84d22efc0bb33918665bb3a67444c6ce070f53267519ca23334616
5
5
  SHA512:
6
- metadata.gz: 6ee0d8d0c2e4c52aafa513c8619f92c1ec6b5425cf06911fc48d8d18562855cd4d0aabe96bf68985323663bdb4cb461e56b353c06431e2430a3ef9d51ecd6021
7
- data.tar.gz: 4ef8593a855dd30fd21ee97642dcc5d832f7ec34218393131d1fe6758af1dbe4a8e5ad8d76f3612d289ef6e85d02a7640a65490f6b7dc103d0e32f43fbed2409
6
+ metadata.gz: 3cc0d5bd301ba17b76b1a19b4f0a7d99edef92953117e9f6c4a3b76c245e08b638b4f01b133b7429058de9236c3bc07900e94f7bb72ab8a75ddeb5ae31171f78
7
+ data.tar.gz: 15407e45843f2e83351c8559b129b33019797b25f568364dac2dc0c5ad0c90e952f9b60132354e9d3c65a464970d5296af962c8a8f45e07f10fe727dc245d25e
@@ -4,7 +4,7 @@ module ApiRecipes
4
4
  attr_accessor :name, :configs, :authorization, :basic_auth
5
5
  attr_reader :base_configs, :object
6
6
 
7
- BASE_CONFIGS_KEYS = [:protocol, :host, :port, :api_version, :timeout, :on_bad_code, :verify_with]
7
+ BASE_CONFIGS_KEYS = [:protocol, :host, :port, :api_version, :timeout, :on_bad_code, :mime_type, :verify_with]
8
8
 
9
9
  def initialize(name, configs, object)
10
10
  @name = name
@@ -1,12 +1,16 @@
1
+ require 'yaml'
2
+ require 'erb'
3
+ require 'logger'
4
+
1
5
  module ApiRecipes
2
6
  class Configuration
3
7
 
4
- attr_accessor :log_to, :log_level, :print_urls
8
+ attr_accessor :log_to, :log_level, :print_urls, :apis_files_paths
5
9
 
6
10
  def apis_configs=(configs = {})
7
11
  raise ArgumentError, 'apis_configs must be an Hash' unless configs.is_a? Hash
12
+
8
13
  @apis_configs = configs.deep_symbolize_keys
9
- ApiRecipes._aprcps_define_global_apis
10
14
  end
11
15
 
12
16
  def apis_configs
@@ -16,13 +20,41 @@ module ApiRecipes
16
20
  @apis_configs
17
21
  end
18
22
 
23
+ def apis_files_paths=(paths = [])
24
+ raise ArgumentError, 'apis_files_paths must be an Array' unless paths.is_a? Array
25
+
26
+ @apis_files_paths = paths
27
+ @apis_files_paths.each do |file_path|
28
+ template = ERB.new File.read File.expand_path(file_path)
29
+ data = begin
30
+ YAML.load(template.result(binding), aliases: true)
31
+ rescue ArgumentError
32
+ YAML.load(template.result binding)
33
+ end.deep_symbolize_keys
34
+ # Merge file contents into apis_configs
35
+ data.each do |api, params|
36
+ if apis_configs[api]
37
+ logger.debug "File at #{file_path} overrides config for '#{api}' API"
38
+ end
39
+ apis_configs[api] = params
40
+ end
41
+ end
42
+ end
43
+
44
+ def apis_files_paths
45
+ unless @apis_files_paths
46
+ @apis_files_paths = []
47
+ end
48
+ @apis_files_paths
49
+ end
50
+
19
51
  def logger=(logger)
20
52
  @logger = logger
21
53
  end
22
54
 
23
55
  def logger
24
56
  unless @logger
25
- log = ::Logger.new(log_to)
57
+ log = ::Logger.new(log_to || STDOUT)
26
58
  log.level = normalize_log_level
27
59
  log.progname = 'ApiRecipes'
28
60
  @logger = log
@@ -31,6 +63,10 @@ module ApiRecipes
31
63
  @logger
32
64
  end
33
65
 
66
+ def setup
67
+ ApiRecipes._aprcps_define_global_apis
68
+ end
69
+
34
70
  private
35
71
 
36
72
  # @private
@@ -42,7 +78,7 @@ module ApiRecipes
42
78
  when :error, ::Logger::ERROR, 'error' then ::Logger::ERROR
43
79
  when :fatal, ::Logger::FATAL, 'fatal' then ::Logger::FATAL
44
80
  else
45
- Logger::ERROR
81
+ ENV['LOG_LEVEL'] || Logger::DEBUG
46
82
  end
47
83
  end
48
84
  end
@@ -32,7 +32,12 @@ module ApiRecipes
32
32
  end
33
33
 
34
34
  def url
35
- @route.url
35
+ if @route
36
+ @route.url
37
+ else
38
+ ApiRecipes.logger.debug "No route configured for '#{api.name}#{absolute_path}': Is option 'route: no' present?"
39
+ nil
40
+ end
36
41
  end
37
42
 
38
43
  private
@@ -62,7 +67,6 @@ module ApiRecipes
62
67
  end
63
68
  end
64
69
  end
65
- # Route.new(api: @api, endpoint: self, name: route_name, attributes: route_attrs, req_pars: request_params).start_request &block
66
70
  end
67
71
 
68
72
  def ensure_route_does_not_clash(route_name)
@@ -12,7 +12,7 @@ module ApiRecipes
12
12
  return @data unless @data.nil?
13
13
 
14
14
  # TODO: rescue nil?
15
- @data = @original_response.parse
15
+ @data = @original_response.parse @attributes[:mime_type]
16
16
  end
17
17
 
18
18
  # Forward method calls to 'original' Response class
@@ -16,7 +16,7 @@ module ApiRecipes
16
16
  end
17
17
 
18
18
  def fill(object)
19
- data = @response.parse
19
+ data = @response.parse @attributes[:mime_type]
20
20
  if block_given?
21
21
  tap do
22
22
  try_to_fill object, data
@@ -10,7 +10,8 @@ module ApiRecipes
10
10
  timeout: 3,
11
11
  on_bad_code: 'raise',
12
12
  endpoints: {},
13
- verify_with: nil
13
+ verify_with: nil,
14
+ mime_type: nil
14
15
  }
15
16
 
16
17
  DEFAULT_ROUTE_ATTRIBUTES = {
@@ -20,6 +21,7 @@ module ApiRecipes
20
21
  ok_code: nil,
21
22
  timeout: DEFAULT[:timeout],
22
23
  on_bad_code: DEFAULT[:on_bad_code],
24
+ mime_type: DEFAULT[:mime_type],
23
25
  verify_with: DEFAULT[:verify_with]
24
26
  }
25
27
 
@@ -1,3 +1,3 @@
1
1
  module ApiRecipes
2
- VERSION = '2.8.1'.freeze
2
+ VERSION = '2.10.1'.freeze
3
3
  end
data/lib/api_recipes.rb CHANGED
@@ -65,6 +65,7 @@ module ApiRecipes
65
65
  def self.configure
66
66
  if block_given?
67
67
  yield(configuration)
68
+ configuration.setup
68
69
  else
69
70
  configuration
70
71
  end
@@ -145,6 +146,10 @@ module ApiRecipes
145
146
  global_api_configs
146
147
  end
147
148
  end
149
+
150
+ def self.logger
151
+ configuration.logger
152
+ end
148
153
  end
149
154
 
150
155
  # Monkey-patch URI so it can accept dashed hostnames like "web-service-1"
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_recipes
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.8.1
4
+ version: 2.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alessandro Verlato
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-02 00:00:00.000000000 Z
11
+ date: 2022-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.4.1
19
+ version: '4.4'
20
+ - - "<="
21
+ - !ruby/object:Gem::Version
22
+ version: '5.1'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '4.4'
30
+ - - "<="
25
31
  - !ruby/object:Gem::Version
26
- version: 4.4.1
32
+ version: '5.1'
27
33
  description:
28
34
  email:
29
35
  - averlato@gmail.com
@@ -55,14 +61,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
55
61
  requirements:
56
62
  - - ">="
57
63
  - !ruby/object:Gem::Version
58
- version: 2.5.0
64
+ version: 2.7.0
59
65
  required_rubygems_version: !ruby/object:Gem::Requirement
60
66
  requirements:
61
67
  - - ">="
62
68
  - !ruby/object:Gem::Version
63
69
  version: '0'
64
70
  requirements: []
65
- rubygems_version: 3.0.8
71
+ rubygems_version: 3.3.7
66
72
  signing_key:
67
73
  specification_version: 4
68
74
  summary: Consume HTTP APIs with style