request_master 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in request_master.gemspec
4
+ gemspec
@@ -0,0 +1,36 @@
1
+ # RequestMaster
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/request_master`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'request_master'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install request_master
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/request_master.
36
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "request_master"
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
+ RequestMaster.database = {
14
+ host: '183.178.117.122', port: 5432,
15
+ user: 'eugene', password: 'demo3586',
16
+ database: 'wines', max_connects: 2
17
+ }
18
+ RequestMaster.set_check_defaults(44, 50)
19
+
20
+ @req_main = RequestMaster::ReqMaster.new
21
+
22
+ require "irb"
23
+ IRB.start
@@ -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,65 @@
1
+ require "request_master/version"
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+ require 'open-uri'
5
+ require 'ostruct'
6
+ require 'pg'
7
+ require 'sequel'
8
+
9
+ module RequestMaster
10
+
11
+ class << self
12
+ attr_accessor :reqs_db, :check_args, :details, :action_args
13
+ attr_accessor :alloc_args
14
+ def database= (database)
15
+ RequestMaster.reqs_db = database
16
+ require_relative 'request_master/models'
17
+ require_relative 'request_master/req_stats'
18
+ require_relative 'request_master/ip_checks'
19
+ require_relative 'request_master/ip_details'
20
+ require_relative 'request_master/actions'
21
+ end
22
+
23
+ def my_ip
24
+ open('http://icanhazip.com').read.strip
25
+ end
26
+
27
+ def set_check_defaults (dom_id, avg_freq)
28
+ check_args = OpenStruct.new(dom_id: dom_id, avg_freq: avg_freq)
29
+ RequestMaster.check_args = check_args
30
+ sputs "check defaults were set to #{check_args.to_h}"
31
+ check_args
32
+ end
33
+
34
+ def set_action (object, method, *args)
35
+ RequestMaster.action_args =
36
+ OpenStruct.new(
37
+ object: object, method_name: method, args: args
38
+ )
39
+ end
40
+
41
+ def action_divide= (lines)
42
+ RequestMaster.action_args.divider = lines
43
+ end
44
+
45
+ def set_reallocation (object, method, *args)
46
+ RequestMaster.alloc_args =
47
+ OpenStruct.new(
48
+ object: object, method_name: method, args: args
49
+ )
50
+ end
51
+
52
+ end
53
+
54
+ class ReqMaster
55
+ attr_accessor :domain, :action_count
56
+
57
+ def initialize
58
+ @database = RequestMaster.reqs_db
59
+ @action_count = 0
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+
@@ -0,0 +1,55 @@
1
+ require_relative '../request_master'
2
+ require_relative 'models'
3
+
4
+ module RequestMaster
5
+
6
+ class ReqMaster
7
+
8
+ def run_action
9
+ script = RequestMaster.action_args
10
+ result = script.object.send(script.method_name, *script.args)
11
+ @action_count += 1
12
+
13
+ unless script.divider.nil?
14
+
15
+ if @action_count % script.divider == 0
16
+ puts '------------------------'
17
+ end
18
+
19
+ end
20
+
21
+ @allocation -= 1
22
+ result
23
+ end
24
+
25
+ def allocate (first_run = false)
26
+ my_ip = RequestMaster.my_ip
27
+ checks_out = first_run ? check(my_ip) : false
28
+
29
+ until checks_out
30
+ script = RequestMaster.alloc_args
31
+ my_ip = script.object.send(script.method_name, *script.args)
32
+ checks_out = check(my_ip)
33
+
34
+ unless checks_out
35
+ sputs "failed because #{RequestMaster.details.to_h}"
36
+ end
37
+ end
38
+
39
+ sputs "ip #{my_ip} passed checks"
40
+ sputs ip_stats(my_ip, RequestMaster.check_args.dom_id).to_h.to_s
41
+
42
+ comp_alloc(my_ip)
43
+ sputs "allocation = #{@allocation}"
44
+ @allocation
45
+ end
46
+
47
+ alias_method :reallocate, :allocate
48
+
49
+ def alloc_exhaust?
50
+ @allocation == 0
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,49 @@
1
+ require_relative '../request_master'
2
+ require_relative 'models'
3
+
4
+ module RequestMaster
5
+
6
+ class ReqMaster
7
+
8
+ def check_status (ip, dom_id)
9
+ record = IpStatus[public_ip: ip, website: dom_id]
10
+ if record.nil?
11
+ true
12
+ else
13
+ status = record.status
14
+ if ['blocked', 'shielded'].include?(status)
15
+ RequestMaster.details = {
16
+ ip: ip,
17
+ dom_id: dom_id,
18
+ status: status
19
+ }
20
+ false
21
+ else
22
+ true
23
+ end
24
+ end
25
+ end
26
+
27
+ def check_frequency (ip, dom_id, avg_freq)
28
+ result = ip_stats(ip, dom_id).avg
29
+ if result.nil? || result <= avg_freq
30
+ true
31
+ else
32
+ RequestMaster.details = {
33
+ ip: ip,
34
+ dom_id: dom_id,
35
+ avg_freq: result
36
+ }
37
+ false
38
+ end
39
+ end
40
+
41
+ def check (ip, dom_id = RequestMaster.check_args.dom_id, avg_freq = RequestMaster.check_args.avg_freq)
42
+ check_status(ip, dom_id) && check_freq(ip, dom_id, avg_freq)
43
+ end
44
+
45
+ alias_method :check_freq, :check_frequency
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,18 @@
1
+ require_relative '../request_master'
2
+ require_relative 'models'
3
+
4
+ module RequestMaster
5
+
6
+ class ReqMaster
7
+
8
+ def compute_allowance (ip, dom_id = RequestMaster.check_args.dom_id, avg_freq = RequestMaster.check_args.avg_freq)
9
+
10
+ stats = ip_stats(ip, dom_id)
11
+ @allocation = avg_freq * (stats.days + 1) - stats.num
12
+ end
13
+
14
+ alias_method :comp_alloc, :compute_allowance
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,13 @@
1
+ require_relative '../request_master'
2
+
3
+ module RequestMaster
4
+
5
+ class BrowserRequest < Sequel::Model; end
6
+
7
+ class Website < Sequel::Model; end
8
+
9
+ class IpAddress < Sequel::Model; end
10
+
11
+ class IpStatus < Sequel::Model; end
12
+
13
+ end
@@ -0,0 +1,92 @@
1
+ require_relative '../request_master'
2
+ require_relative 'models'
3
+
4
+ module Kernel
5
+
6
+ def Numbers (array)
7
+ Numbers.new(array)
8
+ end
9
+
10
+ end
11
+
12
+ class Numbers
13
+
14
+ def initialize (array)
15
+ @array = array
16
+ end
17
+
18
+ def method_missing (method, *args, &block)
19
+ @array.send(method, *args, &block)
20
+ end
21
+
22
+ def sum
23
+ @array.inject(0) { |acc, num| acc + num }
24
+ end
25
+
26
+ def average
27
+ sum.to_f / @array.size
28
+ end
29
+
30
+ end
31
+
32
+ module RequestMaster
33
+
34
+ class ReqMaster
35
+
36
+ def dom_id (url)
37
+ Website[domain_url: url].id
38
+ end
39
+
40
+ def ip_stats (ip, dom_id)
41
+ requests = BrowserRequest.where(domain: dom_id, ip_address: ip)
42
+ num = requests.count
43
+
44
+ if num >= 2
45
+ time_span = (requests.last.time.to_date - requests.first.time.to_date + 1).to_f
46
+ avg = (num / time_span).to_f.round(1)
47
+ elsif num == 1
48
+ avg = num
49
+ time_span = 1.0
50
+ else
51
+ avg = nil
52
+ time_span = 0.0
53
+ end
54
+
55
+ OpenStruct.new(num: num, avg: avg, days: time_span.round(1))
56
+ end
57
+
58
+ def all_ip_stats (dom_id)
59
+
60
+ all_ips = IpAddress.select(:public_ip).to_a
61
+ all_ips.map!(&:public_ip)
62
+
63
+ all_avg = Numbers([])
64
+ all_total = Numbers([])
65
+
66
+ output = all_ips.inject([]) { |acc, ip|
67
+ stats = ip_stats(ip, dom_id)
68
+
69
+ if stats.num >= 50
70
+ all_total << stats.num
71
+ all_avg << stats.avg
72
+ acc << {ip => stats.avg}
73
+ else
74
+ acc
75
+ end
76
+ }
77
+
78
+ total_stats = {
79
+ min_total: all_total.min,
80
+ max_total: all_total.max,
81
+ avg_total: all_total.average.round,
82
+ min_avg: all_avg.min,
83
+ max_avg: all_avg.max,
84
+ avg_avg: all_avg.average.round
85
+ }
86
+
87
+ output << total_stats
88
+ end
89
+
90
+ end
91
+
92
+ end
@@ -0,0 +1,3 @@
1
+ module RequestMaster
2
+ VERSION = "0.1.8"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'request_master/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "request_master"
8
+ spec.version = RequestMaster::VERSION
9
+ spec.authors = ["StochasticSpr"]
10
+ spec.email = ["ejt.lai@gmail.com"]
11
+
12
+ spec.summary = 'Summary'
13
+ spec.description = 'A utility to control browser requests'
14
+ spec.homepage = 'http://www.placeholder.com'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.license = 'CC-BY-NC-ND-4.0'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+
26
+ spec.add_runtime_dependency 'pg', '~> 0.18'
27
+ spec.add_runtime_dependency 'sequel', '~> 4.32'
28
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: request_master
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.8
5
+ platform: ruby
6
+ authors:
7
+ - StochasticSpr
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-20 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pg
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.18'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.18'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sequel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.32'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.32'
69
+ description: A utility to control browser requests
70
+ email:
71
+ - ejt.lai@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".idea/misc.xml"
78
+ - ".idea/modules.xml"
79
+ - ".idea/request_master.iml"
80
+ - ".idea/workspace.xml"
81
+ - Gemfile
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/request_master.rb
87
+ - lib/request_master/actions.rb
88
+ - lib/request_master/ip_checks.rb
89
+ - lib/request_master/ip_details.rb
90
+ - lib/request_master/models.rb
91
+ - lib/request_master/req_stats.rb
92
+ - lib/request_master/version.rb
93
+ - request_master.gemspec
94
+ homepage: http://www.placeholder.com
95
+ licenses:
96
+ - CC-BY-NC-ND-4.0
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.6.2
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Summary
118
+ test_files: []