api_proxy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 26ed0912c8057e8afdda8431af9e69d2fca70ca00b98cf31a19d8677aacab2bc
4
+ data.tar.gz: 47351d81f93ffa260fa0ce8650b28658995d3d7e5d753627d9ddc736e88df59f
5
+ SHA512:
6
+ metadata.gz: 18ae4378263005c2226c7736ce05a01cf1b83fd3c817ed9ff68e1cfb02210d17b5e072c58f73e750a4e1029118770fa9a21337d13345ec4dce47cea4e0be3aa7
7
+ data.tar.gz: c60fc9dbaa376ab96570323df1c58bc96b895dfd302b99a1e56ebfc5675ec8722adc8f3a4d3c0fd0794d4dd1251456d1c6ae24ea16c08bc8087db1467054f3c3
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.overcommit.yml ADDED
@@ -0,0 +1,56 @@
1
+ CommitMsg:
2
+ CapitalizedSubject:
3
+ enabled: false
4
+
5
+ EmptyMessage:
6
+ enabled: false
7
+
8
+ TrailingPeriod:
9
+ enabled: true
10
+
11
+ TextWidth:
12
+ enabled: false
13
+
14
+ PreCommit:
15
+ ALL:
16
+ on_warn: fail
17
+
18
+ AuthorEmail:
19
+ enabled: true
20
+
21
+ AuthorName:
22
+ enabled: true
23
+
24
+ MergeConflicts:
25
+ enabled: true
26
+
27
+ BundleCheck:
28
+ enabled: true
29
+
30
+ BundleAudit:
31
+ enabled: true
32
+
33
+ Reek:
34
+ enabled: true
35
+ description: 'Analyze with Reek'
36
+ required_executable: 'reek'
37
+ command: ['bundle', 'exec', 'reek']
38
+ install_command: 'gem install reek'
39
+ include: &1
40
+ - '**/*.gemspec'
41
+ - 'app/**/*.rb'
42
+ - 'config/**/*.rb'
43
+ - 'spec/**/*.rb'
44
+ - 'lib/**/*.rb'
45
+ - 'lib/**/*.rake'
46
+ - '**/Gemfile'
47
+ - '**/Rakefile'
48
+
49
+ RuboCop:
50
+ enabled: true
51
+ description: 'Analyze with RuboCop'
52
+ required_executable: 'rubocop'
53
+ command: ['bundle', 'exec', 'rubocop']
54
+ flags: ['--format=emacs', '--force-exclusion', '--display-cop-names']
55
+ install_command: 'gem install rubocop'
56
+ include: *1
data/.reek.yml ADDED
@@ -0,0 +1,24 @@
1
+ ---
2
+
3
+ ### Generic smell configuration
4
+
5
+ detectors:
6
+ # You can disable smells completely
7
+ IrresponsibleModule:
8
+ enabled: false
9
+
10
+ FeatureEnvy:
11
+ exclude:
12
+ - "ApiProxy::Middleware#call"
13
+
14
+ Attribute:
15
+ exclude:
16
+ - "ApiProxy::Config"
17
+
18
+ TooManyInstanceVariables:
19
+ exclude:
20
+ - "ApiProxy::Config"
21
+
22
+ TooManyStatements:
23
+ exclude:
24
+ - "ApiProxy::Config"
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,55 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5.1
3
+
4
+ Exclude:
5
+ - "Guardfile"
6
+ - "Rakefile"
7
+ - "bin/**/*"
8
+ - "spec/dummy/**/*"
9
+
10
+ ##################### Styles ##################################
11
+
12
+ Style/Documentation:
13
+ Enabled: false
14
+
15
+ Style/SymbolArray:
16
+ Enabled: false
17
+
18
+ Style/FrozenStringLiteralComment:
19
+ Enabled: false
20
+
21
+ Style/ClassAndModuleChildren:
22
+ Exclude:
23
+ - "app/controllers/*_controller.rb"
24
+ - "app/controllers/**/*_controller.rb"
25
+
26
+ #################### Lint ##################################
27
+
28
+ Lint/AmbiguousBlockAssociation:
29
+ Enabled: false
30
+
31
+ ##################### Metrics ##################################
32
+
33
+ Metrics/LineLength:
34
+ Max: 110
35
+
36
+ Metrics/ClassLength:
37
+ Max: 200
38
+
39
+ Metrics/ModuleLength:
40
+ Max: 200
41
+ Exclude:
42
+ - "**/*_spec.rb"
43
+
44
+ Metrics/BlockLength:
45
+ Max: 50
46
+ Exclude:
47
+ - "**/*_spec.rb"
48
+
49
+ ##################### Rails ##################################
50
+
51
+ Rails:
52
+ Enabled: true
53
+
54
+ Rails/SkipsModelValidations:
55
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.1
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.4
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in api_proxy.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,159 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ api_proxy (0.1.0)
5
+ activesupport (>= 4.0)
6
+ api_signature (~> 0.1.2)
7
+ httparty (~> 0.15.7)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (5.2.0)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 0.7, < 2)
15
+ minitest (~> 5.1)
16
+ tzinfo (~> 1.1)
17
+ addressable (2.5.2)
18
+ public_suffix (>= 2.0.2, < 4.0)
19
+ api_signature (0.1.2)
20
+ activesupport (>= 4.0)
21
+ rack (>= 1.6)
22
+ ast (2.4.0)
23
+ axiom-types (0.1.1)
24
+ descendants_tracker (~> 0.0.4)
25
+ ice_nine (~> 0.11.0)
26
+ thread_safe (~> 0.3, >= 0.3.1)
27
+ bundler-audit (0.6.0)
28
+ bundler (~> 1.2)
29
+ thor (~> 0.18)
30
+ childprocess (0.9.0)
31
+ ffi (~> 1.0, >= 1.0.11)
32
+ codeclimate-engine-rb (0.4.1)
33
+ virtus (~> 1.0)
34
+ coderay (1.1.2)
35
+ coercible (1.0.0)
36
+ descendants_tracker (~> 0.0.1)
37
+ concurrent-ruby (1.0.5)
38
+ crack (0.4.3)
39
+ safe_yaml (~> 1.0.0)
40
+ descendants_tracker (0.0.4)
41
+ thread_safe (~> 0.3, >= 0.3.1)
42
+ diff-lcs (1.3)
43
+ equalizer (0.0.11)
44
+ ffi (1.9.25)
45
+ formatador (0.2.5)
46
+ guard (2.14.2)
47
+ formatador (>= 0.2.4)
48
+ listen (>= 2.7, < 4.0)
49
+ lumberjack (>= 1.0.12, < 2.0)
50
+ nenv (~> 0.1)
51
+ notiffany (~> 0.0)
52
+ pry (>= 0.9.12)
53
+ shellany (~> 0.0)
54
+ thor (>= 0.18.1)
55
+ guard-compat (1.2.1)
56
+ guard-rspec (4.7.3)
57
+ guard (~> 2.1)
58
+ guard-compat (~> 1.1)
59
+ rspec (>= 2.99.0, < 4.0)
60
+ hashdiff (0.3.7)
61
+ httparty (0.15.7)
62
+ multi_xml (>= 0.5.2)
63
+ i18n (1.0.1)
64
+ concurrent-ruby (~> 1.0)
65
+ ice_nine (0.11.2)
66
+ iniparse (1.4.4)
67
+ jaro_winkler (1.5.1)
68
+ kwalify (0.7.2)
69
+ listen (3.1.5)
70
+ rb-fsevent (~> 0.9, >= 0.9.4)
71
+ rb-inotify (~> 0.9, >= 0.9.7)
72
+ ruby_dep (~> 1.2)
73
+ lumberjack (1.0.13)
74
+ method_source (0.9.0)
75
+ minitest (5.11.3)
76
+ multi_xml (0.6.0)
77
+ nenv (0.3.0)
78
+ notiffany (0.1.1)
79
+ nenv (~> 0.1)
80
+ shellany (~> 0.0)
81
+ overcommit (0.45.0)
82
+ childprocess (~> 0.6, >= 0.6.3)
83
+ iniparse (~> 1.4)
84
+ parallel (1.12.1)
85
+ parser (2.5.1.2)
86
+ ast (~> 2.4.0)
87
+ powerpack (0.1.2)
88
+ pry (0.11.3)
89
+ coderay (~> 1.1.0)
90
+ method_source (~> 0.9.0)
91
+ public_suffix (3.0.2)
92
+ rack (2.0.5)
93
+ rainbow (3.0.0)
94
+ rake (10.5.0)
95
+ rb-fsevent (0.10.3)
96
+ rb-inotify (0.9.10)
97
+ ffi (>= 0.5.0, < 2)
98
+ reek (5.0.2)
99
+ codeclimate-engine-rb (~> 0.4.0)
100
+ kwalify (~> 0.7.0)
101
+ parser (>= 2.5.0.0, < 2.6, != 2.5.1.1)
102
+ rainbow (>= 2.0, < 4.0)
103
+ rspec (3.7.0)
104
+ rspec-core (~> 3.7.0)
105
+ rspec-expectations (~> 3.7.0)
106
+ rspec-mocks (~> 3.7.0)
107
+ rspec-core (3.7.1)
108
+ rspec-support (~> 3.7.0)
109
+ rspec-expectations (3.7.0)
110
+ diff-lcs (>= 1.2.0, < 2.0)
111
+ rspec-support (~> 3.7.0)
112
+ rspec-mocks (3.7.0)
113
+ diff-lcs (>= 1.2.0, < 2.0)
114
+ rspec-support (~> 3.7.0)
115
+ rspec-support (3.7.1)
116
+ rubocop (0.58.2)
117
+ jaro_winkler (~> 1.5.1)
118
+ parallel (~> 1.10)
119
+ parser (>= 2.5, != 2.5.1.1)
120
+ powerpack (~> 0.1)
121
+ rainbow (>= 2.2.2, < 4.0)
122
+ ruby-progressbar (~> 1.7)
123
+ unicode-display_width (~> 1.0, >= 1.0.1)
124
+ ruby-progressbar (1.9.0)
125
+ ruby_dep (1.5.0)
126
+ safe_yaml (1.0.4)
127
+ shellany (0.0.1)
128
+ thor (0.20.0)
129
+ thread_safe (0.3.6)
130
+ tzinfo (1.2.5)
131
+ thread_safe (~> 0.1)
132
+ unicode-display_width (1.4.0)
133
+ virtus (1.0.5)
134
+ axiom-types (~> 0.1)
135
+ coercible (~> 1.0)
136
+ descendants_tracker (~> 0.0, >= 0.0.3)
137
+ equalizer (~> 0.0, >= 0.0.9)
138
+ webmock (3.4.2)
139
+ addressable (>= 2.3.6)
140
+ crack (>= 0.3.2)
141
+ hashdiff
142
+
143
+ PLATFORMS
144
+ ruby
145
+
146
+ DEPENDENCIES
147
+ api_proxy!
148
+ bundler (~> 1.16)
149
+ bundler-audit (~> 0.6.0)
150
+ guard-rspec (~> 4.7, >= 4.7.3)
151
+ overcommit
152
+ rake (~> 10.0)
153
+ reek
154
+ rspec (~> 3.0)
155
+ rubocop
156
+ webmock (~> 3.4, >= 3.4.2)
157
+
158
+ BUNDLED WITH
159
+ 1.16.2
data/Guardfile ADDED
@@ -0,0 +1,70 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ # Note: The cmd option is now required due to the increasing number of ways
19
+ # rspec may be run, below are examples of the most common uses.
20
+ # * bundler: 'bundle exec rspec'
21
+ # * bundler binstubs: 'bin/rspec'
22
+ # * spring: 'bin/rspec' (This will use spring if running and you have
23
+ # installed the spring binstubs per the docs)
24
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
25
+ # * 'just' rspec: 'rspec'
26
+
27
+ guard :rspec, cmd: "bundle exec rspec" do
28
+ require "guard/rspec/dsl"
29
+ dsl = Guard::RSpec::Dsl.new(self)
30
+
31
+ # Feel free to open issues for suggestions and improvements
32
+
33
+ # RSpec files
34
+ rspec = dsl.rspec
35
+ watch(rspec.spec_helper) { rspec.spec_dir }
36
+ watch(rspec.spec_support) { rspec.spec_dir }
37
+ watch(rspec.spec_files)
38
+
39
+ # Ruby files
40
+ ruby = dsl.ruby
41
+ dsl.watch_spec_files_for(ruby.lib_files)
42
+
43
+ # Rails files
44
+ rails = dsl.rails(view_extensions: %w(erb haml slim))
45
+ dsl.watch_spec_files_for(rails.app_files)
46
+ dsl.watch_spec_files_for(rails.views)
47
+
48
+ watch(rails.controllers) do |m|
49
+ [
50
+ rspec.spec.call("routing/#{m[1]}_routing"),
51
+ rspec.spec.call("controllers/#{m[1]}_controller"),
52
+ rspec.spec.call("acceptance/#{m[1]}")
53
+ ]
54
+ end
55
+
56
+ # Rails config changes
57
+ watch(rails.spec_helper) { rspec.spec_dir }
58
+ watch(rails.routes) { "#{rspec.spec_dir}/routing" }
59
+ watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
60
+
61
+ # Capybara features specs
62
+ watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
63
+ watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
64
+
65
+ # Turnip features and steps
66
+ watch(%r{^spec/acceptance/(.+)\.feature$})
67
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
68
+ Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
69
+ end
70
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Igor Malinovskiy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # ApiProxy
2
+
3
+ This gem is a simple forwarding proxy for api requests
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'api_proxy'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install api_proxy
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ # config/initializers/api_proxy.rb
25
+
26
+ ApiProxy.configure(:my_namespace) do |config|
27
+ config.api_key = ENV['SERVICE_API_KEY']
28
+ config.api_secret = ENV['SERVICE_API_SECRET']
29
+
30
+ config.url_scheme = 'http'
31
+ config.api_host = 'localhost'
32
+ config.api_port = 3000
33
+
34
+ config.api_prefix = '/api/v1/'
35
+
36
+ config.request_starts_with = '/_some_key'
37
+ end
38
+ ```
39
+
40
+ ```ruby
41
+ # config/application.rb
42
+
43
+ config.middleware.use ApiProxy::Middleware, :my_namespace
44
+ ```
45
+
46
+ Then all requests, starting with `/_some_key` will be forwarded to your service
47
+
48
+ `GET localhost:3010/_some_key/orders` => `GET localhost:3000/api/v1/orders`
49
+
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/api_proxy.
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/api_proxy.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'api_proxy/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'api_proxy'
9
+ spec.version = ApiProxy::VERSION
10
+ spec.authors = ['Igor Malinovskiy']
11
+ spec.email = ['igor.malinovskiy@netfixllc.org']
12
+
13
+ spec.summary = 'Proxy for tickets service'
14
+ spec.homepage = 'https://github.com/psyipm/api_proxy'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.16'
25
+ spec.add_development_dependency 'bundler-audit', '~> 0.6.0'
26
+ spec.add_development_dependency 'guard-rspec', '~> 4.7', '>= 4.7.3'
27
+ spec.add_development_dependency 'overcommit'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ spec.add_development_dependency 'reek'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ spec.add_development_dependency 'rubocop'
32
+ spec.add_development_dependency 'webmock', '~> 3.4', '>= 3.4.2'
33
+
34
+ spec.add_dependency 'activesupport', '>= 4.0'
35
+ spec.add_dependency 'api_signature', '~> 0.1.2'
36
+ spec.add_dependency 'httparty', '~> 0.15.7'
37
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "api_proxy"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApiProxy
4
+ class Config
5
+ attr_accessor :api_key, :api_secret, :url_scheme, :api_host, :api_port, :api_prefix, :request_starts_with
6
+
7
+ def initialize
8
+ load_defaults
9
+ end
10
+
11
+ def load_defaults
12
+ @api_key = 'key'
13
+ @api_secret = 'secret'
14
+
15
+ @url_scheme = 'http'
16
+ @api_host = 'localhost'
17
+ @api_port = 3000
18
+
19
+ @api_prefix = '/api/v1/'
20
+ @request_starts_with = '/_ts'
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApiProxy
4
+ class Middleware
5
+ def initialize(app, namespace = :default)
6
+ @app = app
7
+ @config = ApiProxy.configuration(namespace)
8
+ end
9
+
10
+ def call(env)
11
+ return @app.call(env) unless env['REQUEST_PATH'].start_with?(@config.request_starts_with)
12
+
13
+ builder = RequestOptionsBuilder.new(env, @config)
14
+ request = ApiProxy::Request.new(builder)
15
+
16
+ response = request.result
17
+
18
+ Rack::Response.new(response.to_s, response.code, request.headers)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'httparty'
4
+
5
+ module ApiProxy
6
+ class Request
7
+ include ::HTTParty
8
+
9
+ attr_reader :builder
10
+
11
+ delegate :url, :options, :request_method, to: :builder
12
+
13
+ ALLOWED_HEADERS = [
14
+ 'content-type',
15
+ 'etag',
16
+ 'cache-control',
17
+ 'content-length',
18
+ 'content-disposition',
19
+ 'content-transfer-encoding'
20
+ ].freeze
21
+
22
+ def initialize(request_options_builder)
23
+ @builder = request_options_builder
24
+ end
25
+
26
+ def result
27
+ @result ||= perform_request
28
+ end
29
+
30
+ def headers
31
+ result.headers.select { |key, _value| ALLOWED_HEADERS.include?(key) }
32
+ end
33
+
34
+ private
35
+
36
+ def perform_request
37
+ self.class.send(request_method, url, options)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'api_signature'
4
+
5
+ module ApiProxy
6
+ class RequestOptionsBuilder
7
+ attr_reader :env, :config
8
+
9
+ delegate :api_key,
10
+ :api_secret,
11
+ :api_host,
12
+ :api_port,
13
+ :api_prefix,
14
+ :url_scheme,
15
+ :request_starts_with, to: :config
16
+
17
+ def initialize(env, config)
18
+ @env = env
19
+ @config = config
20
+ end
21
+
22
+ def options
23
+ { headers: signature_builder.headers, format: :json }
24
+ end
25
+
26
+ def url
27
+ URI::Generic.build(scheme: url_scheme, host: api_host, port: api_port, path: path)
28
+ end
29
+
30
+ def request_method
31
+ env['REQUEST_METHOD'].to_s.downcase
32
+ end
33
+
34
+ private
35
+
36
+ def signature_builder
37
+ @signature_builder ||= ApiSignature::Builder.new(request_params)
38
+ end
39
+
40
+ def request_params
41
+ {
42
+ access_key: api_key,
43
+ secret: api_secret,
44
+ request_method: request_method,
45
+ scheme: url_scheme,
46
+ host: api_host,
47
+ port: api_port,
48
+ path: path
49
+ }
50
+ end
51
+
52
+ def path
53
+ request_path = env['REQUEST_PATH'].gsub(request_starts_with, '')
54
+
55
+ File.join(api_prefix, request_path)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApiProxy
4
+ VERSION = '0.1.0'
5
+ end
data/lib/api_proxy.rb ADDED
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'api_proxy/version'
4
+ require 'active_support/time'
5
+ require 'active_support/time_with_zone'
6
+
7
+ module ApiProxy
8
+ autoload :Config, 'api_proxy/config'
9
+ autoload :Middleware, 'api_proxy/middleware'
10
+ autoload :RequestOptionsBuilder, 'api_proxy/request_options_builder'
11
+ autoload :Request, 'api_proxy/request'
12
+
13
+ def self.configuration(namespace)
14
+ @configuration ||= {}
15
+ @configuration[namespace] ||= Config.new
16
+ end
17
+
18
+ # @example
19
+ # ApiProxy.setup(:namespace) do |config|
20
+ # config.api_host = '192.168.99.100'
21
+ # end
22
+ #
23
+ def self.setup(namespace = :default)
24
+ yield(configuration(namespace))
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,246 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api_proxy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Igor Malinovskiy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler-audit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.6.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.6.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.7'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 4.7.3
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '4.7'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 4.7.3
61
+ - !ruby/object:Gem::Dependency
62
+ name: overcommit
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '10.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '10.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: reek
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rspec
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '3.0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '3.0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: rubocop
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: webmock
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - "~>"
136
+ - !ruby/object:Gem::Version
137
+ version: '3.4'
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: 3.4.2
141
+ type: :development
142
+ prerelease: false
143
+ version_requirements: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - "~>"
146
+ - !ruby/object:Gem::Version
147
+ version: '3.4'
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: 3.4.2
151
+ - !ruby/object:Gem::Dependency
152
+ name: activesupport
153
+ requirement: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '4.0'
158
+ type: :runtime
159
+ prerelease: false
160
+ version_requirements: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '4.0'
165
+ - !ruby/object:Gem::Dependency
166
+ name: api_signature
167
+ requirement: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - "~>"
170
+ - !ruby/object:Gem::Version
171
+ version: 0.1.2
172
+ type: :runtime
173
+ prerelease: false
174
+ version_requirements: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - "~>"
177
+ - !ruby/object:Gem::Version
178
+ version: 0.1.2
179
+ - !ruby/object:Gem::Dependency
180
+ name: httparty
181
+ requirement: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - "~>"
184
+ - !ruby/object:Gem::Version
185
+ version: 0.15.7
186
+ type: :runtime
187
+ prerelease: false
188
+ version_requirements: !ruby/object:Gem::Requirement
189
+ requirements:
190
+ - - "~>"
191
+ - !ruby/object:Gem::Version
192
+ version: 0.15.7
193
+ description:
194
+ email:
195
+ - igor.malinovskiy@netfixllc.org
196
+ executables: []
197
+ extensions: []
198
+ extra_rdoc_files: []
199
+ files:
200
+ - ".gitignore"
201
+ - ".overcommit.yml"
202
+ - ".reek.yml"
203
+ - ".rspec"
204
+ - ".rubocop.yml"
205
+ - ".ruby-version"
206
+ - ".travis.yml"
207
+ - Gemfile
208
+ - Gemfile.lock
209
+ - Guardfile
210
+ - LICENSE.txt
211
+ - README.md
212
+ - Rakefile
213
+ - api_proxy.gemspec
214
+ - bin/console
215
+ - bin/setup
216
+ - lib/api_proxy.rb
217
+ - lib/api_proxy/config.rb
218
+ - lib/api_proxy/middleware.rb
219
+ - lib/api_proxy/request.rb
220
+ - lib/api_proxy/request_options_builder.rb
221
+ - lib/api_proxy/version.rb
222
+ homepage: https://github.com/psyipm/api_proxy
223
+ licenses:
224
+ - MIT
225
+ metadata: {}
226
+ post_install_message:
227
+ rdoc_options: []
228
+ require_paths:
229
+ - lib
230
+ required_ruby_version: !ruby/object:Gem::Requirement
231
+ requirements:
232
+ - - ">="
233
+ - !ruby/object:Gem::Version
234
+ version: '0'
235
+ required_rubygems_version: !ruby/object:Gem::Requirement
236
+ requirements:
237
+ - - ">="
238
+ - !ruby/object:Gem::Version
239
+ version: '0'
240
+ requirements: []
241
+ rubyforge_project:
242
+ rubygems_version: 2.7.6
243
+ signing_key:
244
+ specification_version: 4
245
+ summary: Proxy for tickets service
246
+ test_files: []