ballad 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5f39455ea4b6e47f9c08898b5a0021090b44bf80
4
+ data.tar.gz: 2e5c6855a00270ee4cd03654a68e06664e527ef7
5
+ SHA512:
6
+ metadata.gz: 48d94716a82925e9fff5399b35d2ccf5beab6060eeabf6eef8d083baf0d0a83cf5a59ea216c90dc2a721d9d7c4a0ffd6a42402bd799ade0a0d25bc9c1871e754
7
+ data.tar.gz: 1e1366f920a5f4e88af9fc86fc352cfed9c125481774c86b12992378a081c6c888bcf46a675fe18888f14823158716ffcace864106bd4bc46c1f5b99e8bba1ca
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.7
5
+ - 2.2.3
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 ksss
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.
@@ -0,0 +1,49 @@
1
+ # Ballad
2
+
3
+ HTTP edit line tool.
4
+
5
+ Ballad send HTTP request to url input by stdin.
6
+
7
+ And edit input line and output to stdout by HTTP response.
8
+
9
+ ## Usage
10
+
11
+ ```shell
12
+ $ cat data.txt
13
+ https://www.google.co.jp/
14
+ https://www.google.co.jp/a
15
+ https://www.google.co.jp/b
16
+
17
+ $ cat data.txt | ballad
18
+ 200 https://www.google.co.jp/
19
+ 301 https://www.google.co.jp/a
20
+ 404 https://www.google.co.jp/b
21
+
22
+ $ cat data.txt | ballad | grep 200 | awk '{print $2}'
23
+ https://www.google.co.jp/
24
+
25
+ ```
26
+
27
+ ## Installation
28
+
29
+ ```shell
30
+ $ gem install ballad
31
+ ```
32
+
33
+ ## Options
34
+
35
+ ### -s
36
+
37
+ Edit line by HTTP response status code (default on).
38
+
39
+ ### -j
40
+
41
+ Number of concurrent to HTTP request(using Thread)
42
+
43
+ ### -m
44
+
45
+ HTTP request method (default head)
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'etc'
5
+ require 'ballad'
6
+
7
+ $stdin.sync = true
8
+ $stdout.sync = true
9
+ Thread.abort_on_exception = true
10
+ Signal.trap(:SIGINT) do
11
+ exit 2
12
+ end
13
+
14
+ Ballad::Cli.new.parse.run
@@ -0,0 +1,23 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'ballad/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ballad"
7
+ spec.version = Ballad::VERSION
8
+ spec.authors = ["ksss"]
9
+ spec.email = ["co000ri@gmail.com"]
10
+
11
+ spec.summary = %q{HTTP response edit by line}
12
+ spec.description = %q{HTTP response edit by line}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "bin"
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,2 @@
1
+ require "ballad/version"
2
+ require "ballad/cli"
@@ -0,0 +1,138 @@
1
+ require 'net/http'
2
+ require 'thread'
3
+ require 'etc'
4
+
5
+ module Ballad
6
+ class Cli
7
+ class Option < Struct.new(:args, :concurrency, :http_method)
8
+ end
9
+
10
+ def initialize
11
+ @opt = Ballad::Cli::Option.new
12
+ @i_p_q = Queue.new
13
+ @i_r_q = Queue.new
14
+ @p_r_q = Queue.new
15
+ end
16
+
17
+ def parse
18
+ # default settings
19
+ @opt.args = [:status]
20
+ @opt.concurrency = Etc.respond_to?(:nprocessors) ? Etc.nprocessors : 8
21
+ @opt.http_method = :head
22
+
23
+ parser = OptionParser.new do |o|
24
+ o.on '-s', 'edit by HTTP response status code (default on)' do |arg|
25
+ @opt.args << :status unless opt.args.index(:status)
26
+ end
27
+ o.on '-j=num', 'number of concurrent (default is cpu count)' do |arg|
28
+ raise "number of concurrent should be over 1" unless 0 < arg.to_i
29
+ @opt.concurrency = arg.to_i
30
+ end
31
+ o.on '-m=name', 'request http method (default head)' do |arg|
32
+ @opt.http_method = arg
33
+ end
34
+ end
35
+ parser.parse!(ARGV)
36
+
37
+ self
38
+ end
39
+
40
+ # Thread design
41
+ #
42
+ # | input | -> | pool | -> | | -> | output |
43
+ # | | results |
44
+ # +------------------->| |
45
+ def run
46
+ in_count = 0
47
+ out_count = 0
48
+ quit = false
49
+ m = Mutex.new
50
+ pool = Array.new(@opt.concurrency) {
51
+ Thread.start {
52
+ while true
53
+ url = @i_p_q.pop
54
+ res = fetch(url)
55
+ @p_r_q.push [edit(res), url]
56
+ end
57
+ }
58
+ }
59
+ IO.pipe do |r, w|
60
+ result_thread = Thread.start {
61
+ stock = []
62
+ check = -> {
63
+ if quit && in_count == out_count
64
+ w.write "q"
65
+ w.close
66
+ Thread.exit
67
+ end
68
+ }
69
+ out_proc = -> (set) {
70
+ print "#{set[0]}\t#{set[1]}\n"
71
+ out_count += 1
72
+ stock.delete(set)
73
+ check.call
74
+ }
75
+ while input = @i_r_q.pop
76
+ found = false
77
+ while st = stock.rassoc(input)
78
+ out_proc.call(st)
79
+ found = true
80
+ end
81
+
82
+ next if found
83
+ check.call
84
+ while res = @p_r_q.pop
85
+ if res[1] == input
86
+ out_proc.call(res)
87
+ break
88
+ else
89
+ stock << res
90
+ end
91
+ end
92
+ end
93
+ }
94
+ while line = $stdin.gets
95
+ in_count += 1
96
+ url = line.chomp
97
+ @i_p_q.push url
98
+ @i_r_q.push url
99
+ end
100
+ quit = true
101
+ r.read(1) # wait for all task
102
+ pool.each(&:kill)
103
+ result_thread.kill
104
+ end
105
+ end
106
+
107
+ def option
108
+ @opt
109
+ end
110
+
111
+ def edit(res)
112
+ if res.nil?
113
+ return "???"
114
+ end
115
+ outputs = []
116
+ @opt.args.each do |arg|
117
+ case arg
118
+ when :status
119
+ outputs << res.code
120
+ end
121
+ end
122
+ outputs.join("\t")
123
+ end
124
+
125
+ def fetch(url)
126
+ uri = URI.parse(url)
127
+ klass = Net::HTTP.const_get(@opt.http_method.capitalize.to_sym)
128
+ req = klass.new(uri.path)
129
+ Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == "https") do |http|
130
+ http.open_timeout = 10
131
+ http.read_timeout = 10
132
+ http.request req
133
+ end
134
+ rescue
135
+ return nil
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,3 @@
1
+ module Ballad
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ballad
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ksss
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-10 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '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'
41
+ description: HTTP response edit by line
42
+ email:
43
+ - co000ri@gmail.com
44
+ executables:
45
+ - ballad
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - .travis.yml
51
+ - CODE_OF_CONDUCT.md
52
+ - Gemfile
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - bin/ballad
57
+ - hred.gemspec
58
+ - lib/ballad.rb
59
+ - lib/ballad/cli.rb
60
+ - lib/ballad/version.rb
61
+ homepage: ''
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.0.14
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: HTTP response edit by line
85
+ test_files: []