api_header_optimizer 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3b97eb0b9a2eca8956b285777447469840f688bfdf861b01d16de01456ba4104
4
+ data.tar.gz: 2401266c155e0f60f815f96e507fff86b969374af5d33292b253a1b8ed886949
5
+ SHA512:
6
+ metadata.gz: 075d21858be1267b3c6f5f628b617945cc7ce2d8396ab91a2743f6e83bc97f29ccb38eb04ff04423b666b91f60b9f02049d08988102bf160141add52ec3e61bd
7
+ data.tar.gz: 0fcdb7d46f331bb596466b4b75835e33ad4dc9d0d45aca32465960410ee95582fd1f8b0faf91a53c3387ae842d07fff74b5cf89b45ad851d68e605b5879bbb88
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in api_header_optimizer.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ api_header_optimizer (0.1.0)
5
+ net-http (~> 0.1.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ net-http (0.1.1)
11
+ net-protocol
12
+ uri
13
+ net-protocol (0.2.2)
14
+ timeout
15
+ rake (13.2.1)
16
+ timeout (0.4.1)
17
+ uri (0.13.1)
18
+
19
+ PLATFORMS
20
+ x86_64-linux
21
+
22
+ DEPENDENCIES
23
+ api_header_optimizer!
24
+ rake (~> 13.0)
25
+
26
+ BUNDLED WITH
27
+ 2.3.25
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Ziya Gökalp
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,29 @@
1
+ # ApiHeaderOptimizer
2
+
3
+ ## Installation
4
+
5
+ Install the gem and add to the application's Gemfile by executing:
6
+
7
+ $ bundle add api_header_optimizer
8
+
9
+ If bundler is not being used to manage dependencies, install the gem by executing:
10
+
11
+ $ gem install api_header_optimizer
12
+
13
+ ## Usage
14
+
15
+ This gem allows to extract the headers required in httparty requests.
16
+
17
+ ## Development
18
+
19
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
20
+
21
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
22
+
23
+ ## Contributing
24
+
25
+ Bug reports and pull requests are welcome on GitHub at https://github.com/zygklp/api_header_optimizer.
26
+
27
+ ## License
28
+
29
+ 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,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/api_header_optimizer/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "api_header_optimizer"
7
+ spec.version = ApiHeaderOptimizer::VERSION
8
+ spec.authors = ["Ziya Gökalp"]
9
+ spec.email = ["ziya93141@gmail.com"]
10
+
11
+ spec.summary = "A gem for optimizing API request headers."
12
+ spec.description = "This gem helps in determining the optimal set of headers for API requests by testing various header combinations."
13
+ spec.homepage = "https://github.com/zygklp/api_header_optimizer"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/zygklp/api_header_optimizer"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(__dir__) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency "net-http", "~> 0.1.0"
34
+
35
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
36
+ end
@@ -0,0 +1,8 @@
1
+ module ApiHeaderOptimizer
2
+ module Helpers
3
+ # Dizinin tüm alt kümelerini döndüren metot
4
+ def self.subsets(array)
5
+ (0..array.size).flat_map { |n| array.combination(n).to_a }
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,33 @@
1
+ require_relative 'tester'
2
+
3
+ module ApiHeaderOptimizer
4
+ class Optimizer
5
+ def initialize(uri, headers)
6
+ @uri = uri
7
+ @headers = headers
8
+ @tester = Tester.new(uri, headers)
9
+ end
10
+
11
+ def optimize
12
+ puts "Starting optimization..."
13
+ @tester.test_combinations
14
+
15
+ optimal_headers = find_optimal_headers
16
+ if optimal_headers.empty?
17
+ puts "No optimal headers found."
18
+ else
19
+ puts "Optimal headers found: #{optimal_headers}"
20
+ end
21
+ optimal_headers
22
+ end
23
+
24
+ private
25
+
26
+ def find_optimal_headers
27
+ # Burada en başarılı olan sonucu buluruz.
28
+ # Örneğin, 200 dönen ve en küçük header setini seçebiliriz.
29
+ best_result = @tester.results.min_by { |result| [result[:headers].size, result[:code].to_i] }
30
+ best_result ? best_result[:headers] : {}
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,50 @@
1
+ require 'net/http'
2
+ require_relative 'helpers'
3
+
4
+ module ApiHeaderOptimizer
5
+ class Tester
6
+ attr_reader :uri, :all_headers, :results
7
+
8
+ def initialize(uri, all_headers)
9
+ @uri = URI(uri)
10
+ @all_headers = all_headers
11
+ @results = []
12
+ end
13
+
14
+ # Tek bir header seti ile istek yapar ve sonucu kontrol eder
15
+ def test_header_set(header_set)
16
+ response = make_request(header_set)
17
+ return unless response # İstek başarısızsa geç
18
+ @results << { headers: header_set, code: response.code, body: response.body }
19
+ end
20
+
21
+ # Header kombinasyonlarını test eder
22
+ def test_combinations
23
+ # Tüm header kombinasyonlarını oluştur ve test et
24
+ header_keys = @all_headers.keys
25
+ subsets = ApiHeaderOptimizer::Helpers.subsets(header_keys)
26
+
27
+ subsets.each do |subset|
28
+ header_set = subset.to_h { |key| [key, @all_headers[key]] }
29
+ test_header_set(header_set)
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def make_request(headers)
36
+ http = Net::HTTP.new(@uri.host, @uri.port)
37
+ http.use_ssl = @uri.scheme == 'https'
38
+ request = Net::HTTP::Get.new(@uri.path)
39
+ headers.each { |key, value| request[key] = value }
40
+
41
+ begin
42
+ response = http.request(request)
43
+ return response if response.code.to_i.between?(200, 299) # Başarılı yanıtlar
44
+ rescue StandardError => e
45
+ puts "Request failed: #{e.message}"
46
+ return nil
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApiHeaderOptimizer
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "api_header_optimizer/version"
4
+ require_relative "api_header_optimizer/helpers"
5
+ require_relative "api_header_optimizer/optimizer"
6
+ require_relative "api_header_optimizer/tester"
7
+
8
+ module ApiHeaderOptimizer
9
+ class Error < StandardError; end
10
+ # Your code goes here...
11
+ end
@@ -0,0 +1,4 @@
1
+ module ApiHeaderOptimizer
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api_header_optimizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ziya Gökalp
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-09-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: net-http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.0
27
+ description: This gem helps in determining the optimal set of headers for API requests
28
+ by testing various header combinations.
29
+ email:
30
+ - ziya93141@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - api_header_optimizer.gemspec
41
+ - lib/api_header_optimizer.rb
42
+ - lib/api_header_optimizer/helpers.rb
43
+ - lib/api_header_optimizer/optimizer.rb
44
+ - lib/api_header_optimizer/tester.rb
45
+ - lib/api_header_optimizer/version.rb
46
+ - sig/api_header_optimizer.rbs
47
+ homepage: https://github.com/zygklp/api_header_optimizer
48
+ licenses:
49
+ - MIT
50
+ metadata:
51
+ allowed_push_host: https://rubygems.org
52
+ homepage_uri: https://github.com/zygklp/api_header_optimizer
53
+ source_code_uri: https://github.com/zygklp/api_header_optimizer
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 2.6.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.2.3
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: A gem for optimizing API request headers.
73
+ test_files: []