yard-link_stdlib 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 +7 -0
- data/LICENSE.txt +28 -0
- data/NAME +1 -0
- data/README.md +39 -0
- data/VERSION +1 -0
- data/bin/make_map.rb +107 -0
- data/lib/yard-link_stdlib.rb +21 -0
- data/lib/yard/cli/link_stdlib.rb +147 -0
- data/lib/yard/link_stdlib.rb +212 -0
- data/lib/yard/link_stdlib/dump.rb +74 -0
- data/lib/yard/link_stdlib/html_helper.rb +133 -0
- data/lib/yard/link_stdlib/object_map.rb +217 -0
- data/lib/yard/link_stdlib/ruby_source.rb +189 -0
- data/lib/yard/link_stdlib/ruby_version.rb +233 -0
- data/lib/yard/link_stdlib/version.rb +101 -0
- data/maps/ruby-2.3.0.json.gz +0 -0
- data/maps/ruby-2.4.0.json.gz +0 -0
- data/maps/ruby-2.5.0.json.gz +0 -0
- metadata +176 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2a59fb5f579b3ba52d91480842cba718088aabf7
|
4
|
+
data.tar.gz: 719132fdcb142763240354613603ade4a7c1422d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: df1320327489dbe4bb9f61420922a85aa73a2a317d54c7e5311b5c42e43b5f031a775d62816dbfb5e5e5c8b23f680081aac9965fc8a25c1f575b31189674bc34
|
7
|
+
data.tar.gz: 83e3a17de615b41176dc0aab4ed5bd93e851d84a050f7ea91ce30af17bee4df5f5f8094b151b2bc74a517a4cdc0f60858204cc2d820a175bc0a654b36ee41c70
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
The BSD License ('Cause we West Coast)
|
2
|
+
|
3
|
+
Copyright Two-Thousand Eighteen by NRSER A.K.A. Neil R. Souza
|
4
|
+
|
5
|
+
Redistribution and use in source and binary forms, with or without modification,
|
6
|
+
are permitted provided that the following conditions are met:
|
7
|
+
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
9
|
+
list of conditions and the following disclaimer.
|
10
|
+
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
12
|
+
this list of conditions and the following disclaimer in the documentation and/or
|
13
|
+
other materials provided with the distribution.
|
14
|
+
|
15
|
+
3. Neither the name of the copyright holder nor the names of its contributors
|
16
|
+
may be used to endorse or promote products derived from this software without
|
17
|
+
specific prior written permission.
|
18
|
+
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
20
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
21
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
23
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
24
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
25
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
26
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
27
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
28
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# YARD::LinkStdlib
|
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/yard/link_stdlib`. 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 'yard-link_stdlib'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install yard-link_stdlib
|
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. Then, run `rake spec` to run the tests. 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]/yard-link_stdlib.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/bin/make_map.rb
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'pathname'
|
4
|
+
require 'fileutils'
|
5
|
+
require 'zlib'
|
6
|
+
|
7
|
+
require 'rdoc/rdoc'
|
8
|
+
|
9
|
+
# Get paths in order - we want to be in the Ruby repo checkout
|
10
|
+
GEM_ROOT = Pathname.new( __dir__ ).join( '..' ).expand_path
|
11
|
+
# REPO = GEM_ROOT.join 'tmp', 'ruby'
|
12
|
+
# REPO = GEM_ROOT.join 'tmp', 'ruby-2_5_1'
|
13
|
+
|
14
|
+
class RDoc::RDoc
|
15
|
+
# Pretty much a copy of `RDoc::RDoc#document`, just with the `#generate` step
|
16
|
+
# commented-out.
|
17
|
+
def almost_document options = ARGV
|
18
|
+
self.store = RDoc::Store.new
|
19
|
+
|
20
|
+
if RDoc::Options === options then
|
21
|
+
@options = options
|
22
|
+
@options.finish
|
23
|
+
else
|
24
|
+
@options = load_options
|
25
|
+
@options.parse options
|
26
|
+
end
|
27
|
+
|
28
|
+
if @options.pipe then
|
29
|
+
handle_pipe
|
30
|
+
exit
|
31
|
+
end
|
32
|
+
|
33
|
+
unless @options.coverage_report then
|
34
|
+
@last_modified = setup_output_dir @options.op_dir, @options.force_update
|
35
|
+
end
|
36
|
+
|
37
|
+
@store.encoding = @options.encoding
|
38
|
+
@store.dry_run = @options.dry_run
|
39
|
+
@store.main = @options.main_page
|
40
|
+
@store.title = @options.title
|
41
|
+
@store.path = @options.op_dir
|
42
|
+
|
43
|
+
@start_time = Time.now
|
44
|
+
|
45
|
+
@store.load_cache
|
46
|
+
|
47
|
+
file_info = parse_files @options.files
|
48
|
+
|
49
|
+
@options.default_title = "RDoc Documentation"
|
50
|
+
|
51
|
+
@store.complete @options.visibility
|
52
|
+
|
53
|
+
@stats.coverage_level = @options.coverage_report
|
54
|
+
|
55
|
+
gen_klass = @options.generator
|
56
|
+
|
57
|
+
@generator = gen_klass.new @store, @options
|
58
|
+
|
59
|
+
# generate
|
60
|
+
nil
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def main args
|
65
|
+
src = Pathname.new( args.shift ).expand_path
|
66
|
+
dest = Pathname.new( args.shift ).expand_path
|
67
|
+
|
68
|
+
puts "src: #{ src.inspect }"
|
69
|
+
puts "dest: #{ dest.inspect }"
|
70
|
+
|
71
|
+
# RDoc needs this output dir arg in `ARGV` or it will bail out with an error
|
72
|
+
# due to `//doc` existing, even though we don't ever actually write to any of
|
73
|
+
# it.
|
74
|
+
unless args.include? '--op'
|
75
|
+
args << '--op'
|
76
|
+
args << '/tmp/not_actually_used'
|
77
|
+
end
|
78
|
+
|
79
|
+
Dir.chdir src
|
80
|
+
|
81
|
+
rd = RDoc::RDoc.new
|
82
|
+
|
83
|
+
rd.almost_document args
|
84
|
+
|
85
|
+
map = {}
|
86
|
+
|
87
|
+
rd.store.all_classes_and_modules.each do |mod|
|
88
|
+
map[mod.full_name] = mod.path
|
89
|
+
|
90
|
+
mod.class_method_list.each do |class_method|
|
91
|
+
map[class_method.full_name] = class_method.path
|
92
|
+
end
|
93
|
+
|
94
|
+
mod.instance_method_list.each do |instance_method|
|
95
|
+
map[instance_method.full_name] = instance_method.path
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
FileUtils.mkdir_p dest.dirname unless dest.dirname.exist?
|
100
|
+
|
101
|
+
Zlib::GzipWriter.open dest do |gz|
|
102
|
+
gz.write JSON.pretty_generate( map )
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# Kickoff!
|
107
|
+
main( ARGV ) if __FILE__ == $0
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
##############################################################################
|
5
|
+
# Plugin Entry Point for YARD
|
6
|
+
# ============================================================================
|
7
|
+
#
|
8
|
+
# While the library itself lives in the `YARD::StdLib` namespaces under the
|
9
|
+
# usual directory structure of `//lib/yard/link_stdlib`, when requiring
|
10
|
+
# plugins YARD will reach for `yard-link_stdlib`, basically just calling
|
11
|
+
#
|
12
|
+
# require 'yard-link_stdlib'
|
13
|
+
#
|
14
|
+
# which leads it here. This is nice, because it lets us use the kind-of funkily
|
15
|
+
#
|
16
|
+
##############################################################################
|
17
|
+
|
18
|
+
|
19
|
+
require_relative "./yard/link_stdlib"
|
20
|
+
|
21
|
+
YARD::LinkStdlib.install!
|
@@ -0,0 +1,147 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
# Requirements
|
5
|
+
# =======================================================================
|
6
|
+
|
7
|
+
# Stdlib
|
8
|
+
# -----------------------------------------------------------------------
|
9
|
+
|
10
|
+
# Deps
|
11
|
+
# -----------------------------------------------------------------------
|
12
|
+
|
13
|
+
# We need {YARD::CLI::Command}
|
14
|
+
require 'yard'
|
15
|
+
|
16
|
+
# Project / Package
|
17
|
+
# -----------------------------------------------------------------------
|
18
|
+
|
19
|
+
require 'yard/link_stdlib/ruby_source'
|
20
|
+
|
21
|
+
|
22
|
+
# Namespace
|
23
|
+
# =======================================================================
|
24
|
+
|
25
|
+
module YARD
|
26
|
+
module CLI
|
27
|
+
|
28
|
+
|
29
|
+
# Definitions
|
30
|
+
# =======================================================================
|
31
|
+
|
32
|
+
# @todo document LinkStdlib class.
|
33
|
+
class LinkStdlib < Command
|
34
|
+
|
35
|
+
# Subcommands
|
36
|
+
# ============================================================================
|
37
|
+
|
38
|
+
class List < Command
|
39
|
+
def description
|
40
|
+
"List Ruby versions"
|
41
|
+
end
|
42
|
+
|
43
|
+
def run
|
44
|
+
log.puts \
|
45
|
+
YARD::LinkStdlib::ObjectMap.
|
46
|
+
list.
|
47
|
+
map { |om| om.version.to_s }.
|
48
|
+
join( "\n" )
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
class Add < Command
|
54
|
+
def description
|
55
|
+
"Download version source and build object map"
|
56
|
+
end
|
57
|
+
|
58
|
+
def run version
|
59
|
+
log.puts "Adding object map for Ruby #{ version }..."
|
60
|
+
YARD::LinkStdlib::ObjectMap.new( version ).make
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
class Help < Command
|
66
|
+
def description
|
67
|
+
"Show this message"
|
68
|
+
end
|
69
|
+
|
70
|
+
def run
|
71
|
+
commands = LinkStdlib.commands
|
72
|
+
log.puts "Usage: yard stdlib COMMAND... [OPTIONS] [ARGS]"
|
73
|
+
log.puts
|
74
|
+
log.puts "Commands:"
|
75
|
+
commands.keys.sort_by(&:to_s).each do |command_name|
|
76
|
+
command_class = commands[command_name]
|
77
|
+
next unless command_class < Command
|
78
|
+
command = command_class.new
|
79
|
+
log.puts "%-8s %s" % [command_name, command.description]
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
# Instance Methods
|
86
|
+
# ========================================================================
|
87
|
+
|
88
|
+
def description
|
89
|
+
"Mange Ruby stdlib linking"
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
def self.commands
|
94
|
+
{
|
95
|
+
help: Help,
|
96
|
+
list: List,
|
97
|
+
add: Add,
|
98
|
+
}
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
def run *args
|
103
|
+
target = self.class.commands
|
104
|
+
|
105
|
+
args = [ 'help' ] if args.empty?
|
106
|
+
|
107
|
+
while target.is_a? Hash
|
108
|
+
key = args[0].gsub( '-', '_' ).to_sym
|
109
|
+
if target.key? key
|
110
|
+
target = target[key]
|
111
|
+
args.shift
|
112
|
+
else
|
113
|
+
raise "Bad command name: #{ args[0] }"
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
target.run( *args )
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
protected
|
122
|
+
# ========================================================================
|
123
|
+
|
124
|
+
# @todo Document respond method.
|
125
|
+
#
|
126
|
+
# @param [type] arg_name
|
127
|
+
# @todo Add name param description.
|
128
|
+
#
|
129
|
+
# @return [return_type]
|
130
|
+
# @todo Document return value.
|
131
|
+
#
|
132
|
+
def respond response
|
133
|
+
log.puts response unless response.nil?
|
134
|
+
exit true
|
135
|
+
end # #respond
|
136
|
+
|
137
|
+
public # end protected ***************************************************
|
138
|
+
|
139
|
+
|
140
|
+
end # class LinkStdlib
|
141
|
+
|
142
|
+
|
143
|
+
# /Namespace
|
144
|
+
# =======================================================================
|
145
|
+
|
146
|
+
end # module CLI
|
147
|
+
end # module YARD
|
@@ -0,0 +1,212 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
# Requirements
|
5
|
+
# =======================================================================
|
6
|
+
|
7
|
+
# Stdlib
|
8
|
+
# -----------------------------------------------------------------------
|
9
|
+
|
10
|
+
require 'shellwords'
|
11
|
+
|
12
|
+
# Project / Package
|
13
|
+
# -----------------------------------------------------------------------
|
14
|
+
|
15
|
+
require_relative "./cli/link_stdlib"
|
16
|
+
require_relative "./link_stdlib/version"
|
17
|
+
require_relative "./link_stdlib/html_helper"
|
18
|
+
|
19
|
+
|
20
|
+
# Namespace
|
21
|
+
# =======================================================================
|
22
|
+
|
23
|
+
module YARD
|
24
|
+
module LinkStdlib
|
25
|
+
|
26
|
+
|
27
|
+
# Definitions
|
28
|
+
# =======================================================================
|
29
|
+
|
30
|
+
# Constants
|
31
|
+
# ----------------------------------------------------------------------------
|
32
|
+
|
33
|
+
# Available helper modules by their format (as found in `options.format`).
|
34
|
+
#
|
35
|
+
# We only cover `:html` for the moment, but may add more in the future.
|
36
|
+
#
|
37
|
+
# @return [Hash<Symbol, Module>]
|
38
|
+
#
|
39
|
+
HELPERS_BY_FORMAT = {
|
40
|
+
html: HtmlHelper,
|
41
|
+
}.freeze
|
42
|
+
|
43
|
+
|
44
|
+
# The {Proc} that we add to {YARD::Templates::Template.extra_includes} on
|
45
|
+
# {.install!}. The proc accepts template options and responds with the helper
|
46
|
+
# module corresponding to the format (if any - right now we only handle
|
47
|
+
# `:html`).
|
48
|
+
#
|
49
|
+
# We want this to be a constant so we can tell if it's there and
|
50
|
+
# avoid ever double-adding it.
|
51
|
+
#
|
52
|
+
# @return [Proc<YARD::Templates::TemplateOptions -> Module?>]
|
53
|
+
#
|
54
|
+
HELPER_FOR_OPTIONS = proc { |options|
|
55
|
+
HELPERS_BY_FORMAT[ options.format ]
|
56
|
+
}.freeze
|
57
|
+
|
58
|
+
|
59
|
+
# Add the {HELPER_FOR_OPTIONS} {Proc} to
|
60
|
+
# {YARD::Templates::Template.extra_includes} (if it's not there already).
|
61
|
+
#
|
62
|
+
# @see https://www.rubydoc.info/gems/yard/YARD/Templates/Template#extra_includes-class_method
|
63
|
+
#
|
64
|
+
# @return [nil]
|
65
|
+
#
|
66
|
+
def self.install!
|
67
|
+
# NOTE Due to YARD start-up order, this happens *before* log level is set,
|
68
|
+
# so the `--debug` CLI switch won't help see it... don't know a way to
|
69
|
+
# at the moment.
|
70
|
+
log.debug "Installing `yard-link_stdlib` plugin..."
|
71
|
+
|
72
|
+
unless YARD::Templates::Template.extra_includes.include? HELPER_FOR_OPTIONS
|
73
|
+
YARD::Templates::Template.extra_includes << HELPER_FOR_OPTIONS
|
74
|
+
end
|
75
|
+
|
76
|
+
YARD::CLI::CommandParser.commands[:stdlib] ||= YARD::CLI::LinkStdlib
|
77
|
+
|
78
|
+
nil
|
79
|
+
end # .install!
|
80
|
+
|
81
|
+
|
82
|
+
# General Utilities
|
83
|
+
# ----------------------------------------------------------------------------
|
84
|
+
|
85
|
+
# @param [Symbol | #to_s] value
|
86
|
+
# Either an object whose string representation expands to a path to an
|
87
|
+
# existing directory, or one of the following symbols:
|
88
|
+
#
|
89
|
+
# 1. `:system`, `:global` - `/tmp/yard-link_stdlib`
|
90
|
+
# 2.
|
91
|
+
#
|
92
|
+
# @return [Pathname]
|
93
|
+
# The assigned path.
|
94
|
+
#
|
95
|
+
def self.tmp_dir= value
|
96
|
+
@tmp_dir = case value
|
97
|
+
when :system, :global
|
98
|
+
Pathname.new '/tmp/yard-link_stdlib'
|
99
|
+
when :user
|
100
|
+
Pathname.new( '~/tmp/yard-link_stdlib' ).expand_path
|
101
|
+
when :gem, :install
|
102
|
+
ROOT.join 'tmp'
|
103
|
+
when :project
|
104
|
+
Pathname.getwd.join 'tmp', 'yard-link_stdlib'
|
105
|
+
else
|
106
|
+
dir = Pathname.new( value.to_s ).expand_path
|
107
|
+
|
108
|
+
unless dir.directory?
|
109
|
+
raise ArgumentError,
|
110
|
+
"When assigning a custom tmp_dir path it must be an existing " +
|
111
|
+
"directory, received #{ value.to_s.inspect }"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
FileUtils.mkdir_p @tmp_dir unless @tmp_dir.exist?
|
116
|
+
|
117
|
+
@tmp_dir
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
# Get where to put temporary shit, most Ruby source code that's been downloaded
|
122
|
+
# to generate the link maps from.
|
123
|
+
#
|
124
|
+
# @return [Pathname]
|
125
|
+
#
|
126
|
+
def self.tmp_dir &block
|
127
|
+
if @tmp_dir.nil?
|
128
|
+
self.tmp_dir = repo? ? :gem : :user
|
129
|
+
end
|
130
|
+
|
131
|
+
if block
|
132
|
+
Dir.chdir @tmp_dir, &block
|
133
|
+
else
|
134
|
+
@tmp_dir
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
|
139
|
+
# Run a {Kernel.system}, raising if it fails.
|
140
|
+
#
|
141
|
+
# @param [Array] *args
|
142
|
+
# See {Kernel.system}.
|
143
|
+
#
|
144
|
+
# @return [true]
|
145
|
+
#
|
146
|
+
# @raise [SystemCallError]
|
147
|
+
# If the command fails.
|
148
|
+
#
|
149
|
+
def self.system! *args
|
150
|
+
opts = args[-1].is_a?( Hash ) ? args.pop : {}
|
151
|
+
env = args[0].is_a?( Hash ) ? args.shift : {}
|
152
|
+
|
153
|
+
log.info [
|
154
|
+
"Making system call:",
|
155
|
+
"\t#{ Shellwords.join args }",
|
156
|
+
( opts.empty? ? nil : "\toptions: #{ opts.inspect }" ),
|
157
|
+
( env.empty? ? nil : "\tenv: #{ env.inspect }" ),
|
158
|
+
].compact.join( "\n" )
|
159
|
+
|
160
|
+
Kernel.system( *args ).tap { |success|
|
161
|
+
unless success
|
162
|
+
raise SystemCallError.new \
|
163
|
+
%{ Code #{ $?.exitstatus } error executing #{ args.inspect } },
|
164
|
+
$?.exitstatus
|
165
|
+
end
|
166
|
+
}
|
167
|
+
end
|
168
|
+
|
169
|
+
|
170
|
+
# Make a `GET` request. Follows redirects. Handles SSL.
|
171
|
+
#
|
172
|
+
# @param [String] url
|
173
|
+
# What ya want.
|
174
|
+
#
|
175
|
+
# @param [Integer] redirect_limit
|
176
|
+
# Max number of redirects to follow before it gives up.
|
177
|
+
#
|
178
|
+
# @return [Net::HTTPResponse]
|
179
|
+
# The first successful response that's not a redirect.
|
180
|
+
#
|
181
|
+
# @raise [Net::HTTPError]
|
182
|
+
# If there was an HTTP error.
|
183
|
+
#
|
184
|
+
# @raise
|
185
|
+
#
|
186
|
+
def self.http_get url, redirect_limit = 5
|
187
|
+
raise "Too many HTTP redirects" if redirect_limit < 0
|
188
|
+
|
189
|
+
uri = URI url
|
190
|
+
request = Net::HTTP::Get.new uri.path
|
191
|
+
response = Net::HTTP.start(
|
192
|
+
uri.host,
|
193
|
+
uri.port,
|
194
|
+
use_ssl: uri.scheme == 'https',
|
195
|
+
) { |http| http.request request }
|
196
|
+
|
197
|
+
case response
|
198
|
+
when Net::HTTPSuccess
|
199
|
+
response
|
200
|
+
when Net::HTTPRedirection
|
201
|
+
http_get response['location'], redirect_limit - 1
|
202
|
+
else
|
203
|
+
response.error!
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
|
208
|
+
# /Namespace
|
209
|
+
# =======================================================================
|
210
|
+
|
211
|
+
end # module LinkStdlib
|
212
|
+
end # module YARD
|