ducky 0.0.1

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: 041b802768c3710cb7c05265067078ee0ddec8b9
4
+ data.tar.gz: 43b873d162179bdad779857c36744471b861d34a
5
+ SHA512:
6
+ metadata.gz: 7502c398dcdcd57c2a12d2d47a13e79840bfd69b002397456ecb24039ed53d542f1ceb7ecd0e6282edd29bf9a615cde94fb2efe420348cd289183752d2cf338b
7
+ data.tar.gz: 3fff1aead271b898277a0672514391da313054a391b1eab9fc46189b13c5771d359330e53fe151f1de278b8bb6d25d134f23203766ae6d9e0191a06db3652055
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ducky.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 medcat
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,76 @@
1
+ # Ducky
2
+
3
+ Retreives documentation on any core, stdlib, or gem method. Core and
4
+ stdlib get the added benefit of checking the inheritance chain; for
5
+ gems, it only checks the class.
6
+
7
+ ## Installation
8
+
9
+ Install it yourself:
10
+
11
+ $ gem install ducky
12
+
13
+ ## Usage
14
+
15
+ Check out
16
+
17
+ $ ducky help
18
+
19
+ The ducky command line has three subcommands: `core`, `stdlib`, or
20
+ `gem`. `core` expects 1 or 2 parameters; the first is the method to
21
+ lookup, and the second (optional) is the ruby version to look it up
22
+ on. `stdlib` expects 2 or 3 parameters; the first is the stdlib
23
+ library that the method is under, the second is the method, and
24
+ the third (optional) is the ruby version to look it up on. `gem`
25
+ expects 2 or 3 parameters; the first is the gem the method is under,
26
+ the second is the method, and the third (optional) is the version of
27
+ the gem to look it up on (by default, selects the latest version).
28
+
29
+ ### Method Value
30
+
31
+ method values look like `<class><accessor><method>`, where `<class>`
32
+ is the class it's defined on, `<accessor>` is how the method is
33
+ accessed (i.e. one of `.`, `#`, or `::`), and `method` is the method
34
+ name. `<class>` is optional (it defaults to `Object`); others are
35
+ not. For example, `Kernel#printf`, `Array.new`, `#object_id` are
36
+ all valid, but `printf`, `Kernel`, and `Array.` are not.
37
+
38
+ ### Examples
39
+
40
+ > ducky gem antelope "Antelope::Generator::Base#template"
41
+ - (void) template(source, destination) {|content| ... } (protected)
42
+
43
+ This method returns an undefined value.
44
+ Copies a template from the source, runs it through erb (in the context of this
45
+ class), and then outputs it at the destination. If given a block, it will call
46
+ the block after the template is run through erb with the content from erb; the
47
+ result of the block is then used as the content instead.
48
+
49
+ Parameters:
50
+ source (String) — the source file. This should be in source_root.
51
+
52
+ destination (String) — the destination file. This will be in Ace::Grammar#output.
53
+
54
+ Yield Parameters:
55
+ content (String) — The content that ERB created.
56
+
57
+ Yield Returns:
58
+ (String) — The new content to write to the output.
59
+
60
+ > ducky core "Array.new"
61
+ new(size=0, obj=nil)
62
+ new(array)
63
+ new(size) {|index| block }
64
+
65
+
66
+ Returns a new array.
67
+ # (truncated)
68
+
69
+
70
+ ## Contributing
71
+
72
+ 1. Fork it ( https://github.com/medcat/ducky/fork )
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path("../../lib", __FILE__)
4
+ $LOAD_PATH << lib unless $LOAD_PATH.include?(lib)
5
+
6
+ require "ducky"
7
+
8
+ Ducky::CLI.start(ARGV)
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ducky/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ducky"
8
+ spec.version = Ducky::VERSION
9
+ spec.authors = ["medcat"]
10
+ spec.email = ["redjazz96@gmail.com"]
11
+ spec.summary = %q{Lookup documentation.}
12
+ spec.description = %q{Lookup documentation for core, stdlib, and gem methods.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "thor"
22
+ spec.add_dependency "faraday"
23
+ spec.add_dependency "faraday_middleware"
24
+ spec.add_dependency "nokogiri"
25
+ spec.add_development_dependency "bundler", "~> 1.6"
26
+ spec.add_development_dependency "rake"
27
+ end
@@ -0,0 +1,25 @@
1
+ require "ducky/version"
2
+ require "ducky/source"
3
+ require "ducky/sources"
4
+ require "ducky/cli"
5
+ require "ducky/format"
6
+
7
+ module Ducky
8
+
9
+ SOURCES = {
10
+ :core => Sources::Core,
11
+ :stdlib => Sources::StdLib,
12
+ :gem => Sources::Gem
13
+ }
14
+
15
+ def lookup(type, *args)
16
+ source = SOURCES[type]
17
+ Format.format source.new(*args).lookup
18
+
19
+ rescue NoMethodError => e
20
+ e.message
21
+ end
22
+
23
+ extend self
24
+
25
+ end
@@ -0,0 +1,25 @@
1
+ require "thor"
2
+
3
+ module Ducky
4
+ class CLI < Thor
5
+
6
+ desc "core METHOD [VERSION]",
7
+ "lookup the given method in the given ruby version's core"
8
+ def core(method, version = RUBY_VERSION)
9
+ puts Ducky.lookup(:core, method, version)
10
+ end
11
+
12
+ desc "stdlib LIB METHOD [VERSION]",
13
+ "lookup the given method in the given ruby version's stdlib"
14
+ def stdlib(lib, method, version = RUBY_VERSION)
15
+ puts Ducky.lookup(:stdlib, lib, method, version)
16
+ end
17
+
18
+ desc "gem NAME METHOD [VERSION]",
19
+ "lookup the given method in the given gem version's source"
20
+ def gem(name, method, version = :guess)
21
+ puts Ducky.lookup(:gem, name, method, version)
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,42 @@
1
+ module Ducky
2
+ module Format
3
+
4
+ extend self
5
+
6
+ def format(information)
7
+ out = ""
8
+
9
+ information[:head].each do |head|
10
+ out << head << "\n"
11
+ end
12
+
13
+ body = ""
14
+ information[:body].children.each do |child|
15
+ content = word_wrap(child.content.gsub(/\s+/, " "))
16
+ case child.name
17
+ when "comment", "div"
18
+ when "text"
19
+ body << content
20
+ when "p"
21
+ body << content << "\n"
22
+ when /h[1-6]/
23
+ body << "\n\n" << content << "\n"
24
+ when "pre"
25
+ body << "\n\n " <<
26
+ child.content.split("\n").join("\n ") << "\n\n"
27
+ end
28
+ end
29
+
30
+ out << "\n\n" << body.strip << "\n\n" << information[:footer]
31
+ end
32
+
33
+ # thanks, Rails!
34
+ def word_wrap(text, options = {})
35
+ line_width = options.fetch(:line_width, 80)
36
+
37
+ text.split("\n").collect! do |line|
38
+ line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
39
+ end * "\n"
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,72 @@
1
+ require "nokogiri"
2
+ require "erb"
3
+
4
+ module Ducky
5
+ class Source
6
+
7
+ include ERB::Util
8
+
9
+ def initialize(method)
10
+ @method = method
11
+ @receiver, @type, @message = split_method
12
+ end
13
+
14
+ def response
15
+ @_response ||= source.get(url)
16
+ end
17
+
18
+ def document
19
+ @_document ||= Nokogiri::HTML(response.body)
20
+ end
21
+
22
+ def lookup
23
+ raise NotImplementedError
24
+ end
25
+
26
+ private
27
+
28
+ def url
29
+ raise NotImplementedError
30
+ end
31
+
32
+ def handle_error
33
+ core = self.class.
34
+ new("#{@method.gsub(@receiver, parent)}", version)
35
+ core.lookup
36
+ rescue NoMethodError
37
+ no_method!
38
+ end
39
+
40
+ def parent
41
+ no_method!
42
+ end
43
+
44
+ def no_method!
45
+ raise NoMethodError, "No method named #{@method}"
46
+ end
47
+
48
+ def split_method
49
+ match = @method.match(/\A(.*)(\.|\#|\:\:)(.*?)\z/)
50
+ no_method! unless match
51
+
52
+ base = if match[1].empty?
53
+ "Object"
54
+ else
55
+ match[1]
56
+ end
57
+
58
+ type = case match[2]
59
+ when ".", "::"
60
+ :class
61
+ when "#"
62
+ :instance
63
+ end
64
+
65
+ [
66
+ base.gsub("::", "/"),
67
+ type,
68
+ url_encode(match[3])
69
+ ]
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,28 @@
1
+ require "faraday"
2
+ require "faraday_middleware"
3
+ require "ducky/sources/core"
4
+ require "ducky/sources/gem"
5
+ require "ducky/sources/stdlib"
6
+
7
+ module Ducky
8
+ module Sources
9
+
10
+ RUBY_DOC = Faraday.new(:url => "http://ruby-doc.org") do |conn|
11
+ conn.use Faraday::Response::RaiseError
12
+ conn.adapter Faraday.default_adapter
13
+ end
14
+
15
+
16
+ RUBYDOC_INFO = Faraday.new(:url => "http://rubydoc.info") do |conn|
17
+ conn.use Faraday::Response::RaiseError
18
+ conn.adapter Faraday.default_adapter
19
+ end
20
+
21
+ RUBY_GEMS = Faraday.new(:url => "http://rubygems.org") do |conn|
22
+ conn.use Faraday::Response::RaiseError
23
+ conn.response :json
24
+ conn.adapter Faraday.default_adapter
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,77 @@
1
+ module Ducky
2
+ module Sources
3
+ class Core < Source
4
+
5
+ include ERB::Util
6
+
7
+ def initialize(method, version)
8
+ @method = method
9
+ @version = version
10
+
11
+ super(method)
12
+ end
13
+
14
+ def lookup
15
+ elements = document.css(selector)
16
+ information = {
17
+ :head => [],
18
+ :body => [],
19
+ :source => "",
20
+ :footer => "",
21
+ :url => url
22
+ }
23
+
24
+ elements.each do |element|
25
+ element.css(".method-heading").each do |call|
26
+ information[:head] << call.inner_text.
27
+ gsub("click to toggle source", "").strip
28
+ end
29
+
30
+ information[:body] = element.
31
+ css('div:not(.method-source-code):not(.aliases)').last
32
+ source = element.css('.method-source-code').each do |source|
33
+ information[:source] = source.content
34
+ end
35
+
36
+ element.css('.aliases').each do |a|
37
+ information[:footer] << a << "\n"
38
+ end
39
+
40
+ information[:footer].strip!
41
+ end
42
+
43
+ unless elements.any?
44
+ handle_error
45
+ else
46
+ information
47
+ end
48
+
49
+ rescue Faraday::ResourceNotFound
50
+ no_method!
51
+ end
52
+
53
+ private
54
+
55
+ def source
56
+ RUBY_DOC
57
+ end
58
+
59
+ def selector
60
+ "#public-#{@type}-method-details " \
61
+ "##{@message.gsub(/[\W]/, "-").gsub(/\A-/, "")}-method"
62
+ end
63
+
64
+ def url
65
+ "/core-#{@version}/#{@receiver}.html"
66
+ end
67
+
68
+ def parent
69
+ @_parent ||= document.css("#parent-class-section p.link a").
70
+ first.content
71
+
72
+ rescue
73
+ super
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,103 @@
1
+ module Ducky
2
+ module Sources
3
+ class Gem < Source
4
+
5
+ def initialize(name, method, version = :guess)
6
+ @name = name
7
+ @method = method
8
+ @version = version
9
+
10
+ super(method)
11
+ end
12
+
13
+ def lookup
14
+ methods = document.css('#method_details .method_details')
15
+ no_method! unless methods.any?
16
+
17
+ methods.each do |method|
18
+ method.css('.signature').each do |sig|
19
+ information[:head] << sig.inner_text.strip
20
+ end
21
+
22
+ information[:body] = document.
23
+ css('.docstring .discussion').last
24
+
25
+ information[:footer] =
26
+ create_footer(document.css('.tags').last)
27
+
28
+ information[:source] = document.
29
+ css('.source_code pre.code').last.inner_text
30
+ end
31
+
32
+ information
33
+
34
+ rescue Faraday::ResourceNotFound
35
+ no_method!
36
+ end
37
+
38
+ private
39
+
40
+ def create_footer(tags)
41
+ return unless tags
42
+ out = ""
43
+
44
+ tags.children.each do |tag|
45
+ case tag.name
46
+ when "p"
47
+ out << tag.inner_text.strip
48
+ when "ul"
49
+ tag.children.each do |li|
50
+ li.children.each do |span|
51
+ content = span.inner_text.gsub(/\s+/, " ")
52
+
53
+ out << Format.word_wrap(content, line_width: 71)
54
+ end
55
+ out << "\n"
56
+
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+
63
+ out
64
+ end
65
+
66
+ def source
67
+ RUBYDOC_INFO
68
+ end
69
+
70
+ def information
71
+ @_information ||= {
72
+ :head => [],
73
+ :body => [],
74
+ :source => "",
75
+ :footer => "",
76
+ :url => url
77
+ }
78
+ end
79
+
80
+ def version
81
+ @_version ||= begin
82
+ if @version == :guess
83
+ response = RUBY_GEMS.get("/api/v1/gems/#{@name}.json")
84
+ response.body["version"]
85
+ else
86
+ @version
87
+ end
88
+ end
89
+ end
90
+
91
+ def url
92
+ part = "/gems/#{@name}/#{version}/#{@receiver}"
93
+ if @type == :class
94
+ part << "."
95
+ else
96
+ part << ":"
97
+ end
98
+
99
+ part << @message
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,21 @@
1
+ module Ducky
2
+ module Sources
3
+ class StdLib < Core
4
+
5
+ def initialize(lib, method, version)
6
+ @lib = lib
7
+ super(method, version)
8
+ end
9
+
10
+ private
11
+
12
+ def url
13
+ "/stdlib-#{version}/libdoc/#{@lib}/rdoc/#{receiver}.html"
14
+ end
15
+
16
+ def parent
17
+ no_method!
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Ducky
2
+ VERSION = "0.0.1".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ducky
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - medcat
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday_middleware
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Lookup documentation for core, stdlib, and gem methods.
98
+ email:
99
+ - redjazz96@gmail.com
100
+ executables:
101
+ - ducky
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - bin/ducky
111
+ - ducky.gemspec
112
+ - lib/ducky.rb
113
+ - lib/ducky/cli.rb
114
+ - lib/ducky/format.rb
115
+ - lib/ducky/source.rb
116
+ - lib/ducky/sources.rb
117
+ - lib/ducky/sources/core.rb
118
+ - lib/ducky/sources/gem.rb
119
+ - lib/ducky/sources/stdlib.rb
120
+ - lib/ducky/version.rb
121
+ homepage: ''
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.2.2
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Lookup documentation.
145
+ test_files: []