api_valve 0.0.1.beta.2 → 0.0.1.beta.3

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: 88db069feae42e9dc2fe3397891acfc31567a36b057a78c57d0a8ed2be567be7
4
- data.tar.gz: 29aba6e067ae80f88effcde5d03d048eff6137c34b11376ae0dfba9a0b47669b
3
+ metadata.gz: a274e16dfc06a57221ef6912fccb8fe4134deebb7a937ebb8c0e906658b1a0af
4
+ data.tar.gz: 56baccd5fc56ca6cff54ed2614627b62233a1559d059890188b83b8b8bf012ac
5
5
  SHA512:
6
- metadata.gz: 9c7d05c3e3788a426844ea4628c2fc8fa83def60bfba39e85f0138407c820ba81b942e947e3576e85d1fb27c62206698b6b60b517fdb08478cbeb202f541ab6f
7
- data.tar.gz: 3c57ae3efe82c65ff552084300bc7b0aa494789490ed132689f0c48c91a4b0920c937767dd41564ad7e782578bdb92c567ae71e4a231de296dde1756261a7f75
6
+ metadata.gz: a56cbce9c1dfa4192482e1f2a4619835e14d4cd6973ca0efe976398ccc070db7dd8e09c7f04f3ad168db7798a9ba1dacfe11957cd02c9812441086cbff29b086
7
+ data.tar.gz: b122661096332f3a5b7b92f9d69dd2c8d2c6f8ecd86427adbdadd25a5f99ccef9c7ac861a4465dcaf8b52a99af5e6273868af5b7907f6f2ee450778d02add5ce
@@ -1,23 +1,55 @@
1
- require 'byebug'
2
-
3
1
  module ApiValve
4
2
  class Proxy
5
3
  include ActiveSupport::Callbacks
6
4
 
7
5
  FORWARDER_OPTIONS = %w(endpoint request response permission_handler).freeze
8
6
 
7
+ class_attribute :permission_handler, :request, :response
8
+ self.permission_handler = Forwarder::PermissonHandler
9
+ self.request = Forwarder::Request
10
+ self.response = Forwarder::Response
11
+
9
12
  define_callbacks :call
10
13
 
11
14
  class << self
15
+ def from_config(file_name = nil)
16
+ file_name ||= name.underscore
17
+ path = find_config(file_name)
18
+ raise "Config not found for #{name.underscore}(.yml|.yml.erb) in #{ApiValve.config_paths.inspect}" unless path
19
+ yaml = File.read(path)
20
+ yaml = ERB.new(yaml, nil, '-').result if path.fnmatch? '*.erb'
21
+ from_yaml yaml
22
+ end
23
+
12
24
  def from_yaml(string)
13
25
  from_hash YAML.load(string) # rubocop:disable Security/YAMLLoad
14
26
  end
15
27
 
16
28
  def from_hash(config)
17
29
  config = config.with_indifferent_access
18
- forwarder = Forwarder.new(config.slice(*FORWARDER_OPTIONS))
30
+ forwarder = Forwarder.new(forwarder_config(config))
19
31
  new(forwarder).tap { |proxy| proxy.build_routes_from_config config }
20
32
  end
33
+
34
+ private
35
+
36
+ def find_config(file_name)
37
+ ApiValve.config_paths.each do |dir|
38
+ path = dir.join("#{file_name}.yml")
39
+ return path if path.exist?
40
+ path = dir.join("#{file_name}.yml.erb")
41
+ return path if path.exist?
42
+ end
43
+ nil
44
+ end
45
+
46
+ def forwarder_config(config)
47
+ {
48
+ permission_handler: {klass: permission_handler},
49
+ request: {klass: request},
50
+ response: {klass: response}
51
+ }.with_indifferent_access.deep_merge config.slice(*FORWARDER_OPTIONS)
52
+ end
21
53
  end
22
54
 
23
55
  attr_reader :request, :forwarder, :router
data/lib/api_valve.rb CHANGED
@@ -7,6 +7,7 @@ require 'active_support/core_ext/module'
7
7
  require 'active_support/json'
8
8
  require 'active_support/rescuable'
9
9
  require 'benchmark'
10
+ require 'byebug'
10
11
  require 'faraday'
11
12
  require 'multi_json'
12
13
  require 'logger'
@@ -35,6 +36,10 @@ module ApiValve
35
36
  false
36
37
  end
37
38
 
39
+ config_accessor :config_paths do
40
+ []
41
+ end
42
+
38
43
  # :nocov:
39
44
  def self.configure
40
45
  yield config
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_valve
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.beta.2
4
+ version: 0.0.1.beta.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - mkon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-22 00:00:00.000000000 Z
11
+ date: 2018-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport